forked from carlfriess/nupic.tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.js
executable file
·90 lines (78 loc) · 3.27 KB
/
program.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
// MAIN PROGRAM, start here.
// global libs
var assert = require('assert'),
fs = require('fs'),
path = require('path'),
connect = require('connect'),
log = require('./utils/log'),
// local libs
utils = require('./utils/general'),
githubHookHandler = require('./githubHook'),
// The configReader reads the given file, and merges it with any existing user
// configuration file.
cfg = require('./utils/configReader').read(path.join(__dirname, 'conf/config.json')),
HOST = cfg.host,
PORT = cfg.port || 8081,
baseUrl = 'http://' + HOST + ':' + PORT,
// This path is registered with Github as a webhook URL.
githubHookPath = '/github-hook',
prWebhookUrl = baseUrl + githubHookPath,
// This directory contains all the additional service
// handlers that will be loaded dynamically and attached
// to this web server.
HANDLER_DIR = 'handlers';
log.info('nupic.tools server starting...');
log('nupic.tools will use the following configuration:');
log.verbose(JSON.stringify(utils.sterilizeConfig(cfg), null, 2));
utils.constructRepoClients(prWebhookUrl, cfg, function(repoClients) {
var dynamicHttpHandlerModules,
activeValidators,
// The Connect JS application
app = connect(),
padInt = utils.padInt,
padDecimal = utils.padDecimal;
// Print the time of the request to the millisecond.
app.use(function(req, res, next) {
var now = new Date(),
dateString = now.getFullYear() + '/' +
padInt(now.getMonth() + 1) + '/' +
padInt(now.getDate()) + ' ' +
padInt(now.getHours()) + ':' +
padInt(now.getMinutes()) + ':' +
padInt(now.getSeconds()) + '.' +
padDecimal(now.getMilliseconds());
log('\n' + dateString + ' | Request received');
next();
});
// Enable a log of logging.
app.use(connect.logger('dev'))
// Auto body parsing is nice.
.use(connect.bodyParser())
// This puts the Github webhook handler into place
.use(githubHookPath, githubHookHandler.initializer(repoClients, cfg));
log.debug('The following validators are active:');
activeValidators = githubHookHandler.getValidators();
activeValidators.forEach(function(v) {
log.verbose('\t==> ' + v);
});
dynamicHttpHandlerModules = utils.initializeModulesWithin(HANDLER_DIR);
// Loads all the modules within the handlers directory, and registers the URLs
// the declared, linked to their request handler functions.
log.debug('The following URL handlers are active:');
dynamicHttpHandlerModules.forEach(function(handlerConfig) {
var urls = Object.keys(handlerConfig);
urls.forEach(function(url) {
var handler = handlerConfig[url](repoClients, dynamicHttpHandlerModules, cfg, activeValidators),
name = handler.title,
desc = handler.description,
msg = '\t==> ' + name + ' listening for url pattern: ' + url;
if (! handler.disabled) {
log.verbose(msg);
app.use(url, handler);
}
});
});
app.listen(PORT, function() {
log.info('\nServer running at ' + baseUrl + '\n');
});
});