This repository was archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.js
110 lines (95 loc) · 3.03 KB
/
layout.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
108
109
110
const express = require("express");
const Layout = require("@podium/layout");
const path = require("path");
const promClient = require("prom-client");
const PrometheusConsumer = require("@metrics/prometheus-consumer");
const axios = require("axios");
const fetchMiddleware = require("./middleware");
const { basePath, port, isDevelopmentEnv, urls } = require("./config");
const layout = new Layout({
name: "min-side",
pathname: basePath,
development: isDevelopmentEnv,
logger: console,
});
const podlets = [
layout.client.register({
name: "podlet-dittnav-personalia",
uri: urls.dittnavPersonaliaUrl,
resolveJs: true,
resolveCss: true,
}),
layout.client.register({
name: "podlet-dittnav-meldinger",
uri: urls.dittnavMeldingerUrl,
resolveJs: true,
resolveCss: true,
}),
layout.client.register({
name: "podlet-dittnav-generelle-fliser",
uri: urls.dittnavGenerelleFliserUrl,
resolveJs: true,
resolveCss: true,
}),
layout.client.register({
name: "podlet-veientilarbeid",
uri: urls.vtaUrl,
resolveJs: true,
resolveCss: true,
}),
layout.client.register({
name: "podlet-dittnav-lenkeliste",
uri: urls.dittnavLenkelisteUrl,
resolveJs: true,
resolveCss: true,
}),
];
layout.context.register("authlevel", {
parse: async function (inc) {
const cookie = inc.request.headers["cookie"];
if (!cookie) {
return "N/A";
}
try {
const resp = await axios.get("https://innloggingsstatus.dev.nav.no/person/innloggingsstatus/auth", {
headers: {
cookie,
},
});
return resp.data.securityLevel;
} catch (e) {
console.error("Error getting security level", e);
return "N/A";
}
},
});
const metricsConsumer = new PrometheusConsumer({ client: promClient });
promClient.collectDefaultMetrics({ register: metricsConsumer.registry });
metricsConsumer.on("error", (err) => console.error(err));
layout.metrics.pipe(metricsConsumer);
const app = express();
app.use(layout.middleware());
app.set('view engine', 'html');
app.engine('html', require('hbs').__express);
app.set("views", path.resolve(__dirname, "./build"));
app.use(`${layout.pathname()}/static`, express.static("build/static"));
app.use(`/static`, express.static("build/static"));
app.get(`${layout.pathname()}/isAlive|isReady`, (req, res) => {
res.sendStatus(200);
});
app.get(`${layout.pathname()}`, fetchMiddleware(podlets), (req, res) => {
res.status(200).render("index", res.locals);
});
app.get("/metrics", async function (req, res) {
const metrics = await metricsConsumer.metrics();
res.set("Content-Type", metricsConsumer.contentType()).send(metrics);
});
app.use((error, req, res, next) => {
res.status(500).send("<html lang='no'><body><h1>Beklager, det oppstod en uventet feil. Prøv igjen senere.</h1></body></html>");
});
layout.client.refreshManifests().then(() => {
console.log("Manifests refreshed");
});
console.log(`Starting on port ${port} with basePath ${basePath}`);
console.log(`http://localhost:${port}${basePath}`);
app.listen(7000);