-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
77 lines (63 loc) · 2.23 KB
/
server.js
File metadata and controls
77 lines (63 loc) · 2.23 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
import "dotenv/config";
import express from "express";
import cors from "cors";
import config, { getGVisorStatus } from "./src/config/index.js";
import { requestLogger } from "./src/infrastructure/logs/requestLogger.js";
import { info, warn } from "./src/infrastructure/logs/logger.js";
import { errorHandler } from "./src/middleware/errorHandler.js";
import { configureRoutes } from "./src/api/routes/index.js";
import { startWorker } from "./src/core/workers/executorWorker.js";
import { redis, redisBlocking } from "./src/infrastructure/redis/redisClient.js";
const app = express();
// Middleware
// CORS: Allow any origin for authenticated requests
// Security is via API Key / JWT authentication, not origin restriction
const corsOptions = {
origin: true, // Reflect request origin (allows all)
methods: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization", "X-API-Key"],
credentials: true,
};
app.use(cors(corsOptions));
app.use(requestLogger);
app.use(express.json({ limit: "100kb" }));
// Routes
configureRoutes(app);
// Error Handler
app.use(errorHandler);
// Start Server
const server = app.listen(config.port, "0.0.0.0", () => {
info(`server started on port ${config.port}`);
// Log gVisor status
const gvisor = getGVisorStatus();
if (gvisor.available) {
info(`gVisor (runsc) runtime detected — sandbox hardening ENABLED (${gvisor.reason})`);
} else {
warn(`gVisor (runsc) not available — ${gvisor.reason}`);
}
// Start workers
for (let i = 1; i <= config.workerCount; i++) startWorker(i);
});
// Graceful shutdown
const gracefulShutdown = async (signal) => {
info(`${signal} received, shutting down gracefully`);
server.close(async () => {
info("HTTP server closed");
// Disconnect Redis clients
try {
await redis.quit();
await redisBlocking.quit();
info("Redis connections closed");
} catch {
// Ignore Redis disconnect errors during shutdown
}
process.exit(0);
});
// Force shutdown after 10 seconds
setTimeout(() => {
warn("Forced shutdown after timeout");
process.exit(1);
}, 10000);
};
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
process.on("SIGINT", () => gracefulShutdown("SIGINT"));