Files

109 lines
2.6 KiB
TypeScript
Raw Permalink Normal View History

2025-07-25 15:15:14 -07:00
import express from "express";
import path from "path";
import cors from "cors";
import helmet from "helmet";
import compression from "compression";
2025-08-11 17:47:09 -04:00
import cookieParser from "cookie-parser";
2026-03-20 12:40:27 -07:00
import swaggerUi, { type JsonObject } from "swagger-ui-express";
2026-01-21 11:18:13 -08:00
import { handleErrors } from "@/middleware/handleErrors.js";
import { generalApiLimiter } from "@/middleware/rateLimiter.js";
import { sanitizeBody, sanitizeQuery } from "@/middleware/sanitization.js";
import { setupRoutes } from "@/config/routes.js";
import { InitializedServices } from "@/config/services.js";
import { InitializedControllers } from "@/config/controllers.js";
import { EnvConfig } from "@/service/system/settingsService.js";
2025-07-28 12:00:09 -07:00
2026-01-21 11:18:13 -08:00
export const createApp = ({
services,
controllers,
envSettings,
frontendPath,
openApiSpec,
}: {
services: InitializedServices;
controllers: InitializedControllers;
envSettings: EnvConfig;
frontendPath: string;
2026-03-20 12:40:27 -07:00
openApiSpec: JsonObject;
2026-01-21 11:18:13 -08:00
}) => {
2025-07-28 14:37:18 -07:00
const allowedOrigin = envSettings.clientHost;
2025-07-25 15:15:14 -07:00
const app = express();
2026-01-30 19:41:41 +00:00
2025-07-28 12:00:09 -07:00
app.use(generalApiLimiter);
2025-07-25 15:15:14 -07:00
app.use(
cors({
origin: allowedOrigin,
methods: "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS",
2026-01-30 19:41:41 +00:00
allowedHeaders: ["Content-Type", "Authorization", "Accept-Language"],
2025-07-25 15:15:14 -07:00
credentials: true,
})
);
2026-01-30 19:41:41 +00:00
app.use(express.static(frontendPath));
2025-07-25 15:15:14 -07:00
app.use(express.json());
2025-08-11 17:47:09 -04:00
app.use(cookieParser());
2025-08-11 17:39:08 -04:00
app.use(sanitizeBody());
app.use(sanitizeQuery());
2025-08-11 17:39:08 -04:00
2025-07-25 15:15:14 -07:00
app.use(
helmet({
hsts: false,
contentSecurityPolicy: {
useDefaults: true,
directives: {
upgradeInsecureRequests: null,
"script-src": ["'self'", "'unsafe-inline'", "'unsafe-eval'"],
"object-src": ["'none'"],
"base-uri": ["'self'"],
2025-07-25 15:15:14 -07:00
},
},
})
);
app.use(
compression({
level: 6,
threshold: 1024,
filter: (req, res) => {
if (req.headers["x-no-compression"]) {
return false;
}
return compression.filter(req, res);
},
})
);
// Swagger UI — dynamically set server URL from request
app.use("/api-docs", swaggerUi.serve, (req: express.Request, res: express.Response, next: express.NextFunction) => {
const protocol = req.protocol;
const host = req.get("host");
const dynamicSpec = {
...openApiSpec,
servers: [
{
url: `${protocol}://${host}/api/v1`,
description: "Current Server",
},
...openApiSpec.servers,
],
};
swaggerUi.setup(dynamicSpec)(req, res, next);
});
2025-07-25 15:15:14 -07:00
app.use("/api/v1/health", (req, res) => {
res.json({
status: "OK",
});
});
// Main app routes
setupRoutes(app, controllers, services);
2025-07-25 15:15:14 -07:00
// FE routes
app.get("*", (req, res) => {
res.sendFile(path.join(frontendPath, "index.html"));
});
app.use(handleErrors);
return app;
};