forked from misterhat/captmoose
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
142 lines (118 loc) · 3.95 KB
/
Copy pathapp.js
File metadata and controls
142 lines (118 loc) · 3.95 KB
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
var fs = require('fs'),
http = require('http'),
Datastore = require('nedb'),
jsonBody = require('body/json'),
routes = require('routes'),
st = require('st');
var config = require('./config'),
def = require('./moose-def'),
db = new Datastore({ filename: './moose.db', autoload: true }),
mount = st({ path: __dirname + '/browser', url: '/', passthrough: true }),
router = routes(),
// cache index to avoid fs reads
indexHtml = fs.readFileSync('./browser/index.html'),
server;
// make sure name is indexed since it's used for finding the moose in IRC
db.ensureIndex({ fieldName: 'name' });
// make sure the moose has no colours that aren't defined here
function validateMoose(moose) {
var i, j;
if (moose.length !== def.height || moose[0].length !== def.width) {
return false;
}
for (i = 0; i < moose.length; i += 1) {
for (j = 0; j < moose[0].length; j += 1) {
// is the colour of the cell in our colour definition?
if (def.colours.indexOf(moose[i][j]) === -1) {
return false;
}
}
}
return true;
}
function error(req, res, message) {
res.statusCode = 500;
res.end('{"error":"' + message + '"}');
}
function notFound(req, res) {
res.statusCode = 404;
res.end('{"error":"not found"}');
}
// used finding the latest moose in the gallery
router.addRoute('/moose/latest', function (req, res) {
db.find({}).sort({ added: 1 }).limit(10).exec(function (err, moose) {
if (err) {
console.error(err.stack);
return error(req, res, 'database error');
}
res.end(JSON.stringify(moose));
});
});
router.addRoute('/moose/:name', function (req, res, params) {
// find a specific moose by name
if (req.method === 'GET') {
db.findOne({ name: params.name }, function (err, moose) {
if (err) {
console.error(err.stack);
return error(req, res, 'database error');
}
if (moose) {
res.end(JSON.stringify(moose));
} else {
notFound(req, res);
}
});
// create a new moose
} else if (req.method === 'POST') {
if (!/[A-z0-9 -_]+/.test(params.name) || params.name.length > 48) {
return error(req, res, 'invalid moose name');
}
jsonBody(req, function (err, body) {
if (err) {
console.error(err.stack);
return error(req, res, 'invalid json');
}
if (!validateMoose(body)) {
error(req, res, 'malformed moose (wrong colours or size)');
return;
}
db.findOne({ name: params.name }, function (err, moose) {
if (err) {
console.error(err.stack);
return error(req, res, 'database error');
}
if (moose) {
// 409 is used to specify existing moose
res.statusCode = 409;
return res.end('{"error":"moose already exists" }');
}
db.insert({
name: params.name,
moose: body,
added: Date.now()
});
res.end('{"success":"created new moose"}');
});
});
} else {
error(req, res, 'wrong method');
}
});
// additional to the /index.html handled by st
router.addRoute('/', function (req, res) {
res.end(indexHtml);
});
router.addRoute('/edit/:moose', function (req, res) {
res.end(indexHtml);
});
server = http.createServer(function (req, res) {
var match = router.match(req.url);
if (match) {
match.fn(req, res, match.params);
} else {
mount(req, res, notFound.bind(null, req, res));
}
});
server.listen(config.port, function () {
console.log('listening on ' + server.address().port);
});