This repository has been archived by the owner on Oct 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
93 lines (86 loc) · 2.55 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
var http = require('http')
,parseUrl = require('url').parse
,fs = require('fs')
,di = require('./lib/di')
// Import settings and inject into di module.
,conf = di.conf = JSON.parse(fs.readFileSync('settings.json'))
// Load own modules after injection.
,controller = require('./lib/controller')
,dispatch = require('./lib/dispatch');
// If an argument is given in the command line, override server port.
if (process.argv[2])
conf.serverPort = parseInt(process.argv[2]);
var server = http.createServer()
,rootPath = conf.serverRootPath || ''; // Just easier to use.
server.on('request', function (req, res) {
var url = parseUrl(req.url)
,handlePath = (url.pathname.substr(0, rootPath.length) === rootPath)
,handled = 0
,handledMethods = 0;
console.log(req.method+' '+req.url);
console.dir(req.headers);
if (handlePath) {
var path = url.pathname.substr(rootPath.length);
dispatch.match(path, {
'^/geo/([^/]+)$': {
GET: function (id) {
controller.getAnonymousGeo(req, res, id);
}
}
,'^/geo$': {
GET: function () {
controller.listAnonymousGeo(req, res);
}
,POST: function () {
controller.saveAnonymousGeo(req, res);
}
}
,'^/user/([^/]+)$': {
GET: function (username) {
controller.getUser(req, res, username);
}
}
,'^/user/([^/]+)/geo$': {
GET: function (username) {
controller.listGeo(req, res, username);
}
,POST: function (username) {
controller.saveGeo(req, res, username);
}
}
,'^/token$': {
GET: function () {
controller.getToken(req, res);
}
,POST: function () {
controller.getToken(req, res);
}
}
,'.*': {
OPTIONS: function () {
controller.okCors(req, res);
}
}
}
,function (handler, args) {
handled++;
//if (handled++ === 0) {
console.dir(arguments);
dispatch.route(req.method, handler, function (method) {
handledMethods++;
method.apply(this, args);
}
,function () {
// Method unhandled.
});
//} else {
// console.log('WARNING: "'
// +url.pathname+'" matched more than one handler.');
//}
});
}
if (handled === 0) controller.notFound(req, res);
else if (handledMethods === 0) controller.methodNotAllowed(req, res);
});
server.listen(conf.serverPort, conf.serverHost);
console.log('Server running at http://'+conf.serverHost+':'+conf.serverPort+'/');