66 lines
2.2 KiB
TypeScript
66 lines
2.2 KiB
TypeScript
import express from "express";
|
|
import { connectDatabase } from "@/db/MongoDB.js";
|
|
import { initServices, initControllers, initRoutes } from "./init/index.js";
|
|
import cookieParser from "cookie-parser";
|
|
import cors from "cors";
|
|
import { addUserContext } from "./middleware/AddUserContext.js";
|
|
import { verifyToken } from "./middleware/VerifyToken.js";
|
|
import { config } from "@/config/index.js";
|
|
import { getChildLogger } from "@/logger/Logger.js";
|
|
import ApiError from "./utils/ApiError.js";
|
|
const indexLogger = getChildLogger("index");
|
|
import swaggerUi from "swagger-ui-express";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
|
|
const createApp = async () => {
|
|
await connectDatabase();
|
|
const services = await initServices();
|
|
const controllers = initControllers(services);
|
|
const app = express();
|
|
// Stripe webhook requires raw body for signature verification; mount it first
|
|
app.post(
|
|
"/api/v1/stripe/webhook",
|
|
express.raw({ type: "application/json" }) as any,
|
|
// Defer to router handler registered later; this preserves raw body
|
|
(req, res, next) => next()
|
|
);
|
|
app.use(express.json());
|
|
app.use(cookieParser());
|
|
app.use(
|
|
cors({
|
|
origin: config.ORIGIN,
|
|
methods: "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS",
|
|
allowedHeaders: ["Content-Type", "Authorization", "x-team-id"],
|
|
credentials: true,
|
|
})
|
|
);
|
|
const routes = initRoutes(controllers, app);
|
|
// Swagger UI at /api-docs
|
|
try {
|
|
// Load OpenAPI doc from project root within server folder
|
|
const openapiPath = path.resolve(process.cwd(), "openapi.json");
|
|
const openapiJson = JSON.parse(fs.readFileSync(openapiPath, "utf-8"));
|
|
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(openapiJson));
|
|
indexLogger.info("Swagger UI available at /api-docs");
|
|
} catch (e) {
|
|
indexLogger.error("Failed to load OpenAPI document for Swagger UI", e);
|
|
}
|
|
app.use("/api/v1/health", verifyToken, addUserContext, (req, res) => {
|
|
res.json({
|
|
status: "OK",
|
|
});
|
|
});
|
|
|
|
const port = 52345;
|
|
app.listen(port, "0.0.0.0", () => {
|
|
indexLogger.info(`Server is running on http://localhost:${port}`);
|
|
indexLogger.error(new ApiError("Test error logging", 500));
|
|
indexLogger.warn("test", {
|
|
testKey: "testValue",
|
|
});
|
|
});
|
|
};
|
|
|
|
createApp();
|