-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (116 loc) · 3.89 KB
/
index.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
var path = require('path')
, fs = require('fs')
, _ = require('underscore')
, Handlebars = require('handlebars')
, request = require('request')
, yaml = require('js-yaml')
, express = require('express')
, CONFIG = require('config')
, PORT = process.env.PORT || CONFIG.app.port
, ajaxHandlersContainer = require('./server/ajaxHandlers')
, ajaxHandlers
, requestProxy = require('./server/requestProxy')
, SprinterDash = require('sprinter-dash')
, githubBadge = require('./server/github-badge')
;
function writeHtmlTemplate(name, layout) {
var html;
if (! layout && CONFIG.layout) {
layout = CONFIG.layout;
}
html = fs.readFileSync(path.join(__dirname, 'layouts', layout + '.html'));
fs.writeFileSync(path.join(__dirname, 'client', name + '.html'), html);
}
function getGlobalRepos(url, cb) {
console.log('Fetching global repo list from %s', url);
request.get(url, function(err, resp, body) {
var repos;
if (err) {
return cb(err);
}
try {
// Have to append the "---" line to the start of the YAML file or it
// doesn't parse properly.
repos = yaml.safeLoad("---\n" + body).repos;
// Default branch is master.
_.each(repos, function(repo) {
if (! repo.branch) { repo.branch = 'master'; }
});
} catch(e) {
throw new Error('Config file "' + url + '" is invalid YAML!');
}
cb(null, repos);
});
}
function normalizeConfig(cfg) {
if (cfg.refresh_rate) {
_.each(cfg.monitors, function(monitor) {
if (! monitor.refresh_rate) {
monitor.refresh_rate = cfg.refresh_rate;
}
});
}
cfg.github = {
username: process.env.GH_USERNAME
, password: process.env.GH_PASSWORD
};
}
function generateOneMonitorPageHandler(config, callback) {
ajaxHandlersContainer.getMonitors(config.monitors, function(monitors) {
var templateHtml = fs.readFileSync(path.join(__dirname, 'layouts/monitor.html'), 'utf-8')
, template = Handlebars.compile(templateHtml);
callback(function(req, res) {
var monitor = req.params.monitorName
, monitorConfig = monitors[monitor]
, title = ''
, body
;
if (monitorConfig.options && monitorConfig.options.name) {
title = monitorConfig.options.name;
}
body = template({
title: title
, monitorName: monitor
, monitorConfig: JSON.stringify(monitorConfig)
});
res.end(body);
});
});
}
function startServer() {
console.log('starting server');
var app = express()
.use(express.json())
.use(express.urlencoded())
.use(express.static(__dirname + '/client'))
.get('/github-badge/:repo/:issue', githubBadge(CONFIG))
// HTTP request proxy
.use(requestProxy());
generateOneMonitorPageHandler(CONFIG, function(oneMonitorHandler) {
app.get('/monitor/:monitorName', oneMonitorHandler);
// Adding handling for system ajax calls.
_.each(ajaxHandlers, function(handler, path) {
app.get(path, handler);
});
dash = new SprinterDash({
repos: CONFIG.repos
, travisOrg: 'numenta'
, title: 'Numenta OS Issues'
});
dash.attach(app, '/');
app.listen(PORT, function() {
console.log('nupic.wallboard server running on\n'
+ '\thttp://localhost:' + PORT);
});
});
}
getGlobalRepos(CONFIG.repos_url, function(err, repos) {
if (err) {
throw err;
}
CONFIG.repos = repos;
normalizeConfig(CONFIG);
ajaxHandlers = ajaxHandlersContainer.initializer(CONFIG);
writeHtmlTemplate('index', 'nupic');
startServer();
});