-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
162 lines (138 loc) · 4.11 KB
/
server.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
var express = require('express');
var mongodb = require('mongodb');
var app = express();
app.configure(function() {
app.use(express.errorHandler({ showStack: true, dumpExceptions: true }));
app.use(express.bodyParser());
app.use(express.static(__dirname));
});
// DB Setup
var connection;
var db;
function guard(onError, onSuccess) {
return function(error, data) {
if(error) return onError(error);
onSuccess(data);
}
};
function connect(onConnect) {
if (!connection)
connection = new mongodb.Db('new_deathmatch', new mongodb.Server("127.0.0.1",27017, { auto_reconnect : true }), {safe:false});
if (db) return onConnect(null,db);
connection.open(function(error, db_) {
db = db_;
onConnect(null,db);
});
};
function collection(collectionName) {
return function(onLoad) {
connect(function(error, db) {
if(error) return onLoad(error);
db.collection(collectionName, onLoad);
});
};
};
function save(collection, message, onSaved) {
return collection(guard(onSaved,save));
function save(collection) {
collection.save(message, onSaved);
}
};
function stream(collection, criteria, onEach, onDone) {
var cursor;
return collection(guard(onEach,withCollection));
function withCollection(collection) {
cursor = collection.find(criteria);
cursor.nextObject(next);
}
function next(error, message) {
if(!error && !message) return onDone ? onDone() : null;
onEach(error, message);
cursor.nextObject(next);
}
}
function find(collection, criteria, cb) {
return collection(guard(cb,withCollection));
function withCollection(collection) { collection.find(criteria, guard(cb,withCursor)) }
function withCursor(c) { c.each(cb) }
}
var simulations = collection('simulations');
var generations = collection('generations');
connect(function() {
simulations(function(err,collection) {
collection.ensureIndex('key')
})
generations(function(err,collection) {
collection.ensureIndex('key');
collection.ensureIndex('simulation');
collection.ensureIndex('index');
})
})
app.get('/', function(request, response, next) {
response.send({ok:true});
});
app.get('/simulations/:key', function( request, response, next ) {
return find(simulations, {key:request.params.key}, withResult);
function withResult(err, simulation) {
if (err) return next(err);
if (!simulation) return response.send(404)
return response.send(simulation);
}
})
app.put('/simulations/:key', function( request, response, next ) {
request.body.key = request.params.key;
return save(simulations, request.body, onSave);
function onSave(err) {
if (err) return next(err);
return response.send(201);
}
})
app.get('/latest-generation/:simulation', function( request, response, next ) {
generations(function(err,collection) {
collection.aggregate([
{$project:{simulation:1,index:1}},
{$match:{simulation:request.params.simulation}},
{$sort:{index:-1}},
{$limit:1}],
fetchById)
function fetchById( err, result ) {
if (!err && result && result.length)
collection.findOne({_id:result[0]._id}, send);
else
return response.send(404);
}
function send( err, generation ) {
if (err)
response.send(500);
response.send(generation);
}
})
})
app.post('/generations/aggregate', function( request, response, next ) {
generations(function(err,collection) {
collection.aggregate(request.body, respond)
function respond( err, results ) {
if ( err )
next( err )
response.send(results);
}
})
})
app.get('/generations/:key', function( request, response, next ) {
return find(generations, {key:request.params.key}, withResult);
function withResult(err, generation) {
if (err) return next(err);
if ( generation != null )
response.send(generation);
}
})
app.put('/generations/:key', function( request, response, next ) {
request.body.key = request.params.key;
return save(generations, request.body, onSave);
function onSave(err) {
if (err) return next(err);
return response.send(201);
}
})
app.listen('9024')
console.log("listening on 9024")