This repository has been archived by the owner on Sep 9, 2018. It is now read-only.
forked from ltonetwork/mongodb-rest
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathserver.js
191 lines (157 loc) · 4.13 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
188
189
190
191
/*
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.
*/
var fs = require("fs");
var path = require("path");
var express = require('express');
var https = require('https');
var extend = require("extend");
//
// Default logger to use, if none is passed in.
//
var defaultLogger = {
verbose: function (msg) {
// console.log(msg);
},
info: function (msg) {
console.log(msg);
},
warn: function (msg) {
console.log(msg);
},
error: function (msg) {
console.log(msg);
},
};
var defaultConfig = {
db: 'mongodb://localhost:27017',
server: {
port: 3000,
address: "0.0.0.0"
},
accessControl: {
allowOrigin: "*",
allowMethods: "GET,POST,PUT,DELETE,HEAD,OPTIONS",
allowCredentials: false
},
mongoOptions: {
serverOptions: {
},
dbOptions: {
w: 1
}
},
humanReadableOutput: true,
collectionOutputType: "json",
urlPrefix: "",
logger: defaultLogger,
ssl: {
enabled: false,
options: {}
}
};
var server;
module.exports = {
//
// Start the REST API server.
//
startServer: function (config, started) {
var logger = (config && config.logger) || defaultLogger;
var curDir = process.cwd();
logger.verbose("Current directory: " + curDir);
if (!config) {
var configFilePath = path.join(curDir, "config.js");
if (fs.existsSync(configFilePath)) {
logger.verbose("Loading configuration from: " + configFilePath);
config = require(configFilePath);
config.logger = defaultLogger;
}
else {
logger.verbose("Using default configuration.");
logger.verbose("Please put config.js in current directory to customize configuration.");
config = defaultConfig;
}
}
else {
if (!config.logger) {
config.logger = defaultLogger;
}
}
var app = express();
require('express-csv');
app.use(require('body-parser')());
if (config.humanReadableOutput) {
app.set('json spaces', 4);
}
if (config.accessControl) {
var accesscontrol = require('./lib/accesscontrol')(config);
app.use(accesscontrol.handle);
}
app.get('/favicon.ico', function (req, res) {
res.status(404);
});
if (!config.db) {
config.db = "mongodb://localhost:27017";
}
require('./lib/rest')(app, config);
logger.verbose('Input Configuration:');
logger.verbose(config);
// Make a copy of the config so that defaults can be applied.
config = extend(true, {}, config);
if (!config.server) {
config.server = {};
}
if (!config.server.address) {
config.server.address = "0.0.0.0";
}
if (!config.server.port) {
config.server.port = 3000;
}
logger.verbose('Configuration with defaults applied:');
logger.verbose(config);
var host = config.server.address;
var port = config.server.port;
var ssl = config.ssl || {enabled: false, options: {}};
logger.verbose('Starting mongodb-rest server: ' + host + ":" + port);
logger.verbose('Connecting to db ' + JSON.stringify(config.db, null, 4));
var start = function() {
logger.verbose('Now listening on: ' + host + ":" + port + ' SSL:' + ssl.enabled);
if (started) {
started();
}
};
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);
}
},
//
// Stop the REST API server.
//
stopServer: function () {
if (server) {
server.close();
server = null;
}
},
};
if (process.argv.length >= 2) {
if (process.argv[1].indexOf('server.js') != -1) {
//
// Auto start server when run as 'node server.js'
//
module.exports.startServer();
}
}