-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
68 lines (60 loc) · 1.75 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
var os = require('os');
var url = require('url');
var http = require('http');
var querystring = require('querystring');
function AdServer(options, providers) {
console.dir(providers);
if(!providers){
providers = {};
}
this.providers = providers;
this.options = options;
this.address = 'http://' + options.host + ':' + options.port;
var This = this;
http.createServer(function handler(request, response) {
This.handleRequest(request, response);
}).listen(options.port, options.host);
console.log('Server running at ' + this.address);
}
AdServer.prototype.getProvider = function(providerName) {
if (!this.providers[providerName]) {
try {
this.providers[providerName] = require('./providers/' + providerName).get(this);
} catch (err) {
console.error(err.stack);
return null;
}
}
return this.providers[providerName];
};
AdServer.prototype.handleRequest = function(request, response) {
var urlInfo = url.parse(request.url);
var pathParts = urlInfo.pathname.split('/');
if (pathParts.length !== 3) {
response.writeHead(404);
response.end('Page not found!');
return;
}
var providerName = pathParts[1].toLowerCase();
var action = pathParts[2].toLowerCase();
var params = querystring.parse(urlInfo.query);
var provider = this.getProvider(providerName);
if(!provider){
response.writeHead(404);
response.end('Service [' + providerName + '] not found!');
}
try {
if(typeof provider[action] === 'function'){
provider[action].apply(provider, [ request, response, params ]);
}
else{
response.writeHead(404);
response.end('Action [' + providerName + '.' + action + '] not found!');
}
} catch (err) {
console.error(err.stack);
}
};
exports.create = function(options, providers){
return new AdServer(options, providers);
};