This repository was archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
121 lines (105 loc) · 3.31 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
require('dotenv').config();
const express = require('express');
const path = require('path');
const mustacheExpress = require('mustache-express');
const Promise = require('promise');
const getDecorator = require('./decorator');
const prometheus = require('prom-client');
// Prometheus metrics
const collectDefaultMetrics = prometheus.collectDefaultMetrics;
collectDefaultMetrics({timeout: 5000});
const httpRequestDurationMicroseconds = new prometheus.Histogram({
name: 'http_request_duration_ms',
help: 'Duration of HTTP requests in ms',
labelNames: ['route'],
// buckets for response time from 0.1ms to 500ms
buckets: [0.10, 5, 15, 50, 100, 200, 300, 400, 500],
});
const server = express();
const env = process.argv[2];
const localbackend = process.argv[3] === 'backend';
const development = process.argv[4] === 'development';
let settings;
if (localbackend && !development) {
settings = require('./settings.json');
} else if (env === 'local') {
settings = { isProd: false };
} else {
settings = require('./settings.json');
}
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(
'/sykepengesoknad/resources',
express.static(path.resolve(__dirname, 'dist/resources')),
);
server.use(
'/sykepengesoknad/img',
express.static(path.resolve(__dirname, 'dist/resources/img')),
);
server.get(
['/', '/sykepengesoknad/?', /^\/sykepengesoknad\/(?!(resources|img)).*$/],
nocache,
(req, res) => {
res.send(html);
httpRequestDurationMicroseconds
.labels(req.route.path)
.observe(10);
},
);
server.get('/actuator/metrics', (req, res) => {
res.set('Content-Type', prometheus.register.contentType);
res.end(prometheus.register.metrics());
});
server.get('/health/isAlive', (req, res) => {
res.sendStatus(200);
});
server.get('/health/isReady', (req, res) => {
res.sendStatus(200);
});
if (env === 'opplaering' || env === 'local') {
require('./mock/mockEndepunkter')(server, env === 'local', localbackend);
}
const port = process.env.PORT;
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);
});