-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
87 lines (76 loc) · 2.46 KB
/
app.js
File metadata and controls
87 lines (76 loc) · 2.46 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require('dotenv').config();
const express = require("express");
const cors = require("cors");
const swaggerUi = require("swagger-ui-express");
const swaggerDocument = require("./swagger/config.json");
const rateLimiter = require("./middleware/rateLimiter");
const app = express();
const PORT = process.env.PORT || 8800;
/** ============================
* Middleware
============================ **/
app.use(cors());
app.use(express.json());
app.use(rateLimiter);
/** ============================
* API Routes
============================ **/
app.use("/api/whole", require("./routes/all"));
app.use("/api/profile", require("./routes/profile"));
app.use("/api/ratings", require("./routes/ratings"));
app.use("/api/recent", require("./routes/recent"));
app.use("/api/upcoming", require("./routes/upcoming"));
/** ============================
* Swagger Documentation
============================ **/
app.use("/", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
/** ============================
* Health Check Endpoint
============================ **/
app.get("/health", async (_, res) => {
const health = { status: "OK", redis: "disconnected" };
try {
const cache = require("./utils/cache");
await cache.get("health_check");
health.redis = "connected";
} catch (e) {}
res.json(health);
});
/** ============================
* Error Handling Middleware
============================ **/
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({
success: false,
error: "Internal Server Error",
message: process.env.NODE_ENV === "production" ? "Something went wrong" : err.message
});
});
/** ============================
* 404 Not Found Handler
============================ **/
app.use((req, res) => {
res.status(404).json({
success: false,
error: "Not Found",
message: "The requested endpoint does not exist"
});
});
/** ============================
* Start Server & Graceful Shutdown
============================ **/
const server = app.listen(PORT, () =>
console.log(`🚀 Server running at http://localhost:${PORT}`)
);
const { closeBrowser } = require("./utils/playwright");
const gracefulShutdown = async (signal) => {
console.log(`${signal} signal received: closing HTTP server`);
await closeBrowser();
server.close(() => {
console.log('HTTP server closed');
process.exit(0);
});
};
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
process.on('SIGINT', () => gracefulShutdown('SIGINT'));