forked from anuraghazra/github-readme-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
163 lines (141 loc) · 4.26 KB
/
server.js
File metadata and controls
163 lines (141 loc) · 4.26 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// @ts-check
import "dotenv/config";
import express from "express";
import compression from "compression";
import statsCard from "./api/index.js";
import repoCard from "./api/pin.js";
import langCard from "./api/top-langs.js";
import wakatimeCard from "./api/wakatime.js";
import gistCard from "./api/gist.js";
import { getRedisClient, isRedisHealthy, closeRedis } from "./src/common/redis.js";
const app = express();
// Enable gzip compression
app.use(compression());
// Request logging middleware
app.use((req, res, next) => {
const start = Date.now();
res.on("finish", () => {
const duration = Date.now() - start;
console.log(`${req.method} ${req.url} ${res.statusCode} - ${duration}ms`);
});
next();
});
// CORS headers for broader compatibility
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
if (req.method === "OPTIONS") {
return res.sendStatus(200);
}
next();
});
// Health check endpoints for Kubernetes probes
app.get("/health", (req, res) => {
res.json({
status: "ok",
timestamp: new Date().toISOString(),
});
});
app.get("/healthz", (req, res) => {
res.json({
status: "ok",
timestamp: new Date().toISOString(),
});
});
// Readiness probe - checks Redis connectivity if configured
// Always returns ready (200) - Redis is optional for caching
app.get("/ready", async (req, res) => {
let redisStatus = "not configured";
if (process.env.REDIS_HOST) {
try {
// Timeout isRedisHealthy to prevent probe from hanging
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error("timeout")), 2000)
);
const healthyPromise = isRedisHealthy();
const redisHealthy = await Promise.race([healthyPromise, timeoutPromise]).catch(() => false);
redisStatus = redisHealthy ? "connected" : "disconnected";
} catch {
redisStatus = "disconnected";
}
}
res.json({
status: "ready",
timestamp: new Date().toISOString(),
redis: redisStatus,
});
});
app.get("/readyz", async (req, res) => {
try {
const redisHealthy = await isRedisHealthy();
res.json({
status: "ready",
redis: redisHealthy ? "ok" : "unavailable",
});
} catch (err) {
res.status(503).json({
status: "not ready",
error: err.message,
});
}
});
// API routes
const router = express.Router();
router.get("/", statsCard);
router.get("/pin", repoCard);
router.get("/top-langs", langCard);
router.get("/wakatime", wakatimeCard);
router.get("/gist", gistCard);
app.use("/api", router);
// Also serve at root for compatibility
app.get("/", (req, res) => {
// If there are query params, treat as stats card request
if (Object.keys(req.query).length > 0) {
return statsCard(req, res);
}
// Otherwise return info
res.json({
name: "github-readme-stats",
version: "1.0.0",
endpoints: [
"/api - Stats card",
"/api/pin - Repository pin card",
"/api/top-langs - Top languages card",
"/api/wakatime - WakaTime card",
"/api/gist - Gist card",
"/health - Health check",
"/ready - Readiness check",
],
});
});
// Start server
const port = process.env.PORT || process.env.port || 9000;
const server = app.listen(port, "0.0.0.0", async () => {
console.log(`github-readme-stats server running on port ${port}`);
// Initialize Redis connection if configured
if (process.env.REDIS_HOST) {
console.log(`Connecting to Redis at ${process.env.REDIS_HOST}:${process.env.REDIS_PORT || 6379}`);
await getRedisClient();
}
});
// Graceful shutdown handler
const shutdown = async (signal) => {
console.log(`\n${signal} received, shutting down gracefully...`);
// Close server first (stop accepting new requests)
server.close(async () => {
console.log("HTTP server closed");
// Close Redis connection
await closeRedis();
console.log("Shutdown complete");
process.exit(0);
});
// Force exit after 10 seconds
setTimeout(() => {
console.error("Forced shutdown after timeout");
process.exit(1);
}, 10000);
};
process.on("SIGTERM", () => shutdown("SIGTERM"));
process.on("SIGINT", () => shutdown("SIGINT"));
export default app;