jws-backend/src/app.ts

52 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-04-01 13:28:43 +07:00
import "dotenv/config";
import cors from "cors";
import express, { json, urlencoded } from "express";
import error from "./middlewares/error";
2024-07-26 17:35:09 +07:00
import morgan from "./middlewares/morgan";
2024-04-01 13:28:43 +07:00
import { RegisterRoutes } from "./routes";
import { initThailandAreaDatabase } from "./utils/thailand-area";
2024-10-24 11:42:16 +07:00
import { initFirstAdmin } from "./utils/database";
2024-10-24 11:50:55 +07:00
import { apiReference } from "@scalar/express-api-reference";
import { initSchedule } from "./services/schedule";
2024-04-01 13:28:43 +07:00
2024-04-01 13:36:06 +07:00
const APP_HOST = process.env.APP_HOST || "0.0.0.0";
const APP_PORT = +(process.env.APP_PORT || 3000);
2024-04-01 13:28:43 +07:00
(async () => {
const app = express();
await initThailandAreaDatabase();
2024-10-24 11:42:16 +07:00
await initFirstAdmin();
2024-07-01 13:24:02 +07:00
initSchedule();
2024-07-26 17:35:09 +07:00
const originalSend = app.response.json;
app.response.json = function (body: unknown) {
this.app.locals.response = body;
return originalSend.call(this, body);
};
2024-04-01 13:28:43 +07:00
app.use(cors());
app.use(json());
app.use(urlencoded({ extended: true }));
2024-07-26 17:35:09 +07:00
app.use(morgan);
2024-04-03 16:12:41 +07:00
2024-04-01 13:28:43 +07:00
app.use("/", express.static("static"));
2024-10-24 11:50:55 +07:00
app.use("/api-docs", apiReference({ theme: "kepler", spec: { url: "/api/openapi" } }));
2024-04-01 13:28:43 +07:00
RegisterRoutes(app);
app.get("*", (_, res) =>
res.status(404).send({
status: 404,
message: "Route not found.",
devMessage: "unknown_url",
}),
);
2024-04-01 13:28:43 +07:00
app.use(error);
app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`));
})();