-
Notifications
You must be signed in to change notification settings - Fork 8
/
kartlytics.js
245 lines (206 loc) · 5.67 KB
/
kartlytics.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* kartlytics.js: kartlytics server
*
* New videos go through the following state machine:
*
* o uploading: the video is currently being uploaded
*
* o waiting: the video has been uploaded, but has not yet been processed, and
* currently has tasks waiting in line to be processed
*
* o processing: the video is currently being processed by one or more tasks,
* which includes the "kartvid" task that extracts detailed information as
* well as the webm transcode task
*
* o unimported: the video has been processed, but its races have not yet been
* added to the general pool of race data because a person must confirm the
* results
*
* o done: the video has been processed and its races have been imported
*
* When we come up, all videos are either (a) aborted uploads (which we won't
* see at all), (b) done (fully processed), or (c) unprocessed, and are put
* directly into the "waiting" state.
*/
var mod_assert = require('assert');
var mod_fs = require('fs');
var mod_path = require('path');
var mod_bunyan = require('bunyan');
var mod_getopt = require('posix-getopt');
var mod_formidable = require('formidable');
var mod_restify = require('restify');
var mod_kartvid = require('./kartvid');
var klName = 'kartlytics';
var klPort = 8085;
var klAuthfile;
var klAuth, klLog, klServer;
var klVideos = {};
function main()
{
var parser, option;
parser = new mod_getopt.BasicParser('l:a:', process.argv);
while ((option = parser.getopt())) {
if (option.error)
usage();
switch (option.option) {
case 'l':
klPort = parseInt(option.optarg, 10);
if (isNaN(klPort))
usage();
break;
case 'a':
klAuthfile = option.optarg;
break;
default:
/* can't happen */
throw (new Error('unknown option: ' + option.option));
}
}
klLog = new mod_bunyan({
'name': klName,
'level': process.env['LOG_LEVEL'] || 'info'
});
/*
* kartvid assumes it's running out of the root of the repo in order to
* find its assets.
*/
initData();
process.chdir(mod_path.join(__dirname, '..'));
initServer();
}
function usage()
{
console.error('usage: node kartlytics.js [-l port] [-d data_dir] ' +
'[-a authfile]');
process.exit(2);
}
/*
* Read all previously stored data.
*/
function initData()
{
var contents;
if (klAuthfile) {
klLog.info('loading auth file %s', klAuthfile);
contents = mod_fs.readFileSync(klAuthfile);
klAuth = JSON.parse(contents);
}
}
/*
* Set up the HTTP server.
*/
function initServer()
{
var filespath;
klServer = mod_restify.createServer({
'name': klName,
'log': klLog
});
klServer.on('uncaughtException', function (_1, _2, _3, err) {
klLog.fatal(err, 'uncaught exception from restify handler');
throw (err);
});
klServer.use(mod_restify.authorizationParser());
klServer.use(mod_restify.acceptParser(klServer.acceptable));
klServer.use(mod_restify.queryParser());
klServer.use(mod_restify.urlEncodedBodyParser());
filespath = mod_path.normalize(mod_path.join(__dirname, '..', 'docs'));
klServer.get('/', fileServer.bind(
null, mod_path.join(filespath, 'index.htm')));
klServer.get('/resources/.*', dirServer.bind(null, '/resources/',
mod_path.join(filespath, 'resources')));
klServer.post('/kart/video', auth, upload);
klServer.on('after', mod_restify.auditLogger({ 'log': klLog }));
klServer.listen(klPort, function () {
klLog.info('%s server listening at %s',
klServer.name, klServer.url);
});
}
/*
* Restify handler to authenticate a request.
*/
function auth(request, response, next)
{
if (!klAuth) {
next();
return;
}
if (request.authorization &&
request.authorization.basic &&
request.authorization.basic.username &&
klAuth[request.authorization.basic.username] ==
request.authorization.basic.password) {
next();
return;
}
response.header('WWW-authenticate', 'Basic realm="kartlytics"');
next(new mod_restify.UnauthorizedError());
response.end();
}
/*
* Restify handler to redirect any request to the given path.
*/
function redirect(path, request, response, next)
{
response.header('Location', path);
response.send(301);
next();
}
/*
* Restify handler to serve flat files at "baseuri" out of "basedir".
*/
function dirServer(baseuri, basedir, request, response, next)
{
/*
* This is only safe as long as there are no symlinks inside this
* directory tree. We don't bother trying to avoid them, since doing
* it correctly (without races) is much more complex than we really
* need here.
*/
mod_assert.equal(baseuri, request.path().substr(0, baseuri.length));
var filename = mod_path.normalize(
mod_path.join(basedir, decodeURIComponent(
request.path().substr(baseuri.length))));
if (filename.substr(0, basedir.length) != basedir) {
request.log.warn('denying request for file outside of %s',
basedir);
next(new mod_restify.ResourceNotFoundError());
return;
}
fileServer(filename, request, response, next);
}
/*
* Restify handler to serve the given file.
*/
function fileServer(filename, request, response, next)
{
var file = mod_fs.createReadStream(filename);
var headers = {};
headers['access-control-allow-origin'] = '*';
file.on('error', function (err) {
if (err['code'] == 'ENOENT') {
next(new mod_restify.ResourceNotFoundError());
return;
}
request.log.error(err);
next(err);
});
file.on('open', function () {
file.removeAllListeners('error');
response.writeHead(200, headers);
file.pipe(response);
file.on('error', function (err) {
request.log.warn(err);
response.end();
});
});
file.on('end', next);
}
/*
* Restify handler for handling form uploads.
*/
function upload(request, response, next)
{
next(new mod_restify.BadRequestError('Unauthorized'));
}
main();