-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
107 lines (104 loc) · 3.03 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
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
// load the things we need
var express = require("express");
var compression = require("compression");
var helmet = require("helmet");
var app = express();
var waterUtil = require("./public/javascript/backend/water_util.js");
app.set("view engine", "ejs");
app.use(compression());
// secure the app but allow scripts required for bootstrap
app.use(
helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
connectSrc: ["https://kiwis.innetag.ch"],
scriptSrc: [
"'self'",
"https://stackpath.bootstrapcdn.com",
"https://cdn.jsdelivr.net",
"https://cdnjs.cloudflare.com",
"https://code.jquery.com",
"https://unpkg.com"
],
styleSrc: [
"'self'",
"https://stackpath.bootstrapcdn.com",
"'unsafe-inline'"
],
imgSrc: [
"'self'",
"https://kiwis.innetag.ch",
"https://data.monitron.ch"
],
//allowed iframe users
frameAncestors: [
"'self'",
"http://*.lisag.ch:*",
"https://*.lisag.ch",
"https://*.karten-werk.ch",
"https://geo.ur.ch",
"https://*.geo.ur.ch" // necessary for www.geo.ur.ch etc.
]
}
})
);
app.use(express.static("public"));
const endpoints = ["/wasser?stationid=21872"];
// index page
app.get("/", function (req, res) {
// caching strategy:
// public: allow caching on a server.
// max-age: how long can content be stored in users browsers.
// s-maxage: how long can content be stored on a cdn.
res.set("Cache-Control", "public, max-age=300, s-maxage=600s");
res.render("pages/index", {
endpoints
});
});
// wasser page
app.get("/wasser", async (req, res, next) => {
res.set("Cache-Control", "public, max-age=300, s-maxage=600s");
const { stationid, stationgroupid } = req.query;
if (!stationid) {
res.render("pages/index", {
endpoints
});
return;
}
try {
const station = await waterUtil.getWaterStationInfo(
stationgroupid || false,
stationid
);
res.render("pages/wasser", {
station: station.info,
docs: station.docs,
time_series: station.time_series,
measure_params: station.measure_params,
unit_names: station.unit_names,
measure_periods: station.measure_periods,
service_host: station.service_host,
documents_host: station.documents_host
});
//console.log(station);
} catch (error) {
return next(error);
}
});
// boden page
app.get("/boden", function (req, res) {
const { stationid } = req.query;
res.render("pages/boden", { station: stationid });
});
// not found
app.use(function (req, res, next) {
res.status(404).send("Sorry this route does not exist!");
});
//error
app.use(function (err, req, res, next) {
res.status(500).render("pages/error", { error: err.stack });
});
const port = process.env.PORT || 8080;
app.listen(port);
console.log(`${port} is the magic port`);
module.exports = { app };