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"; import * as cron from "node-cron"; import { init as rabbitmqInit } from "./services/rabbitmq"; import error from "./middlewares/error"; import { AppDataSource } from "./database/data-source"; import { RegisterRoutes } from "./routes"; import { OrganizationController } from "./controllers/OrganizationController"; import logMiddleware from "./middlewares/logs"; import { CommandController } from "./controllers/CommandController"; // import { WebSocketServer } from "ws"; // import http from "http"; // export const wss = new WebSocketServer({ noServer: true, // path: "/api/v1/org/socket", // }); async function main() { await AppDataSource.initialize(); const app = express(); // สร้างเซิร์ฟเวอร์ HTTP // const server = http.createServer(app); app.use( cors({ origin: "*", }), ); app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use(logMiddleware); 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); const cronTime = "0 8 * * * *"; // ตั้งเวลาทุกวันเวลา 08:00:00 // const cronTime = "*/10 * * * * *"; cron.schedule(cronTime, async () => { try { const orgController = new OrganizationController(); await orgController.cronjobRevision(); } catch (error) { console.error("Error executing function from controller:", error); } }); 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); } }); // app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`)); 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`) ), ); async function runMessageQueue() { try { await rabbitmqInit(); } catch (e) { console.log(e); setTimeout(runMessageQueue, 1000); } } runMessageQueue(); // การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket // 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); // }); // }); // // ตั้งค่า Express routes // app.get('/', (req, res) => { // res.send('Hello from Express!'); // }); // 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"); // }); } main();