-
Notifications
You must be signed in to change notification settings - Fork 4
/
worker.js
112 lines (94 loc) · 3.25 KB
/
worker.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
/**
* worker.js
* @description: server subprocess
* @author: Chris Young <[email protected]>
*/
var cluster = require('cluster'),
http = require('http'),
domain = require('domain'),
url = require('url');
var config = require(__dirname+'/config.json'),
router = require(__dirname+'/router.js'),
bot = require(__dirname+'/bot.js');
var id = require(__dirname+'/library/id.js'),
log = require(__dirname+'/library/log.js'),
parse = require(__dirname+'/library/parse.js');
module.exports = function worker() {
bot.setup(function setup(error) {
if (error) {
log.error('bot setup failed', error);
process.exit(-1);
}
var server = http.createServer(function server(request, response) {
var serverDomain = domain.create();
serverDomain.add(request);
serverDomain.add(response);
serverDomain.on('error', function domainError(error) {
log.error('uncaught exception', error, response.id);
try {
var kill = setTimeout(function timeout() {
process.exit(1);
}, 30000);
kill.unref();
server.close();
cluster.worker.disconnect();
response.statusCode = 500;
response.end();
} catch (exception) {
log.error('failed to respond after uncaught exception', exception, response.id);
}
});
serverDomain.run(function run() {
request.id = id();
request.body = '';
request.on('error', function runError(error) {
log.error('request error', error, request.id);
response.statusCode = 500;
response.end();
});
request.on('data', function runData(data) {
request.body += data;
});
request.on('end', function runEnd() {
request.url = url.parse(request.url);
request.url.parameters = request.url.query ? parse.httpParameters(request.url.query) : [];
log.info('request', {
method: request.method,
pathname: request.url.pathname,
parameters: request.url.parameters,
ip: request.connection.remoteAddress
}, request.id);
log.info('headers', request.headers, request.id);
if (request.body) {
try {
request.body = parse.httpParameters(request.body);
log.info('body', request.body, request.id);
} catch (exception) {
log.error('failed to parse request body', exception, request.id);
response.statusCode = 415;
response.end();
return;
}
}
request.data = {};
if (request.body.token) {
if (request.body.token === config.slack.token )
router(request, response);
else {
log.error('invalid token', request.body.token, request.id);
response.statusCode = 403;
response.end();
}
} else {
log.error('missing token', {}, request.id);
response.statusCode = 403;
response.end();
}
});
});
});
server.listen(config.port, function listen() {
log.info('listening on port ' + config.port);
});
});
};