2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-04-03 19:21:41 +08:00
|
|
|
const express = require('express');
|
|
|
|
const jsonReplacer = require('./utils/json-replacer');
|
2018-04-09 22:18:30 +08:00
|
|
|
const ApiRouter = require('./api/api-router');
|
|
|
|
const ServerInfoController = require('./server-info-controller');
|
2018-04-03 19:21:41 +08:00
|
|
|
const StatsClient = require('./stats/client');
|
2015-07-05 05:09:00 +08:00
|
|
|
|
2018-04-03 01:02:31 +08:00
|
|
|
module.exports = function createServer (serverOptions) {
|
2019-10-22 05:33:27 +08:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(serverOptions, 'routes')) {
|
2019-10-02 01:34:03 +08:00
|
|
|
throw new Error('Must initialise server with "routes" as base paths configuration');
|
|
|
|
}
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2018-04-03 01:02:31 +08:00
|
|
|
// Make stats client globally accessible
|
|
|
|
global.statsClient = StatsClient.getInstance(serverOptions.statsd);
|
2015-07-05 02:41:22 +08:00
|
|
|
|
2018-04-03 19:21:41 +08:00
|
|
|
const app = express();
|
2018-03-29 01:37:31 +08:00
|
|
|
|
2018-04-03 19:21:41 +08:00
|
|
|
app.enable('jsonp callback');
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
app.disable('etag');
|
|
|
|
app.set('json replacer', jsonReplacer());
|
2018-04-03 18:26:35 +08:00
|
|
|
|
2018-04-06 01:42:20 +08:00
|
|
|
const apiRouter = new ApiRouter({ serverOptions, environmentOptions: global.environment });
|
2019-10-04 18:35:12 +08:00
|
|
|
|
2019-10-04 18:22:23 +08:00
|
|
|
apiRouter.route(app, serverOptions.routes.api);
|
2018-04-04 01:16:37 +08:00
|
|
|
|
2020-04-04 23:42:26 +08:00
|
|
|
const serverInfoController = new ServerInfoController();
|
2019-10-04 18:22:23 +08:00
|
|
|
serverInfoController.route(app);
|
2017-09-21 17:46:31 +08:00
|
|
|
|
2015-07-05 02:41:22 +08:00
|
|
|
return app;
|
|
|
|
};
|