-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (53 loc) · 1.63 KB
/
Copy pathserver.js
File metadata and controls
64 lines (53 loc) · 1.63 KB
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
const dotenv = require("dotenv");
dotenv.config();
const bodyParser = require("body-parser");
const cors = require("cors");
const helmet = require("helmet");
const db = require("./config/db-config.js");
const { initial } = require("./factory.js");
const { swaggerUi, swaggerSpecs } = require("./config/swaggerConfig.js");
const { app, server } = require("./socketManager");
const { logger } = require("./config/winston.js");
// CORS configuration
// Middleware
app.use(
cors({
origin: new RegExp(
`^(http:\/\/|https:\/\/|http:\/\/localhost:3000|https:\/\/localhost:3000|http:\/\/${process.env.LOCAL_ALLOWED_ORIGIN}|https:\/\/${process.env.PRODUCTION_ALLOWED_ORIGIN}|http:\/\/${process.env.ANDROID_ALLOWED_ORIGIN}|http:\/\/${process.env.ANDROID_ALLOWED_ORIGIN})`
),
})
);
app.use(bodyParser.json());
app.use(helmet());
// Routes
const router = require("./routers/router.js");
app.use("/", router);
// test liara haha
// Swagger UI
app.use(
"/api-docs",
swaggerUi.serve,
swaggerUi.setup(swaggerSpecs, { explorer: true })
);
// Database synchronization
db.sequelize
.sync({ force: true })
.then(() => {
console.log("Database synchronized");
initial();
})
.catch((error) => {
console.log("Database synchronization failed:", error);
});
// Server listening
const serverInstance = server.listen(parseInt(process.env.PORT), () => {
console.log(`App listening on port ${parseInt(process.env.PORT)}`);
});
// Graceful shutdown
process.on("SIGINT", () => {
console.log("Received SIGINT. Closing server gracefully.");
serverInstance.close(() => {
console.log("Server closed.");
process.exit(0);
});
});