privatization
This commit is contained in:
225
src/index.ts
225
src/index.ts
@@ -1,104 +1,149 @@
|
||||
import { WsService } from "@dpu/shared";
|
||||
import { logError } from "@dpu/shared/dist/logger.js";
|
||||
import fastifyCors from "@fastify/cors";
|
||||
import fastifySwagger from "@fastify/swagger";
|
||||
import fastifySwaggerUi from "@fastify/swagger-ui";
|
||||
import fastifyCors, { type FastifyCorsOptions } from "@fastify/cors";
|
||||
import fastifySwagger, { type SwaggerOptions } from "@fastify/swagger";
|
||||
import fastifySwaggerUi, {
|
||||
type FastifySwaggerUiOptions,
|
||||
} from "@fastify/swagger-ui";
|
||||
import fastifyWebsocket from "@fastify/websocket";
|
||||
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||
import Fastify from "fastify";
|
||||
import fastifyAxios from "fastify-axios";
|
||||
import fastifyAxios, { type FastifyAxiosOptions } from "fastify-axios";
|
||||
import {
|
||||
jsonSchemaTransform,
|
||||
serializerCompiler,
|
||||
validatorCompiler,
|
||||
type ZodTypeProvider,
|
||||
} from "fastify-type-provider-zod";
|
||||
import type { OpenAPIV3 } from "openapi-types";
|
||||
import { SwaggerTheme, SwaggerThemeNameEnum } from "swagger-themes";
|
||||
import { z } from "zod";
|
||||
import { Config } from "./config.js";
|
||||
import { GristClient } from "./grist/client.js";
|
||||
import { gristRoutes } from "./grist/routes.js";
|
||||
import { privateGristRoutes } from "./grist/private-routes.js";
|
||||
import { GristService } from "./grist/service.js";
|
||||
import { HomeAssistantClient } from "./homeassistant/client.js";
|
||||
import { homeAssistantRoutes } from "./homeassistant/routes.js";
|
||||
import { privateHomeAssistantRoutes } from "./homeassistant/private-routes.js";
|
||||
import { HomeAssistantService } from "./homeassistant/service.js";
|
||||
import { homepageRoutes } from "./homepage/routes.js";
|
||||
import { privateHomepageRoutes } from "./homepage/private-routes.js";
|
||||
import { publicHomepageRoutes } from "./homepage/public-routes.js";
|
||||
import { HomepageService } from "./homepage/service.js";
|
||||
import { TidalClient } from "./tidal/client.js";
|
||||
import { tidalRoutes } from "./tidal/routes.js";
|
||||
import { privateTidalRoutes } from "./tidal/private-routes.js";
|
||||
import { TidalService } from "./tidal/service.js";
|
||||
import { wsRoutes } from "./websocket/routes.js";
|
||||
import { publicWsRoutes } from "./websocket/public-routes.js";
|
||||
|
||||
const fastify = Fastify().withTypeProvider<ZodTypeProvider>();
|
||||
// Initialize with Zod
|
||||
const publicServer = Fastify().withTypeProvider<ZodTypeProvider>();
|
||||
const privateServer = Fastify().withTypeProvider<ZodTypeProvider>();
|
||||
publicServer.setValidatorCompiler(validatorCompiler);
|
||||
publicServer.setSerializerCompiler(serializerCompiler);
|
||||
privateServer.setValidatorCompiler(validatorCompiler);
|
||||
privateServer.setSerializerCompiler(serializerCompiler);
|
||||
|
||||
fastify.setValidatorCompiler(validatorCompiler);
|
||||
fastify.setSerializerCompiler(serializerCompiler);
|
||||
// Cors
|
||||
function getCorsObject(urls: string[]): FastifyCorsOptions {
|
||||
return {
|
||||
origin: urls,
|
||||
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||||
allowedHeaders: ["Content-Type", "Authorization"],
|
||||
};
|
||||
}
|
||||
|
||||
await fastify.register(fastifyCors, {
|
||||
origin: ["http://localhost:4321", "https://dariusbag.dev"],
|
||||
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||||
allowedHeaders: ["Content-Type", "Authorization"],
|
||||
});
|
||||
await publicServer.register(
|
||||
fastifyCors,
|
||||
getCorsObject(["https://dariusbag.dev"]),
|
||||
);
|
||||
await privateServer.register(
|
||||
fastifyCors,
|
||||
getCorsObject(["http://localhost:4321"]),
|
||||
);
|
||||
|
||||
await fastify.register(fastifySwagger, {
|
||||
openapi: {
|
||||
info: {
|
||||
title: "DPU API",
|
||||
description: "API Documentation",
|
||||
version: "1.0.0",
|
||||
// Swagger
|
||||
function getSwaggerObject(servers: OpenAPIV3.ServerObject[]): SwaggerOptions {
|
||||
return {
|
||||
openapi: {
|
||||
info: {
|
||||
title: "DPU API",
|
||||
description: "API Documentation",
|
||||
version: "1.0.0",
|
||||
},
|
||||
servers: servers,
|
||||
},
|
||||
servers: [
|
||||
{ url: "http://localhost:8080", description: "dev" },
|
||||
{ url: "https://dpu.dariusbag.dev/api", description: "prod" },
|
||||
],
|
||||
},
|
||||
transform: jsonSchemaTransform,
|
||||
});
|
||||
transform: jsonSchemaTransform,
|
||||
};
|
||||
}
|
||||
|
||||
await publicServer.register(
|
||||
fastifySwagger,
|
||||
getSwaggerObject([
|
||||
{ url: "https://dpu.dariusbag.dev/api", description: "prod" },
|
||||
]),
|
||||
);
|
||||
await privateServer.register(
|
||||
fastifySwagger,
|
||||
getSwaggerObject([
|
||||
{ url: "http://192.168.178.161:20001", description: "prod" },
|
||||
]),
|
||||
);
|
||||
|
||||
// Swagger UI
|
||||
const theme = new SwaggerTheme();
|
||||
const content = theme.getBuffer(SwaggerThemeNameEnum.ONE_DARK);
|
||||
|
||||
await fastify.register(fastifySwaggerUi, {
|
||||
routePrefix: "/docs",
|
||||
indexPrefix: `${Config.env_dev ? "" : "/api"}`,
|
||||
uiConfig: {
|
||||
docExpansion: "list",
|
||||
deepLinking: false,
|
||||
},
|
||||
theme: {
|
||||
css: [{ filename: "theme.css", content: content }],
|
||||
},
|
||||
});
|
||||
function getSwaggerUiObject(indexPrefix: string): FastifySwaggerUiOptions {
|
||||
return {
|
||||
routePrefix: "/docs",
|
||||
indexPrefix: indexPrefix,
|
||||
uiConfig: {
|
||||
docExpansion: "list",
|
||||
deepLinking: false,
|
||||
},
|
||||
theme: {
|
||||
css: [{ filename: "theme.css", content: content }],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
await fastify.register(fastifyAxios, {
|
||||
clients: {
|
||||
grist: {
|
||||
baseURL: Config.grist.api_url,
|
||||
headers: {
|
||||
Authorization: `Bearer ${Config.grist.api_token}`,
|
||||
await publicServer.register(fastifySwaggerUi, getSwaggerUiObject("/api"));
|
||||
await privateServer.register(fastifySwaggerUi, getSwaggerUiObject(""));
|
||||
|
||||
// axios
|
||||
function getAxiosConfig(): FastifyAxiosOptions {
|
||||
return {
|
||||
clients: {
|
||||
grist: {
|
||||
baseURL: Config.grist.api_url,
|
||||
headers: {
|
||||
Authorization: `Bearer ${Config.grist.api_token}`,
|
||||
},
|
||||
},
|
||||
homeassistant: {
|
||||
baseURL: Config.homeassistant.api_url,
|
||||
headers: {
|
||||
Authorization: `Bearer ${Config.homeassistant.api_token}`,
|
||||
},
|
||||
},
|
||||
tidal: {
|
||||
baseURL: `${Config.tidal.address}`,
|
||||
},
|
||||
},
|
||||
homeassistant: {
|
||||
baseURL: Config.homeassistant.api_url,
|
||||
headers: {
|
||||
Authorization: `Bearer ${Config.homeassistant.api_token}`,
|
||||
},
|
||||
},
|
||||
tidal: {
|
||||
baseURL: `${Config.tidal.address}`,
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
await fastify.register(fastifyWebsocket);
|
||||
await privateServer.register(fastifyAxios, getAxiosConfig());
|
||||
|
||||
const gristClient = new GristClient(fastify.axios.grist);
|
||||
// Websockets
|
||||
await publicServer.register(fastifyWebsocket);
|
||||
|
||||
// Clients and Services
|
||||
const gristClient = new GristClient(privateServer.axios.grist);
|
||||
const gristService = new GristService(gristClient);
|
||||
|
||||
const haClient = new HomeAssistantClient(fastify.axios.homeassistant);
|
||||
const haClient = new HomeAssistantClient(privateServer.axios.homeassistant);
|
||||
const haService = new HomeAssistantService(haClient);
|
||||
|
||||
const tidalClient = new TidalClient(fastify.axios.tidal);
|
||||
const tidalClient = new TidalClient(privateServer.axios.tidal);
|
||||
const tidalService = new TidalService(tidalClient);
|
||||
|
||||
const wsService = new WsService();
|
||||
@@ -122,21 +167,45 @@ async function verifyAPIKey(
|
||||
}
|
||||
}
|
||||
|
||||
const port = parseInt(Config.port, 10);
|
||||
|
||||
// Register routes
|
||||
await fastify.register(gristRoutes, { gristService });
|
||||
await fastify.register(homeAssistantRoutes, { haService, verifyAPIKey });
|
||||
await fastify.register(tidalRoutes, { tidalService, verifyAPIKey });
|
||||
await fastify.register(wsRoutes, { wsService });
|
||||
await fastify.register(homepageRoutes, {
|
||||
await publicServer.register(publicWsRoutes, { wsService });
|
||||
await publicServer.register(publicHomepageRoutes, {
|
||||
hpService,
|
||||
});
|
||||
|
||||
await privateServer.register(privateGristRoutes, { gristService });
|
||||
await privateServer.register(privateHomeAssistantRoutes, {
|
||||
haService,
|
||||
verifyAPIKey,
|
||||
});
|
||||
await privateServer.register(privateTidalRoutes, {
|
||||
tidalService,
|
||||
verifyAPIKey,
|
||||
});
|
||||
await privateServer.register(privateHomepageRoutes, {
|
||||
hpService,
|
||||
gristService,
|
||||
haService,
|
||||
verifyAPIKey,
|
||||
});
|
||||
|
||||
fastify.get(
|
||||
// Ping Routes
|
||||
publicServer.get(
|
||||
"/ping",
|
||||
{
|
||||
schema: {
|
||||
description: "Health check endpoint",
|
||||
tags: ["default"],
|
||||
response: {
|
||||
200: z.literal("pong"),
|
||||
},
|
||||
},
|
||||
},
|
||||
async (_request, _reply) => {
|
||||
return "pong" as const;
|
||||
},
|
||||
);
|
||||
privateServer.get(
|
||||
"/ping",
|
||||
{
|
||||
schema: {
|
||||
@@ -152,8 +221,20 @@ fastify.get(
|
||||
},
|
||||
);
|
||||
|
||||
await fastify.ready();
|
||||
fastify.listen({ port: port, host: "0.0.0.0" }, (err, address) => {
|
||||
const port = parseInt(Config.port, 10);
|
||||
const publicPort = port + 1;
|
||||
|
||||
await publicServer.ready();
|
||||
await privateServer.ready();
|
||||
|
||||
publicServer.listen({ port: publicPort, host: "0.0.0.0" }, (err, address) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`Server listening at ${address}`);
|
||||
});
|
||||
privateServer.listen({ port: port, host: "0.0.0.0" }, (err, address) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
|
||||
Reference in New Issue
Block a user