forked from cinepro-org/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (69 loc) · 2.93 KB
/
index.js
File metadata and controls
84 lines (69 loc) · 2.93 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
import express from "express";
import {scrapeMedia} from "./src/api.js";
import {getMovieFromTmdb, getTvFromTmdb} from "./src/helpers/tmdb.js";
import cors from "cors";
import {strings} from "./src/strings.js";
import {checkIfPossibleTmdbId, handleErrorResponse} from "./src/helpers/helper.js";
import {ErrorObject} from "./src/helpers/ErrorObject.js";
const PORT = process.env.PORT;
const allowedOrigins = ["https://cinepro.mintlify.app"]; // localhost is also allowed. (from any localhost port)
const app = express();
app.use(cors({
origin: (origin, callback) => {
(!origin || allowedOrigins.includes(origin) || /^http:\/\/localhost/.test(origin)) ? callback(null, true) : callback(new Error("Not allowed by CORS"))
}
}));
app.get("/", (req, res) => {
res.status(200).json({
home: strings.HOME_NAME,
routes: strings.ROUTES,
information: strings.INFORMATION,
license: strings.LICENSE,
source: strings.SOURCE
});
});
app.get("/movie/:tmdbId", async (req, res) => {
if (!checkIfPossibleTmdbId(req.params.tmdbId)) {
return handleErrorResponse(res, new ErrorObject(strings.INVALID_MOVIE_ID, "user", 405, strings.INVALID_MOVIE_ID_HINT, true, false));
}
const media = await getMovieFromTmdb(req.params.tmdbId);
if (media instanceof ErrorObject) {
return handleErrorResponse(res, media);
}
const output = await scrapeMedia(media);
if (output instanceof ErrorObject) {
return handleErrorResponse(res, output);
}
res.status(200).json(output);
});
app.get("/tv/:tmdbId", async (req, res) => {
if (!checkIfPossibleTmdbId(req.params.tmdbId) || !checkIfPossibleTmdbId(req.query.s) || !checkIfPossibleTmdbId(req.query.e)) {
return handleErrorResponse(res, new ErrorObject(strings.INVALID_TV_ID, "user", 405, strings.INVALID_TV_ID_HINT, true, false));
}
const media = await getTvFromTmdb(req.params.tmdbId, req.query.s, req.query.e);
if (media instanceof ErrorObject) {
return handleErrorResponse(res, media);
}
const output = await scrapeMedia(media);
if (output instanceof ErrorObject) {
return handleErrorResponse(res, output);
}
res.status(200).json(output);
});
app.get("/movie/", (req, res) => {
handleErrorResponse(res, new ErrorObject(strings.INVALID_MOVIE_ID, "user", 405, strings.INVALID_MOVIE_ID_HINT, true, false));
});
app.get("/tv/", (req, res) => {
handleErrorResponse(res, new ErrorObject(strings.INVALID_TV_ID, "user", 405, strings.INVALID_TV_ID_HINT, true, false));
});
app.get("*", (req, res) => {
handleErrorResponse(res, new ErrorObject(strings.ROUTE_NOT_FOUND, "user", 404, strings.ROUTE_NOT_FOUND_HINT, true, false));
});
app.listen(PORT, () => {
console.log(`Server is running on port http://localhost:${PORT};`);
if (process.argv.includes("--debug")) {
console.log(`Debug mode is enabled.`);
} else {
console.log("Debug mode is disabled.");
}
});