hrms-api-org/src/app.ts

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-01-24 11:39:00 +07:00
import "dotenv/config";
import "reflect-metadata";
import cors from "cors";
import express from "express";
import swaggerUi from "swagger-ui-express";
import swaggerDocument from "./swagger.json";
2024-02-07 15:45:13 +07:00
import * as cron from "node-cron";
2024-01-24 11:39:00 +07:00
import error from "./middlewares/error";
2024-01-25 10:52:44 +07:00
import { AppDataSource } from "./database/data-source";
2024-01-24 11:39:00 +07:00
import { RegisterRoutes } from "./routes";
2024-02-07 15:45:13 +07:00
import { OrganizationController } from "./controllers/OrganizationController";
2024-02-07 11:45:55 +07:00
2024-01-24 11:39:00 +07:00
async function main() {
2024-01-25 10:52:44 +07:00
await AppDataSource.initialize();
2024-01-24 11:39:00 +07:00
const app = express();
2024-01-26 16:05:18 +07:00
app.use(
cors({
origin: "*",
}),
);
2024-01-24 11:39:00 +07:00
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/", express.static("static"));
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
RegisterRoutes(app);
app.use(error);
const APP_HOST = process.env.APP_HOST || "0.0.0.0";
const APP_PORT = +(process.env.APP_PORT || 3000);
2024-02-07 15:45:13 +07:00
2024-02-08 09:49:35 +07:00
const cronTime = "0 8 * * * *"; // ตั้งเวลาทุกวันเวลา 08:00:00
2024-02-07 15:45:13 +07:00
cron.schedule(cronTime, async () => {
try {
const orgController = new OrganizationController();
await orgController.cronjobRevision();
} catch (error) {
console.error("Error executing function from controller:", error);
}
});
2024-01-24 11:39:00 +07:00
// app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`));
2024-01-26 16:05:18 +07:00
app.listen(
APP_PORT,
APP_HOST,
() => (
console.log(`[APP] Application is running on: http://localhost:${APP_PORT}`),
console.log(`[APP] Swagger on: http://localhost:${APP_PORT}/api-docs`)
),
);
2024-01-24 11:39:00 +07:00
}
main();