forked from MTES-MCT/ecobalyse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
136 lines (116 loc) · 4.06 KB
/
server.js
File metadata and controls
136 lines (116 loc) · 4.06 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
require("dotenv").config();
const fs = require("fs");
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const yaml = require("js-yaml");
const helmet = require("helmet");
const Sentry = require("@sentry/node");
const { Elm } = require("./server-app");
const lib = require("./lib");
const app = express(); // web app
const api = express(); // api app
const host = "0.0.0.0";
const port = process.env.PORT || 3000;
// Env vars
const { SENTRY_DSN, MATOMO_HOST, MATOMO_SITE_ID, MATOMO_TOKEN } = process.env;
// Matomo
if (process.env.NODE_ENV !== "test" && (!MATOMO_HOST || !MATOMO_SITE_ID || !MATOMO_TOKEN)) {
console.error("Matomo environment variables are missing. Please check the README.");
process.exit(1);
}
// Sentry
if (SENTRY_DSN) {
Sentry.init({ dsn: SENTRY_DSN, tracesSampleRate: 0.1 });
// Note: Sentry middleware *must* be the very first applied to be effective
app.use(Sentry.Handlers.requestHandler());
}
// Web
// Note: helmet middlewares have to be called *after* the Sentry middleware
// but *before* other middlewares to be applied effectively
app.use(
helmet({
crossOriginEmbedderPolicy: false,
hsts: false,
xssFilter: false,
contentSecurityPolicy: {
useDefaults: true,
directives: {
"default-src": [
"'self'",
"https://api.github.com",
"https://raw.githubusercontent.com",
"https://sentry.incubateur.net",
"*.gouv.fr",
],
"frame-src": ["'self'", `https://${process.env.MATOMO_HOST}`, "https://www.loom.com"],
"img-src": [
"'self'",
"data:",
"blob:",
"https://avatars.githubusercontent.com/",
"https://raw.githubusercontent.com",
],
// FIXME: We should be able to remove 'unsafe-inline' as soon as the Matomo
// server sends the appropriate `Access-Control-Allow-Origin` header
// @see https://matomo.org/faq/how-to/faq_18694/
"script-src": ["'self'", "'unsafe-inline'", `https://${process.env.MATOMO_HOST}`],
"object-src": ["blob:"],
},
},
}),
);
app.use(
express.static("dist", {
setHeaders: (res) => {
// Note: helmet sets this header to `0` by default and doesn't allow overriding
// this value
res.set("X-XSS-Protection", "1; mode=block");
},
}),
);
// Redirects: Web
app.get("/accessibilite", (_, res) => res.redirect("/#/pages/accessibilité"));
app.get("/mentions-legales", (_, res) => res.redirect("/#/pages/mentions-légales"));
app.get("/stats", (_, res) => res.redirect("/#/stats"));
// API
const openApiContents = yaml.load(fs.readFileSync("openapi.yaml"));
// Matomo
const apiTracker = lib.setupTracker(openApiContents);
const elmApp = Elm.Server.init();
elmApp.ports.output.subscribe(({ status, body, jsResponseHandler }) => {
return jsResponseHandler({ status, body });
});
api.get("/", (req, res) => {
apiTracker.track(200, req);
res.status(200).send(openApiContents);
});
// Redirects: API
api.get(/^\/countries$/, (_, res) => res.redirect("textile/countries"));
api.get(/^\/materials$/, (_, res) => res.redirect("textile/materials"));
api.get(/^\/products$/, (_, res) => res.redirect("textile/products"));
const cleanRedirect = (url) => (url.startsWith("/") ? url : "");
api.get(/^\/simulator(.*)$/, ({ url }, res) => res.redirect(`/api/textile${cleanRedirect(url)}`));
// Note: Text/JSON request body parser (JSON is decoded in Elm)
api.all(/(.*)/, bodyParser.json(), (req, res) => {
elmApp.ports.input.send({
method: req.method,
url: req.url,
body: req.body,
jsResponseHandler: ({ status, body }) => {
apiTracker.track(status, req);
res.status(status).send(body);
},
});
});
api.use(cors()); // Enable CORS for all API requests
app.use("/api", api);
// Sentry error handler
// Note: *must* be called *before* any other error handler
if (SENTRY_DSN) {
app.use(Sentry.Handlers.errorHandler());
}
const server = app.listen(port, host, () => {
console.log(`Server listening at http://${host}:${port}`);
});
module.exports = server;