-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb.js
More file actions
162 lines (147 loc) · 5.68 KB
/
web.js
File metadata and controls
162 lines (147 loc) · 5.68 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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/terrainReferenceEngine';
var port = process.env.PORT || 5001;
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 simulationUrl = request.body.simulation_href;
var simulationPathname = url.parse(simulationUrl).pathname;
console.log('Got main simulation path: ' + simulationUrl);
var simulationId = simulationUrl.split('/').slice(-1);
// Grab the simulation object from MetaSim
// It is already filled out with bodies information`
// Grab the simulation object from MetaSim
http.get(simulationUrl, function(res) {
var simulationHref = res.headers.location;
console.log('response status: ' + res.status);
console.log('response headers: ' + JSON.stringify(res.headers));
console.log('Engine simulation created at ' + simulationHref);
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
// only merge in body if an response was returned
if (body != '') {
console.log('got simulation from MetaSim: ' + body);
var simulation = JSON.parse(body);
for(var i in simulation.bodies) {
// add a terrain link for each body
console.log('adding terrain link for body');
dataUrl = simulationPathname + '/' + i + '/terrain/data.jpg';
if (simulation.bodies[i].links === undefined) {
simulation.bodies[i].links = [];
}
simulation.bodies[i].links.push({
rel: '/rel/world_texture',
href: dataUrl,
method: 'GET'});
simulation.bodies[i].links.push({
rel: '/rel/world_texture_night',
href: dataUrl,
method: 'GET'});
simulation.forwardedPaths.push({
originalUrl: dataUrl,
dest: url.format({
protocol: 'http',
hostname: request.host,
port: port,
pathname: dataUrl})});
// keep a record of the simulation locally
db.collection('simulations').insert(simulation);
response.header('Location', url.format({
protocol: 'http',
hostname: request.host,
port: port,
pathname: request.originalUrl + '/' + simulationId}));
console.log('returning simulation: ' + JSON.stringify(simulation));
response.send(201, simulation);
}
}
});
});
});
// 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/:bodyid/terrain/data.jpg', function(request, response) {
response.sendfile('terrain.jpg');
});
// default handler
app.use(function(request, response) {
response.send(404, null);
});
});
app.listen(port, function() {
console.log("Listening on " + port);
});