This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
server.js
115 lines (95 loc) · 2.66 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
/**
* server.js
* mongodb-rest
*
* Maintained by Ashley Davis 2014-07-02
* Created by Tom de Grunt on 2010-10-03.
* Copyright (c) 2010 Tom de Grunt.
* This file is part of mongodb-rest.
*/
const fs = require("fs");
const path = require("path");
const express = require('express');
const nocache = require('nocache');
const bodyParser = require('body-parser');
const https = require('https');
const extend = require("extend");
const initRoutes = require('./lib/routes');
const resolveConfig = require('./lib/config/resolve-config');
require('express-csv');
var server = null;
module.exports = {
startServer,
stopServer
}
/**
* Start the REST API server.
*/
function startServer(customConfig, onStarted) {
const app = express();
const config = resolveConfig.with(customConfig);
init(app, config);
run(app, config, onStarted);
}
/**
* Stop the REST API server.
*/
function stopServer() {
if (!server) return;
server.close();
server = null;
}
/**
* Init express application
* @param {object} app
* @param {object} config
*/
function init(app, config) {
app.use(nocache());
app.use(bodyParser());
if (config.humanReadableOutput) {
app.set('json spaces', 4);
}
if (config.accessControl) {
const accesscontrol = require('./lib/auth/accesscontrol')(config);
app.use(accesscontrol.handle);
}
app.get('/favicon.ico', function(req, res) {
res.status(404);
});
initRoutes(app, config);
}
/**
* Perform server launch
* @param {object} app
* @param {object} config
* @param {callback} onStarted
*/
function run(app, config, onStarted) {
const logger = config.logger;
const host = config.server.address;
const port = config.server.port;
const ssl = config.ssl || {enabled: false, options: {}};
const start = function() {
logger.verbose('Now listening on: ' + host + ":" + port + ' SSL:' + ssl.enabled);
if (onStarted) onStarted();
};
logger.verbose('Starting mongodb-rest server: ' + host + ':' + port);
logger.verbose('Connecting to db: ' + JSON.stringify(config.db, null, 4));
if (ssl.enabled) {
if (ssl.keyFile) ssl.options.key = fs.readFileSync(ssl.keyFile);
if (ssl.certificate) ssl.options.cert = fs.readFileSync(ssl.certificate);
server = https.createServer(ssl.options, app).listen(port, host, start);
} else {
server = app.listen(port, host, start);
}
}
/**
* Auto start server when run as 'node server.js'
*/
+function() {
const autoStart =
process.argv.length >= 2 &&
process.argv[1].indexOf('server.js') != -1;
if (autoStart) module.exports.startServer();
}();