-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
208 lines (179 loc) · 5.82 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var http = require('http')
, Router = require('./router')
, db = require('./db')
, util = require('util')
, Resource = require('./resource')
, Keys = require('./keys')
, SessionStore = require('./session').SessionStore
, fs = require('fs')
, io = require('socket.io')
, setupReqRes = require('./util/http').setup
, debug = require('debug')('server')
//, apiauth = require('./api-auth')
, config = require('./config-loader');
function extend(origin, add) {
// don't do anything if add isn't an object
if (!add || typeof add !== 'object') return origin;
var keys = Object.keys(add);
var i = keys.length;
while (i--) {
if(add[keys[i]]) origin[keys[i]] = add[keys[i]];
}
return origin;
}
/**
* Create an http server with the given options and create a `Router` to handle its requests.
*
* Options:
*
* - `db` the database connection info
* - `host` the server's hostname
* - `port` the server's port
*
* Properties:
*
* - `sessions` the servers `SessionStore`
* - `sockets` raw socket.io sockets
* - `db` the servers `Db` instance
*
* Example:
*
* var server = new Server({port: 3000, db: {host: 'localhost', port: 27015, name: 'my-db'}});
*
* server.listen();
*
* @param {Object} options
* @return {HttpServer}
*/
function Server(options) {
var server = process.server = this;
http.Server.call(this);
// defaults
this.options = options = extend({
port: 2403,
db: {port: 27017, host: '127.0.0.1', name: 'deployd'}
}, options);
debug('started with options %j', options);
// an object to map a server to its stores
this.stores = {};
// back all memory stores with a db
this.db = db.create(options.db);
// use socket io for a session based realtime channel
this.sockets = io.listen(this, {
'log level': 0
}).sockets;
// persist sessions in a store
var sessionStore = this.sessions = new SessionStore('sessions', this.db, this.sockets);
// persist keys in a store
var keys = this.keys = new Keys();
this.on('request', function (req, res) {
// dont handle socket.io requests
if(req.url.indexOf('/socket.io/') === 0) return;
debug('%s %s', req.method, req.url);
// add utilites to req and res
setupReqRes(req, res, function(err, next) {
if(err) return res.end(err.message);
/*// authenticate the request
var is_authed2 = 100;
authenticate(req, function(is_authed) {
is_authed2 = is_authed;
if(is_authed == 0 || is_authed == -1 || is_authed == -2) {
debug('error : api authentication failed : ', 'userid: '+req.headers.userid, 'apikey: '+req.headers.apikey);
res.setHeader('Content-Type', 'text/plain');
res.statusCode = 401;
return res.end('Authentication failed');
}
});
// this is so convoluted.. there surely must be a better way?
if(is_authed2 == 0 || is_authed2 == -1 || is_authed2 == -2) {
return;
}*/
sessionStore.createSession(req.cookies.get('sid'), function(err, session) {
if(err) {
debug('session error', err, session);
throw err;
} else {
// (re)set the session id
req.cookies.set('sid', session.sid);
req.session = session;
var route = function() {
config.loadConfig('./', server, function(err, resourcesInstances) {
if (err) throw err;
var router = new Router(resourcesInstances, server);
server.router = router;
server.resources = resourcesInstances;
router.route(req, res);
});
};
var root = req.headers['dpd-ssh-key'] || req.cookies.get('DpdSshKey');
if (options.env === 'development') {
if (root) { req.isRoot = true; }
route();
} else if (root) {
// all root requests
// must be authenticated
debug('authenticating', root);
keys.get(root, function(err, key) {
if(err) throw err;
if(key) req.isRoot = true;
debug('is root?', session.isRoot);
route();
});
} else {
// normal route
route();
}
}
});
});
});
server.on('request:error', function (err, req, res) {
console.error();
console.error(req.method, req.url, err.stack || err);
process.exit(1);
});
}
util.inherits(Server, http.Server);
/**
* Start listening for incoming connections.
*
* @return {Server} for chaining
*/
Server.prototype.listen = function(port, host) {
var server = this;
config.loadConfig('./', server, function(err, resourcesInstances) {
if (err) {
console.error();
console.error("Error loading resources: ");
console.error(err.stack || err);
process.exit(1);
} else {
server.resources = resourcesInstances;
var router = new Router(resourcesInstances, server);
server.router = router;
http.Server.prototype.listen.call(server, port || server.options.port, host || server.options.host);
}
});
return this;
};
/**
* Create a new `Store` for persisting data using the database info that was passed to the server when it was created.
*
* Example:
*
* // Create a new server
* var server = new Server({port: 3000, db: {host: 'localhost', port: 27015, name: 'my-db'}});
*
* // Attach a store to the server
* var todos = server.createStore('todos');
*
* // Use the store to CRUD data
* todos.insert({name: 'go to the store', done: true}, ...); // see `Store` for more info
*
* @param {String} namespace
* @return {Store}
*/
Server.prototype.createStore = function(namespace) {
return (this.stores[namespace] = this.db.createStore(namespace));
};
module.exports = Server;