Files
shop-api/src/server.ts
T
morteza-mortezai 485f3efdba gateway
2025-11-30 08:58:29 +03:30

61 lines
1.9 KiB
TypeScript

import "reflect-metadata";
import { createServer } from "http";
import { config } from "dotenv";
import { Container } from "inversify";
config();
import { App } from "./app";
import { Logger } from "./core/logging/logger";
import { connectMongo } from "./db/connection";
import { IOCTYPES } from "./IOC/ioc.types";
import { ChatGateway } from "./modules/chat/chat.gateway";
import { ChatbotGateway } from "./modules/chatbot/chatbot.gateway";
import { StartWorker } from "./queues";
import { gracefulShutdown } from "./utils/shutdown.utils";
const PORT = parseInt(process.env.PORT) || 4000;
const logger = new Logger();
async function bootStrap() {
try {
await connectMongo();
logger.info("mongodb connected");
//
const container = new Container();
const app = new App(container).Build();
const server = createServer(app);
//
const chatGateway = container.get<ChatGateway>(IOCTYPES.ChatGateway);
const wsServer = chatGateway.initialize(server);
const chatbotGateway = container.get<ChatbotGateway>(IOCTYPES.ChatbotGateway);
const chatbotWsServer = chatbotGateway.initialize(server);
const worker = await StartWorker();
//
server.listen(PORT, "0.0.0.0", () => {
logger.info(`listening on http://localhost:${PORT}`);
logger.info(`swagger documentation is serving on http://localhost:${PORT}/api-docs`);
});
process.on("SIGINT", () => gracefulShutdown("SIGINT", server, wsServer, worker[0], logger, chatbotWsServer));
process.on("SIGTERM", () => gracefulShutdown("SIGTERM", server, wsServer, worker[0], logger, chatbotWsServer));
} catch (error) {
logger.error("Error starting the server.", error);
process.exit(1);
}
}
bootStrap();
process.on("uncaughtException", function (err) {
logger.error("Uncaught exception", err);
});
process.on("unhandledRejection", (reason, promise) => {
logger.error("Unhandled Rejection at: Promise", { promise, reason });
});