This repository has been archived by the owner on Jan 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
70 lines (61 loc) · 2.19 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
const express = require('express');
const path = require('path');
const mustacheExpress = require('mustache-express');
const Promise = require('promise');
const compression = require('compression');
const helmet = require('helmet');
const createEnvSettingsFile = require('./src/build/scripts/envSettings');
const getDecorator = require('./src/build/scripts/decorator');
const server = express();
server.use(
helmet({
contentSecurityPolicy: false,
})
);
server.use(compression());
server.set('views', `${__dirname}/dist`);
server.set('view engine', 'mustache');
server.engine('html', mustacheExpress());
createEnvSettingsFile(path.resolve(`${__dirname}/dist/js/settings.js`));
const verifyLoginUrl = () =>
new Promise((resolve, reject) => {
if (!process.env.LOGIN_URL) {
reject();
} else {
resolve();
}
});
const renderApp = (decoratorFragments) =>
new Promise((resolve, reject) => {
server.render('index.html', decoratorFragments, (err, html) => {
if (err) {
reject(err);
} else {
resolve(html);
}
});
});
const startServer = (html) => {
server.use(`${process.env.PUBLIC_PATH}/dist/js`, express.static(path.resolve(__dirname, 'dist/js')));
server.use(`${process.env.PUBLIC_PATH}/dist/css`, express.static(path.resolve(__dirname, 'dist/css')));
server.get(`${process.env.PUBLIC_PATH}/health/isAlive`, (req, res) => res.sendStatus(200));
server.get(`${process.env.PUBLIC_PATH}/health/isReady`, (req, res) => res.sendStatus(200));
server.get(/^\/(?!.*dist).*$/, (req, res) => {
res.send(html);
});
const port = process.env.PORT || 8080;
server.listen(port, () => {
console.log(`App listening on port: ${port}`);
});
};
const logError = (errorMessage, details) => console.log(errorMessage, details);
verifyLoginUrl()
.then(getDecorator, () => {
logError('LOGIN_URL is missing');
process.exit(1);
})
.then(renderApp, (error) => {
logError('Failed to get decorator', error);
process.exit(1);
})
.then(startServer, (error) => logError('Failed to render app', error));