forked from FirstLegoLeague/scoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalserver.js
199 lines (177 loc) · 5.39 KB
/
localserver.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
var express = require('express');
var app = express();
var fs = require('fs');
var Q = require('q');
var mkdirp = require("mkdirp");
var dirname = require('path').dirname;
var argv = require('minimist')(process.argv.slice(2));
var port = argv.p||1390;
var basicAuth = require('basic-auth-connect');
var basicAuthCreds = argv.u;
app.use(express.static('src'));
//set up basic authentication
if (basicAuthCreds) {
var pair = basicAuthCreds.split(':');
var user = pair[0];
var pass = pair[1];
app.use(basicAuth(user, pass));
}
//allow cors headers
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
next();
});
//set raw body as data arrives
app.use(function(req, res, next) {
var data = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
req.body = data;
next();
});
});
function readFile(path) {
return Q.promise(function(resolve,reject) {
fs.exists(path,function(exists) {
if (exists) {
resolve(exists);
} else {
reject({
status: 404,
message: 'file not found'
});
}
});
}).then(function() {
return Q.nfcall(fs.readFile, path, "utf-8").catch(function(e) {
throw new Error({
status: 500,
message: 'error reading file'
});
});
});
}
function parseFile(data) {
return Q.promise(function(resolve,reject) {
try {
var res = JSON.parse(data);
resolve(res);
} catch(e) {
reject(e);
}
});
}
function sendError(res) {
return function(err) {
res.status(err.status).send(err.message);
}
}
//reading the "file system"
app.get(/^\/fs\/(.*)$/, function(req, res) {
var path = __dirname + '/data/' + req.params[0];
fs.stat(path, function(err, stat) {
if (err) {
res.status(404).send('file not found');
return;
}
if (stat.isFile()) {
fs.readFile(path, function(err, data) {
if (err) {
res.status(500).send('error reading file');
return;
}
res.send(data);
});
} else if (stat.isDirectory()) {
fs.readdir(path, function(err, filenames) {
if (err) {
res.status(500).send('error reading dir');
return;
}
// FIXME: this doesn't work for filenames containing
// newlines. Probably not likely, but stil...
var hasNewline = filenames.some(function(name) {
return name.indexOf("\n") >= 0;
});
if (hasNewline) {
res.status(500).send('invalid filename(s)');
return;
}
res.send(filenames.join('\n'));
});
} else {
res.status(500).send('error reading file');
return;
}
});
});
//get challenges over xhr, for hosted service
app.get('/challenge/:year', function(req, res) {
var path = __dirname + '/challenges/js/' + req.params.year + '.js';
readFile(path).then(function(data) {
res.header('Content-Type','text/plain');
res.send(data);
}).catch(sendError(res)).done();
});
//get scores by round
app.get('/scores/:round',function(req,res) {
var path = __dirname + '/data/scores.json';
var round = parseInt(req.params.round,10);
readFile(path).then(parseFile).then(function(result) {
var scores = result.scores;
var scoresForRound = scores.filter(function(score) {
return score.published && score.round === round;
});
res.json(scoresForRound);
}).catch(sendError(res)).done();
});
//get the teams info
app.get('/teams',function(req,res) {
var path = __dirname + '/data/teams.json';
readFile(path).then(parseFile).then(function(result) {
res.json(result);
}).catch(sendError(res)).done();
});
app.get('/teams/:nr',function(req,res) {
var path = __dirname + '/data/teams.json';
readFile(path).then(parseFile).then(function(result) {
var team = result.filter(function(team) {
return team.number == req.params.nr;
})[0];
res.json(team);
}).catch(sendError(res)).done();
});
function writeFile(path, contents, cb) {
var dir = dirname(path);
mkdirp(dir, function(err) {
if (err) return cb(err);
fs.writeFile(path, contents, cb);
});
}
// writing the "file system"
app.post(/^\/fs\/(.*)$/, function(req, res) {
var path = __dirname + '/data/' + req.params[0];
writeFile(path, req.body, function(err) {
if (err) {
console.log(err);
res.status(500).send('error writing file');
}
res.status(200).end();
});
});
// deleting in the "file system"
app.delete(/^\/fs\/(.*)$/, function(req, res) {
var path = __dirname + '/data/' + req.params[0];
fs.unlink(path, function(err) {
if (err) {
res.status(500).send('error removing file');
}
res.status(200).end();
});
});
app.listen(port);
console.log('Listening on port ', port);