-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
61 lines (49 loc) · 1.55 KB
/
index.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
const express = require("express");
const rateLimit = require("express-rate-limit");
const cors = require("cors");
const mainRoute = require("./routes/main");
const scheduleRoute = require("./routes/schedule");
const episodeRoute = require("./routes/anify/episodes");
const sourceRoute = require("./routes/anify/sources");
const consumetEpisodeRoute = require("./routes/consumet/episode");
const consumetSourceRoute = require("./routes/consumet/source");
const PORT = process.env.PORT || 5000;
const whitelist = process.env.WHITELISTED_URL.includes("example")
? undefined
: process.env.WHITELISTED_URL;
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const limiter = rateLimit({
windowMs: 2 * 60 * 1000, // 2 minutes
max: 50, // limit each IP to 50 requests per windowMs
});
app.use(limiter);
app.set("trust proxy", 1);
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
};
app.use(cors());
app.use("/", mainRoute);
app.use("/api/schedules", scheduleRoute);
app.use("/anify/episode", cors(whitelist && corsOptions), episodeRoute);
app.use("/anify/source", cors(whitelist && corsOptions), sourceRoute);
app.use(
"/consumet/episode",
cors(whitelist && corsOptions),
consumetEpisodeRoute
);
app.use(
"/consumet/source",
cors(whitelist && corsOptions),
consumetSourceRoute
);
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});