2026-06-01 21:44:29 +07:00
|
|
|
import { startHttpServer } from "./http/server.js";
|
2026-06-02 21:06:42 +07:00
|
|
|
import { createChildLogger } from "@bete/shared/logger";
|
2026-06-02 00:11:29 +07:00
|
|
|
import type { Server } from "node:http";
|
2026-06-01 21:44:29 +07:00
|
|
|
|
|
|
|
|
const logger = createChildLogger("backend");
|
|
|
|
|
|
2026-06-02 00:11:29 +07:00
|
|
|
let httpServer: Server | undefined;
|
|
|
|
|
|
2026-06-01 21:44:29 +07:00
|
|
|
async function main() {
|
|
|
|
|
try {
|
|
|
|
|
logger.info("Starting Discord Moderation Backend Service");
|
2026-06-02 00:11:29 +07:00
|
|
|
httpServer = await startHttpServer();
|
2026-06-01 21:44:29 +07:00
|
|
|
logger.info("Backend service ready");
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error({ err }, "Failed to start backend service");
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 00:11:29 +07:00
|
|
|
function shutdown(signal: string) {
|
|
|
|
|
logger.info({ signal }, "Shutting down gracefully");
|
2026-06-01 21:44:29 +07:00
|
|
|
|
2026-06-02 00:11:29 +07:00
|
|
|
if (httpServer) {
|
|
|
|
|
httpServer.close(() => {
|
|
|
|
|
logger.info("HTTP server closed");
|
|
|
|
|
process.exit(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Force exit after 10s if connections don't close
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
logger.error("Forced shutdown after timeout");
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}, 10_000).unref();
|
|
|
|
|
} else {
|
|
|
|
|
process.exit(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process.on("SIGINT", () => shutdown("SIGINT"));
|
|
|
|
|
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
2026-06-01 21:44:29 +07:00
|
|
|
|
|
|
|
|
process.on("uncaughtException", (err) => {
|
|
|
|
|
logger.error({ err }, "Uncaught exception");
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
process.on("unhandledRejection", (reason) => {
|
|
|
|
|
logger.error({ reason }, "Unhandled rejection");
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
main();
|