-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmain.ts
136 lines (108 loc) · 3.47 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import "./openapi/zod-extend";
import { createServer } from "node:http";
import path from "node:path";
import process from "node:process";
import { createBullBoard } from "@bull-board/api";
import { BullMQAdapter } from "@bull-board/api/bullMQAdapter";
import { ExpressAdapter } from "@bull-board/express";
import compression from "compression";
import cookieParser from "cookie-parser";
import cors from "cors";
import express from "express";
import session from "express-session";
import helmet from "helmet";
import morgan from "morgan";
import config from "./config/config.service";
import { connectDatabase, disconnectDatabase } from "./lib/database";
import logger, { httpLogger } from "./lib/logger.service";
import { useSocketIo } from "./lib/realtime.server";
import redisStore from "./lib/session.store";
import { extractJwt } from "./middlewares/extract-jwt-schema.middleware";
import apiRoutes from "./routes/routes";
import swaggerUi from "swagger-ui-express";
import YAML from "yaml";
import { convertDocumentationToYaml } from "./openapi/swagger-doc-generator";
import globalErrorHandler from "./utils/globalErrorHandler";
const app = express();
app.set("trust proxy", true);
const server = createServer(app);
const io = useSocketIo(server);
const boostrapServer = async () => {
await connectDatabase();
app.use((req, _, next) => {
req.io = io;
next();
});
app.use(
cors({
origin: [config.CLIENT_SIDE_URL],
optionsSuccessStatus: 200,
credentials: true,
}),
);
if (config.NODE_ENV === "development") {
app.use(morgan("dev"));
} else {
app.use(httpLogger);
}
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(
session({
secret: config.JWT_SECRET,
resave: false,
saveUninitialized: true,
cookie: { secure: true },
store: redisStore,
}),
);
// Middleware to serve static files
app.use(express.static(path.join(__dirname, "..", "public")));
app.use(cookieParser());
app.use(compression());
app.use(extractJwt);
if (config.NODE_ENV === "production") {
app.use(helmet());
}
app.use("/api", apiRoutes);
const swaggerDocument = YAML.parse(convertDocumentationToYaml());
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
const serverAdapter = new ExpressAdapter();
serverAdapter.setBasePath("/admin/queues");
createBullBoard({
queues: Object.entries(global.__registeredQueues || {}).map(
([, values]) => new BullMQAdapter(values.queue),
),
serverAdapter,
});
// Dashbaord for BullMQ
app.use("/admin/queues", serverAdapter.getRouter());
// Global Error Handler
app.use(globalErrorHandler);
server.listen(config.PORT, () => {
logger.info(`Server is running on http://localhost:${config.PORT}`);
logger.info(`RESTful API: http://localhost:${config.PORT}/api`);
logger.info(`Swagger API Docs: http://localhost:${config.PORT}/api-docs`);
logger.info(`BullBoard: http://localhost:${config.PORT}/admin/queues`);
logger.info(`Client-side url set to: ${config.CLIENT_SIDE_URL}`);
});
};
boostrapServer().catch((err) => {
logger.error(err.message);
process.exit(1);
});
for (const signal of ["SIGINT", "SIGTERM"]) {
process.on(signal, async () => {
await disconnectDatabase();
logger.info("Server is shutting down...");
io.disconnectSockets(true);
logger.info("Server disconnected from sockets");
server.close();
logger.info("Server closed");
process.exit(0);
});
}
process.on("uncaughtException", (err) => {
logger.error(err.message);
process.exit(1);
});