jws-backend/src/app.ts
2025-02-20 10:44:28 +07:00

64 lines
1.6 KiB
TypeScript

import "dotenv/config";
import cors from "cors";
import express, { json, urlencoded } from "express";
import error from "./middlewares/error";
import morgan from "./middlewares/morgan";
import { RegisterRoutes } from "./routes";
import { initEmploymentOffice, initThailandAreaDatabase } from "./utils/thailand-area";
import { initFirstAdmin } from "./utils/database";
import { initSchedule } from "./services/schedule";
const APP_HOST = process.env.APP_HOST || "0.0.0.0";
const APP_PORT = +(process.env.APP_PORT || 3000);
(async () => {
const app = express();
await initThailandAreaDatabase();
await initFirstAdmin();
await initEmploymentOffice();
initSchedule();
const originalSend = app.response.json;
app.response.json = function (body: unknown) {
return originalSend.call(this, (this.app.locals.response = body));
};
app.use(cors());
app.use(json());
app.use(urlencoded({ extended: true }));
app.use(morgan);
app.use("/", express.static("static"));
app.use(
"/api-docs",
await import("@scalar/express-api-reference").then(({ apiReference }) =>
apiReference({
theme: "kepler",
layout: "classic",
hideModels: true,
hideClientButton: true,
customCss: `.endpoint-label-name { display: none }`,
spec: { url: "/api/openapi" },
}),
),
);
RegisterRoutes(app);
app.get("*", (_, res) =>
res.status(404).send({
status: 404,
message: "Route not found.",
devMessage: "unknown_url",
}),
);
app.use(error);
app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`));
})();