forked from Shineii86/AniKotoAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
289 lines (253 loc) · 12.9 KB
/
Copy pathserver.js
File metadata and controls
289 lines (253 loc) · 12.9 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* ======= • ======= • ======= • ======= • =======• =======
* AniKotoAPI — server.js
* Repository: https://github.com/Shineii86/AniKotoAPI
*
* @description
* Main entry point for the AniKotoAPI Express server.
* Configures CORS, middleware, static files, API routes,
* and 404 handling. Starts the server on the configured port.
*
* @exports
* None (side-effect: starts Express server)
*
* @author Shinei Nouzen
* @license MIT
* ======= • ======= • ======= • ======= • =======• =======
*/
// WARNING: This import must stay first. Static imports are evaluated before the
// module body runs, so a dotenv.config() call down there fires only after every
// helper has already been evaluated — and helpers that read process.env at their
// top level (mirror, cache, proxy, streamProxy) would silently keep their
// defaults. Importing dotenv/config runs config() as an import side effect, so
// .env is populated before any of those modules load.
import "dotenv/config";
import express from "express";
import compression from "compression";
import path from "path";
import fs from "fs";
import crypto from "crypto";
import { fileURLToPath } from "url";
import { dirname } from "path";
import { createApiRoutes } from "./src/routes/apiRoutes.js";
import { addCreatorInfo } from "./src/middleware/creatorInfo.js";
// ══════════════════════════════════════════════════════════════
// SERVER CONFIGURATION
// ══════════════════════════════════════════════════════════════
const app = express();
const PORT = process.env.PORT || 4444;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const publicDir = path.join(process.cwd(), "public");
const allowedOrigins = process.env.ALLOWED_ORIGINS?.split(",");
// ---- FEATURE: Response compression ----
app.use(compression({
filter: (req, res) => {
if (req.headers["x-no-compression"]) return false;
return compression.filter(req, res);
},
level: 6,
threshold: 1024
}));
// ---- FEATURE: Request body size limits ----
app.use(express.json({ limit: "10kb" }));
app.use(express.urlencoded({ extended: false, limit: "10kb" }));
// ══════════════════════════════════════════════════════════════
// REQUEST ID TRACKING
// ══════════════════════════════════════════════════════════════
// ---- FEATURE: Unique request ID for debugging ----
app.use((req, res, next) => {
req.id = crypto.randomUUID();
res.setHeader("X-Request-Id", req.id);
next();
});
// ══════════════════════════════════════════════════════════════
// CORS MIDDLEWARE
// ══════════════════════════════════════════════════════════════
// NOTE: Single unified CORS middleware — handles all origin validation
app.use((req, res, next) => {
const origin = req.headers.origin;
if (!allowedOrigins || allowedOrigins.includes("*") || (origin && allowedOrigins.includes(origin))) {
res.setHeader("Access-Control-Allow-Origin", origin || "*");
}
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, X-Requested-With");
if (req.method === "OPTIONS") {
return res.status(204).end();
}
next();
});
// ---- FEATURE: Security headers ----
app.use((req, res, next) => {
res.setHeader("X-Frame-Options", "DENY");
res.setHeader("X-Content-Type-Options", "nosniff");
res.setHeader("X-XSS-Protection", "1; mode=block");
res.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
res.setHeader("Permissions-Policy", "camera=(), microphone=(), geolocation=()");
res.setHeader("Content-Security-Policy", "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self'");
res.setHeader("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
res.setHeader("X-Permitted-Cross-Domain-Policies", "none");
next();
});
// ══════════════════════════════════════════════════════════════
// STATIC FILES
// ══════════════════════════════════════════════════════════════
// NOTE: redirect: false prevents automatic redirects to index.html
app.use(express.static(publicDir, { redirect: false }));
// ══════════════════════════════════════════════════════════════
// CLEAN URL ROUTES
// ══════════════════════════════════════════════════════════════
// Serve HTML files without .html extension
app.get("/tos", (req, res) => {
res.sendFile(path.join(publicDir, "tos.html"));
});
app.get("/privacy", (req, res) => {
res.sendFile(path.join(publicDir, "privacy.html"));
});
// ══════════════════════════════════════════════════════════════
// RESPONSE HELPERS
// ══════════════════════════════════════════════════════════════
// ---- FEATURE: Standardized JSON response wrapper ----
/**
* Wraps data in a standardized success JSON response.
*
* @param {object} res - Express response object
* @param {*} data - The data to return in the response
* @param {number} status - HTTP status code (default: 200)
*/
const jsonResponse = (res, data, status = 200) =>
res.status(status).json({ success: true, results: data });
// ---- FEATURE: Standardized error response wrapper ----
/**
* Returns a standardized error JSON response.
*
* @param {object} res - Express response object
* @param {string} message - Error message to return (default: "Internal server error")
* @param {number} status - HTTP status code (default: 500)
*/
const jsonError = (res, message = "Internal server error", status = 500) =>
res.status(status).json({ success: false, message });
// ══════════════════════════════════════════════════════════════
// API ROUTES
// ══════════════════════════════════════════════════════════════
// ---- FEATURE: Rate limiting (configurable, default 100 requests per minute per IP) ----
const requestCounts = new Map();
const RATE_LIMIT = parseInt(process.env.RATE_LIMIT) || 100;
const RATE_WINDOW = parseInt(process.env.RATE_WINDOW) || 60000;
// ---- FEATURE: Rate limiter cleanup interval (every 5 minutes) ----
const RATE_CLEANUP_INTERVAL = 300000;
setInterval(() => {
const now = Date.now();
for (const [ip, timestamps] of requestCounts.entries()) {
const validTimestamps = timestamps.filter(t => now - t < RATE_WINDOW);
if (validTimestamps.length === 0) {
requestCounts.delete(ip);
} else {
requestCounts.set(ip, validTimestamps);
}
}
}, RATE_CLEANUP_INTERVAL);
app.use((req, res, next) => {
const ip = req.ip || req.connection.remoteAddress;
const now = Date.now();
if (!requestCounts.has(ip)) {
requestCounts.set(ip, []);
}
const timestamps = requestCounts.get(ip).filter(t => now - t < RATE_WINDOW);
requestCounts.set(ip, timestamps);
if (timestamps.length >= RATE_LIMIT) {
return res.status(429).json({
success: false,
message: "Rate limit exceeded. Try again later.",
retryAfter: Math.ceil((timestamps[0] + RATE_WINDOW - now) / 1000)
});
}
timestamps.push(now);
res.setHeader("X-RateLimit-Limit", RATE_LIMIT);
res.setHeader("X-RateLimit-Remaining", RATE_LIMIT - timestamps.length);
next();
});
// ---- FEATURE: Creator info middleware (injects attribution into all responses) ----
app.use(addCreatorInfo);
// ---- FEATURE: Request timeout middleware (30 seconds) ----
app.use((req, res, next) => {
const timeout = parseInt(process.env.REQUEST_TIMEOUT) || 30000;
req.setTimeout(timeout, () => {
if (!res.headersSent) {
res.status(408).json({ success: false, message: "Request timeout" });
}
});
next();
});
createApiRoutes(app, jsonResponse, jsonError);
// ══════════════════════════════════════════════════════════════
// 404 HANDLER
// ══════════════════════════════════════════════════════════════
// ---- FEATURE: Global error handler ----
app.use((err, req, res, next) => {
console.error(`[ERROR] ${req.id} ${err.message}`, err.stack);
if (err.type === 'entity.too.large') {
return res.status(413).json({ success: false, message: "Request entity too large" });
}
res.status(err.status || 500).json({
success: false,
message: process.env.NODE_ENV === 'production' ? "Internal server error" : err.message
});
});
// ---- FEATURE: Catch-all 404 handler for undefined routes ----
app.use((req, res) => {
const filePath = path.join(publicDir, "404.html");
if (fs.existsSync(filePath)) {
res.status(404).sendFile(filePath);
} else {
res.status(404).json({
success: false,
message: "Endpoint not found",
availableEndpoints: [
"/", "/search", "/search/suggest", "/info", "/watch",
"/episodes/:id", "/episodes-ajax/:id", "/stream", "/servers",
"/mapper-servers", "/download", "/stream/resolve",
"/stream/qualities", "/stream/proxy", "/stream/ts-proxy",
"/spotlight", "/trending", "/top-ten", "/suggestions",
"/random", "/most-popular", "/upcoming", "/top-rankings",
"/recently-updated", "/completed", "/new-release",
"/newly-added", "/latest-updated", "/trending-sidebar",
"/seasons/:id", "/watch-order/:id", "/az-list/:letter",
"/filter", "/genre/:genre", "/type/:type", "/status/:status",
"/schedule", "/health", "/stats", "/cache/stats",
"/mirrors", "/openapi"
]
});
}
});
// ══════════════════════════════════════════════════════════════
// SERVER START
// ══════════════════════════════════════════════════════════════
const server = app.listen(PORT, () => {
console.info(`AniKotoAPI listening at ${PORT}`);
});
// ══════════════════════════════════════════════════════════════
// GRACEFUL SHUTDOWN
// ══════════════════════════════════════════════════════════════
// ---- FEATURE: Graceful shutdown handling ----
const gracefulShutdown = (signal) => {
console.info(`\n[SHUTDOWN] Received ${signal}. Starting graceful shutdown...`);
server.close(() => {
console.info("[SHUTDOWN] Server closed. Goodbye.");
process.exit(0);
});
setTimeout(() => {
console.error("[SHUTDOWN] Forced shutdown after timeout.");
process.exit(1);
}, 10000);
};
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
process.on("SIGINT", () => gracefulShutdown("SIGINT"));
process.on("unhandledRejection", (reason, promise) => {
console.error("[UNHANDLED] Unhandled Rejection:", reason);
});
process.on("uncaughtException", (error) => {
console.error("[UNCAUGHT] Uncaught Exception:", error);
gracefulShutdown("uncaughtException");
});
// ══════════════════════════════════════════════════════════════ END: server.js