-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
79 lines (64 loc) · 1.97 KB
/
server.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
import express from "express";
import bodyParser from "body-parser";
import { config as dotenvConfig } from "dotenv";
dotenvConfig();
import { session } from "./middleware/index.js";
import { logging } from 'diegos-fly-logger/index.mjs';
import {
searchMovie,
poster,
letterboxdWatchlist,
letterboxdCustomList,
alternativeSearch,
proxy,
} from "./controllers/index.js";
import { isHealthy } from "./helpers/redis.js";
const app = express();
const port = process.env.PORT || 3000;
app.use(express.static("public/dist")); // serve static files that vite built
// anonymous session
app.use(session);
// logging
app.use(logging);
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.get("/healthcheck", (req, res) => {
res.status(200).send("OK");
});
// redis healthcheck endpoint
app.get("/redis-healthcheck", async (req, res) => {
if (await isHealthy()) {
res.status(200).send("OK");
} else {
res.status(500).send("Redis is not healthy");
}
});
// Middleware to set cache control header
const setCacheControl = (req, res, next) => {
// let browsers cache response for 1h
res.setHeader("Cache-Control", "public, max-age=3600");
next();
};
app.post("/api/search-movie", setCacheControl, async (req, res) => {
return searchMovie(req, res);
});
app.post("/api/poster", setCacheControl, async (req, res) => {
return poster(req, res);
});
app.post("/api/letterboxd-watchlist", setCacheControl, async (req, res) => {
return letterboxdWatchlist(req, res);
});
app.post("/api/letterboxd-custom-list", setCacheControl, async (req, res) => {
return letterboxdCustomList(req, res);
});
app.post("/api/alternative-search", setCacheControl, async (req, res) => {
return alternativeSearch(req, res);
});
app.all("/api/proxy/:url(*)", async (req, res) => {
return proxy(req, res);
});
app.listen(port, () =>
console.log(`app listening on port http://localhost:${port}`)
);