jws-backend/src/app.ts
2024-11-07 13:44:49 +07:00

51 lines
1.4 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 { initThailandAreaDatabase } from "./utils/thailand-area";
import { initFirstAdmin } from "./utils/database";
import { apiReference } from "@scalar/express-api-reference";
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();
initSchedule();
const originalSend = app.response.json;
app.response.json = function (body: unknown) {
this.app.locals.response = body;
return originalSend.call(this, body);
};
app.use(cors());
app.use(json());
app.use(urlencoded({ extended: true }));
app.use(morgan);
app.use("/", express.static("static"));
app.use("/api-docs", apiReference({ theme: "kepler", 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}`));
})();