-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
96 lines (77 loc) · 2.76 KB
/
Copy pathserver.ts
File metadata and controls
96 lines (77 loc) · 2.76 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
88
89
90
91
92
93
94
95
96
import "dotenv/config";
import { createRequestHandler } from "@react-router/express";
import compression from "compression";
import express from "express";
import morgan from "morgan";
import path from "node:path";
import url from "node:url";
const BUILD_PATH = path.resolve("build/server/index.js");
const buildModule = await import(url.pathToFileURL(BUILD_PATH).href);
const app = express();
// Trust the first proxy (Traefik) so req.protocol reflects X-Forwarded-Proto
// and req.ip reflects X-Forwarded-For. This is critical for secure cookie
// handling when TLS is terminated at the reverse proxy.
app.set("trust proxy", 1);
app.disable("x-powered-by");
let isShuttingDown = false;
// Kubernetes readiness/liveness probe — before all middleware so it's fast and
// never blocked by compression, static-file serving, or request logging.
app.get("/healthz", (_req, res) => {
if (isShuttingDown) {
res.status(503).send("shutting down");
} else {
res.status(200).send("ok");
}
});
app.use(compression());
// Vite-fingerprinted assets — immutable, cache forever
app.use(
path.posix.join(buildModule.publicPath, "assets"),
express.static(path.join(buildModule.assetsBuildDirectory, "assets"), {
immutable: true,
maxAge: "1y",
}),
);
// Other build assets — short cache
app.use(buildModule.publicPath, express.static(buildModule.assetsBuildDirectory, { maxAge: "1h" }));
// Public static files (fonts, logos, etc.)
app.use(express.static("public", { maxAge: "1h" }));
app.use(morgan("tiny"));
app.all(
"*",
createRequestHandler({
build: buildModule,
mode: process.env.NODE_ENV,
}),
);
const port = Number.parseInt(process.env.PORT || "3000", 10);
const host = process.env.HOST || "0.0.0.0";
const server = app.listen(port, host, () => {
console.log(`[cytario-web] http://${host}:${port}`);
});
// Graceful shutdown on SIGTERM/SIGINT
const SHUTDOWN_DELAY_MS = 5_000;
const DRAIN_TIMEOUT_MS = 15_000;
for (const signal of ["SIGTERM", "SIGINT"]) {
process.once(signal, () => {
console.log(`[cytario-web] ${signal} received, shutting down gracefully`);
isShuttingDown = true;
// Wait for load balancer to deregister the pod before closing connections
setTimeout(() => {
console.log("[cytario-web] closing server to new connections");
server.close((err) => {
if (err) {
console.error("[cytario-web] error during close:", err);
} else {
console.log("[cytario-web] all connections drained, exiting");
}
process.exit(err ? 1 : 0);
});
// Force exit if connections don't drain in time
setTimeout(() => {
console.error("[cytario-web] drain timeout, forcing exit");
process.exit(1);
}, DRAIN_TIMEOUT_MS).unref();
}, SHUTDOWN_DELAY_MS);
});
}