hrms-api-org/src/app.ts

146 lines
4.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-10-16 11:55:45 +07:00
import { init as rabbitmqInit } from "./services/rabbitmq";
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";
import logMiddleware from "./middlewares/logs";
2024-10-11 11:05:31 +07:00
import { CommandController } from "./controllers/CommandController";
2025-03-06 13:00:02 +07:00
import { ProfileSalaryController } from "./controllers/ProfileSalaryController";
2025-03-06 15:04:13 +07:00
import { WebSocketServer } from "ws";
2025-03-05 18:25:11 +07:00
import http from "http";
2025-03-06 15:04:13 +07:00
export const wss = new WebSocketServer({ noServer: true,
path: "/api/v1/org/socket",
});
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();
2025-03-05 12:26:01 +07:00
2024-01-24 11:39:00 +07:00
const app = express();
2025-03-05 17:01:38 +07:00
// สร้างเซิร์ฟเวอร์ HTTP
2025-03-05 18:25:11 +07:00
const server = http.createServer(app);
2024-01-24 11:39:00 +07:00
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(logMiddleware);
2024-01-24 11:39:00 +07:00
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
// const cronTime = "*/10 * * * * *";
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
2024-10-11 11:05:31 +07:00
const cronTime_command = "0 2 * * * *";
// const cronTime_command = "*/10 * * * * *";
cron.schedule(cronTime_command, async () => {
try {
const commandController = new CommandController();
await commandController.cronjobCommand();
} catch (error) {
console.error("Error executing function from controller:", error);
}
});
const cronTime_Oct = "0 0 1 10 *";
cron.schedule(cronTime_Oct, async () => {
try {
const commandController = new CommandController();
await commandController.cronjobUpdateRetirementStatus();
} catch (error) {
console.error("Error executing function from controller:", error);
}
});
2025-03-05 11:52:58 +07:00
2025-03-06 13:00:02 +07:00
const cronTime_Tenure = "0 0 * * *";
cron.schedule(cronTime_Tenure, async () => {
try {
const profileSalaryController = new ProfileSalaryController();
await profileSalaryController.cronjobTenurePositionOfficer();
await profileSalaryController.cronjobTenureLevelOfficer();
await profileSalaryController.cronjobTenurePositionEmployee();
await profileSalaryController.cronjobTenureLevelEmployee();
} 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}`));
2025-03-05 18:25:11 +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-10-11 11:05:31 +07:00
async function runMessageQueue() {
try {
await rabbitmqInit();
} catch (e) {
console.log(e);
setTimeout(runMessageQueue, 1000);
}
}
2024-10-16 11:55:45 +07:00
runMessageQueue();
2025-03-04 18:05:56 +07:00
2025-03-05 17:01:38 +07:00
// การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket
2025-03-05 18:25:11 +07:00
server.on("upgrade", (request:any, socket:any, head:any) => {
console.log("🔹 Handling upgrade request...");
wss.handleUpgrade(request, socket, head, (ws:any) => {
console.log("🔹 WebSocket connection established");
wss.emit("connection", ws, request);
});
});
wss.on("connection", (ws:any) => {
console.log("✅ Client connected to WebSocket");
ws.on("close", () => {
console.log("❌ Client disconnected");
});
ws.on("error", (error:any) => {
console.error("WebSocket error:", error);
});
});
2025-03-06 14:08:22 +07:00
// ตั้งค่า Express routes
2025-03-05 18:25:11 +07:00
app.get('/', (req, res) => {
res.send('Hello from Express!');
});
2025-03-05 17:01:38 +07:00
2025-03-05 18:25:11 +07:00
server.listen(APP_PORT, APP_HOST, () => {
console.log(`[APP] Application is running on: http://${APP_HOST}:${APP_PORT}`);
console.log(`[APP] Swagger on: http://${APP_HOST}:${APP_PORT}/api-docs`);
console.log("[APP] HTTP Server is listening on current port");
});
2025-03-04 18:05:56 +07:00
2024-01-24 11:39:00 +07:00
}
main();