This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
146 lines (123 loc) · 4.29 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
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
137
138
139
140
141
142
143
144
145
146
require('dotenv').config();
const express = require('express');
const path = require('path');
const mustacheExpress = require('mustache-express');
const Promise = require('promise');
const promClient = require('prom-client');
const getDecorator = require('./decorator');
const counters = require('./server/counters');
// Prometheus metrics
const setupMetrics = () => {
const { collectDefaultMetrics, Registry } = promClient;
collectDefaultMetrics({ timeout: 5000 });
const register = new Registry();
register.registerMetric(counters.httpRequestDurationMicroseconds);
register.registerMetric(counters.userKlikkJaMotebehovCounter);
register.registerMetric(counters.userKlikkNeiMotebehovCounter);
register.registerMetric(counters.userKlikkAktivitetsplan);
collectDefaultMetrics({ register });
return register;
};
const prometheus = setupMetrics();
const server = express();
const env = process.argv[2];
const settings = env === 'local' ? { isProd: false } : require('./settings.json');
// Logg alle innkommende lenker til gammel sykmeldinger del av appen
server.use((req, res, next) => {
if (req.path.includes('/sykefravaer/sykmeldinger')) {
console.warn(`Innkommende lenke fra referer: ${req.headers.referer || 'Ikke satt'}`);
}
next();
});
server.set('views', `${__dirname}/dist`);
server.set('view engine', 'mustache');
server.engine('html', mustacheExpress());
const renderApp = (decoratorFragments) => {
return new Promise((resolve, reject) => {
server.render(
'index.html',
Object.assign(
{},
decoratorFragments,
settings,
),
(err, html) => {
if (err) {
reject(err);
} else {
resolve(html);
}
},
);
});
};
function nocache(req, res, next) {
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
}
const startServer = (html) => {
server.use(
'/sykefravaer/resources',
express.static(path.resolve(__dirname, 'dist/resources')),
);
server.use(
'/sykefravaer/img',
express.static(path.resolve(__dirname, 'dist/resources/img')),
);
server.get(
['/', '/sykefravaer/?', /^\/sykefravaer\/(?!(resources|img)).*$/],
nocache,
(req, res) => {
res.send(html);
counters.httpRequestDurationMicroseconds
.labels(req.route.path)
.observe(10);
},
);
server.get('/prometheus', (req, res) => {
res.set('Content-Type', prometheus.contentType);
res.end(prometheus.metrics());
});
server.post('/sykefravaer/metrics/actions/links/:type', (req, res) => {
const counterPostfix = req.params.type
? req.params.type
: '';
const counterKey = counters.getMetricName(counters.METRIC_FILTER_INFIX, counterPostfix);
prometheus.getSingleMetric(counterKey).inc(1, new Date());
res.sendStatus(200);
});
server.post('/sykefravaer/metrics/actions/aktivitetsplan/:antallSykedager', (req, res) => {
const antallSykedager = req.params.antallSykedager
? req.params.antallSykedager
: -1;
const counterKey = counters.getMetricName(counters.METRIC_FILTER_INFIX, 'aktivitetsplan');
prometheus.getSingleMetric(counterKey).labels(antallSykedager).inc(1, new Date());
res.sendStatus(200);
});
server.get('/is_alive', (req, res) => {
res.sendStatus(200);
});
server.get('/is_ready', (req, res) => {
res.sendStatus(200);
});
if (env === 'opplaering' || env === 'local') {
require('./mock/mockEndepunkter')(server, env === 'local');
}
const port = process.env.PORT || 8080;
server.listen(port, () => {
console.log(`App listening on port: ${port}`);
});
};
const logError = (errorMessage, details) => {
console.log(errorMessage, details);
};
getDecorator()
.then(renderApp, (error) => {
logError('Failed to get decorator', error);
process.exit(1);
})
.then(startServer, (error) => {
logError('Failed to render app', error);
});