-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb.js
118 lines (106 loc) · 3.45 KB
/
web.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
var http = require('http');
var express = require('express');
var sys = require('sys');
var fs = require('fs');
var url = require('url');
var uuid = require('node-uuid');
var mongo = require('mongodb');
var ObjectID = mongo.ObjectID;
var mongoUri = process.env.MONGOLAB_URI || 'mongodb://localhost/bodiesReferenceEngine';
var port = process.env.PORT || 5004;
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
function getLinkByRel(links, rel) {
for (var i in links) {
if (links[i].rel === rel) {
return links[i];
}
}
return undefined;
}
var app = express();
app.configure(function(){
app.use(express.bodyParser());
});
mongo.connect(mongoUri, {}, function(error, db) {
console.log('connected to mongodb @ ' + mongoUri);
db.addListener('error', function(error) {
console.log(error);
});
// Endpoint resource
app.get('/metasim/:version', function(request, response) {
if (request.params.version == '1.0') {
response.send({
links: [{
rel: '/rel/simulations',
href: '/metasim/' + request.params.version+ '/simulations',
method: 'GET'}]});
} else {
response.send(404, null);
}
});
// Simulations resource
app.get('/metasim/:version/simulations', function(request, response) {
if (request.params.version == '1.0') {
db.collection('simulations').find({}).toArray(function(err, simulations) {
console.log('sending simulations' + JSON.stringify(simulations));
response.send({
simulations: simulations,
links: [{
rel: '/rel/add',
href: '/metasim/' + request.params.version+ '/simulations',
method: 'POST'}]});
});
} else {
response.send(404, null);
}
});
// Create a new simulation
app.post('/metasim/:version/simulations', function(request, response) {
console.log(JSON.stringify(request.body));
var simulationUri = request.body.simulation_href;
console.log('Got main simulation path: ' + simulationUri);
var simulationId = simulationUri.split('/').slice(-1);
// Create the simulation object and return it.
var simulation = {
simulation_href: request.body.simulation_href};
db.collection('simulations').insert(simulation);
response.header('Location', url.format({
protocol: 'http',
hostname: request.host,
port: port,
pathname: request.originalUrl + '/' + simulationId + '/data'}));
response.send(201, {bodies:[{
radius:1,
xyz_position: {x:5, y:0, z:0},
xyz_velocity: {x:0, y:0, z:0}}]});
});
// Delete simulations
app.delete('/metasim/:version/simulations/:id', function(request, response) {
var version = request.params.version;
if (version == '1.0') {
var simulationId = request.params.id;
if (db.collection('simulations').find({_id:simulationId}).count() > 0) {
db.collection('simulations').remove({_id:simulationId});
response.send(204, null);
} else {
console.log('simulation ' + simulationId + ' not found');
response.send(404, 'simulation ' + simulationId + ' not found');
}
} else {
console.log('version ' + version + ' not found');
response.send(404, 'version ' + version + ' not found');
}
});
// Serve up simulation data
app.get('/metasim/:version/simulations/:id/data', function(request, response) {
response.send({bodies:[{
radius:1,
xyz_position: {x:5, y:0, z:0},
xyz_velocity: {x:0, y:0, z:0}}]});
});
});
app.listen(port, function() {
console.log("Listening on " + port);
});