Files
Checkmate/server/src/index.ts
T

65 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-11-13 13:47:26 -08:00
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");
2025-11-21 23:44:20 +00:00
import swaggerUi from "swagger-ui-express";
import fs from "node:fs";
import path from "node:path";
2025-11-13 13:47:26 -08:00
const createApp = async () => {
await connectDatabase();
const services = await initServices();
const controllers = initControllers(services);
const app = express();
2025-12-02 17:44:44 +00:00
// 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()
);
2025-11-13 13:47:26 -08:00
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);
2025-11-21 23:44:20 +00:00
// 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);
}
2025-11-13 13:47:26 -08:00
app.use("/api/v1/health", verifyToken, addUserContext, (req, res) => {
res.json({
status: "OK",
});
});
const port = 52345;
2025-11-14 15:56:52 -08:00
app.listen(port, "0.0.0.0", () => {
2025-11-13 13:47:26 -08:00
indexLogger.info(`Server is running on http://localhost:${port}`);
indexLogger.error(new ApiError("Test error logging", 500));
2025-11-26 19:26:16 +00:00
indexLogger.warn("test", {
testKey: "testValue",
});
2025-11-13 13:47:26 -08:00
});
};
createApp();