-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathserver.js
More file actions
123 lines (100 loc) · 3.64 KB
/
Copy pathserver.js
File metadata and controls
123 lines (100 loc) · 3.64 KB
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
/*!
governify-render 1.0.0, built on: 2018-05-09
Copyright (C) 2018 ISA group
http://www.isa.us.es/
https://github.com/isa-group/governify-render#readme
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
'use strict';
function check (name, pass, config) {
const nameOK = config.auth.user;
const passOK = config.auth.password;
let valid = true;
valid = name === nameOK && valid;
valid = pass === passOK && valid;
return valid;
}
const deploy = (env, commonsMiddleware, callback) => {
return new Promise((resolve, reject) => {
try {
const bodyParser = require('body-parser');
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const compression = require('compression');
const basicAuth = require('basic-auth');
const path = require('path');
const governify = require('governify-commons');
const logger = governify.getLogger().tag('initialization');
const config = require('./src/backend/configurations');
const app = express();
app.use(
bodyParser.urlencoded({
limit: '50mb',
extended: 'true'
})
);
app.use(
bodyParser.json({
limit: '50mb',
type: 'application/json'
})
);
app.use(commonsMiddleware);
if (config.server.enableHTTPBasicAuth) {
logger.info("Adding 'WWW-Authenticate:' header to every path. Check config/env for getting user and pass");
app.use((req, res, next) => {
const credentials = basicAuth(req);
if (!credentials || !check(credentials.name, credentials.pass, config)) {
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="Snapshot Management Login"');
res.end('Unauthorized');
} else {
next();
}
});
}
const routes = require('./src/backend/routes.js');
app.use(compression());
const frontendPath = path.join(__dirname, '/src/frontend');
logger.info("Serving '%s' as static folder", frontendPath);
app.use(express.static(frontendPath));
if (config.server.bypassCORS) {
logger.info("Adding 'Access-Control-Allow-Origin: *' header to every path.");
app.use(cors());
}
if (config.server.useHelmet) {
logger.info('Adding Helmet related headers.');
app.use(helmet());
}
// Bypassing 405 status put by swagger when no request handler is defined
app.options('/*', (req, res, next) => {
return res.sendStatus(200);
});
const serverPort = process.env.PORT || config.server.port;
app.listen(serverPort, () => {
logger.info('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
});
// For all GET requests, send back index.html
app.use('/', routes);
// app.use('/', express.static(__dirname + '/public'));
} catch (err) {
reject(err);
}
});
};
const undeploy = () => {
process.exit();
};
module.exports = {
deploy: deploy,
undeploy: undeploy
};