forked from governify/reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
187 lines (160 loc) · 5.73 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*!
governify-project-gauss-reporter 1.0.0, built on: 2018-04-19
Copyright (C) 2018 ISA group
http://www.isa.us.es/
https://github.com/isa-group/governify-project-gauss-reporter
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';
/*
* Put here your dependencies
*/
module.exports = {
deploy: _deploy,
undeploy: _undeploy
};
/**
* statesAgreementGET.
* @param {Object} configurations configuration object
* @param {function} callback callback function
* @alias module:registry.deploy
* */
function _deploy (configurations, commonsMiddleware, callback) {
const governify = require('governify-commons');
const config = governify.configurator.getConfig('main');
// Add this to the VERY top of the first file loaded in your app
const apm = require('elastic-apm-node').start({
// Override service name from package.json
// Allowed characters: a-z, A-Z, 0-9, -, _, and space
serviceName: 'Reporter',
serviceNodeName: 'Reporter',
captureBody: 'all',
transactionMaxSpans: -1,
usePathAsTransactionName: true,
abortedErrorThreshold: 0,
distributedTracingOrigins: ['*'],
active: config.telemetry.enabled,
server: config.telemetry.server
});
const http = require('http'); // Use http if your app will be behind a proxy.
const https = require('https'); // Use https if your app will not be behind a proxy.
const bodyParser = require('body-parser');
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const compression = require('compression');
const fs = require('fs');
const path = require('path');
// Self dependencies
const logger = require('governify-commons').getLogger().tag('server');
const swaggerUtils = require('./src/backend/utils').swagger;
const server = null;
const app = express();
if (config.server.bypassCORS) {
logger.info("Adding 'Access-Control-Allow-Origin: *' header to every path.");
app.use(cors());
}
logger.info("Using '%s' as HTTP body size", config.server.bodySize);
app.use(
bodyParser.urlencoded({
limit: config.server.bodySize,
extended: 'true'
})
);
app.use(
bodyParser.json({
limit: config.server.bodySize,
type: 'application/json'
})
);
app.use(commonsMiddleware);
const frontendPath = path.join(__dirname, './src/frontend');
const serverPort = process.env.PORT || config.server.port;
const CURRENT_API_VERSION = 'v4';
app.use(express.static(frontendPath));
// Default server options
app.use(compression());
// Configurable server options
if (config.server.useHelmet) {
logger.info('Adding Helmet related headers.');
app.use(helmet());
}
if (config.server.httpOptionsOK) {
app.options('/*', function (req, res) {
logger.info('Bypassing 405 status put by swagger when no request handler is defined');
return res.sendStatus(200);
});
}
if (config.server.servePackageInfo) {
app.use('/api/info', function (req, res) {
logger.debug("Serving package.json at '%s'", '/api/info');
res.json(require('./../../package.json'));
});
}
// latest documentation redirection
app.use('/api/latest/docs', function (req, res) {
res.redirect('/api/' + CURRENT_API_VERSION + '/docs');
});
app.use('/api/latest/api-docs', function (req, res) {
res.redirect('/api/' + CURRENT_API_VERSION + '/api-docs');
});
app.use('/publicInfrastructure', function (req, res) {
res.json(governify.infrastructure.getServices().external);
});
logger.info('Trying to deploy server');
if (configurations) {
logger.info('Reading configuration...');
for (const c in configurations) {
const prop = configurations[c];
logger.info('Setting property' + c + ' with value ' + prop);
config.setProperty(c, prop);
}
}
// list of swagger documents, one for each version of the api.
const swaggerDocs = [
swaggerUtils.getSwaggerDoc(4)
];
// initialize swagger middleware for each swagger documents.
swaggerUtils.initializeMiddleware(app, swaggerDocs, function () {
if (config.server.listenOnHttps) {
https.createServer({
key: fs.readFileSync('certs/privkey.pem'),
cert: fs.readFileSync('certs/cert.pem')
}, app).listen(serverPort, function () {
logger.info('HTTPS_SERVER mode');
logger.info('Your server is listening on port %d (https://localhost:%d)', serverPort, serverPort);
logger.info('Swagger-ui is available on https://localhost:%d/api/%s/docs', serverPort, CURRENT_API_VERSION);
});
} else {
http.createServer(app).listen(serverPort, function () {
logger.info('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
logger.info('Swagger-ui is available on http://localhost:%d/api/%s/docs', serverPort, CURRENT_API_VERSION);
if (callback) {
callback(server);
}
});
}
});
}
/**
* _undeploy.
* @param {function} callback callback function
* @alias module:registry.undeploy
* */
function _undeploy (callback) {
/* db.close(function () {
server.close(function () {
logger.info('Server has been closed');
callback();
});
}); */
callback();
}