-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
99 lines (89 loc) · 2.58 KB
/
app.js
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
import { performance } from "node:perf_hooks";
import express from "express";
import rateLimit from "express-rate-limit";
import cors from "cors";
import multer from "multer";
import getMe from "./get-me.js";
import getStatus from "./get-status.js";
import getStats from "./get-stats.js";
import search from "./search.js";
import scan from "./scan.js";
import video from "./video.js";
import image from "./image.js";
import github from "./webhook/github.js";
import patreon from "./webhook/patreon.js";
import create from "./user/create.js";
import login from "./user/login.js";
import resetKey from "./user/reset-key.js";
import resetPassword from "./user/reset-password.js";
import rss from "./rss.js";
const app = express();
app.disable("x-powered-by");
app.set("trust proxy", 2);
app.use((req, res, next) => {
res.set("Access-Control-Allow-Origin", "*");
res.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.set("Access-Control-Allow-Headers", "Content-Type, x-trace-secret");
next();
});
app.use(
rateLimit({
max: 100, // limit each IP to max requests per 60 seconds
delayMs: 0, // disable delaying - full speed until the max limit is reached
}),
);
app.use((req, res, next) => {
const startTime = performance.now();
console.log("=>", new Date().toISOString(), req.ip, req.path);
res.on("finish", () => {
console.log(
"<=",
new Date().toISOString(),
req.ip,
req.path,
res.statusCode,
`${(performance.now() - startTime).toFixed(0)}ms`,
);
});
next();
});
app.use(cors({ credentials: true, origin: true }));
app.use(
express.raw({
type: ["application/octet-stream", "application/x-www-form-urlencoded", "image/*", "video/*"],
limit: 25 * 1024 * 1024,
verify: (req, res, buf) => {
req.rawBody = buf;
},
}),
);
app.use(express.urlencoded({ extended: false }));
app.use(
express.json({
verify: (req, res, buf) => {
req.rawBody = buf;
},
}),
);
app.get("/me", getMe);
app.get("/status", getStatus);
app.get("/stats", getStats);
app.get("/scan", scan);
app.all("/webhook/github", github);
app.all("/webhook/patreon", patreon);
app.all(
"/search",
multer({ storage: multer.memoryStorage(), limits: { fileSize: 25 * 1024 * 1024 } }).any(),
search,
);
app.get("/video/:anilistID/:filename", video);
app.get("/image/:anilistID/:filename", image);
app.all("/user/login", login);
app.all("/user/create", create);
app.all("/user/reset-key", resetKey);
app.all("/user/reset-password", resetPassword);
app.all("/rss.xml", rss);
app.all("/", async (req, res) => {
res.send("ok");
});
export default app;