/
service.js
107 lines (89 loc) · 3.05 KB
/
service.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
var fs=require('fs');
var http = require('http');
var MAX_RETURN_SET = 300;
// Load the dataset
if (process.argv[2] == null)
{
console.error('Provide a data-set filename');
return;
}
var fileBuf=JSON.parse(fs.readFileSync(process.argv[2]));
var indexHTML=fs.readFileSync('./index.html'); // Serve this for / requests
console.log('items loaded:' + fileBuf.length);
http.createServer(onRequest).listen(process.env.PORT);
function onRequest(client_req, client_res) {
console.log('serve: ' + client_req.url);
// When the request is / serve index.html otherwise assume an API request
// API is LAT=X (max lat); lat=x; LON=Y; lon=y
// Anthing missing/not numeric just return the empty
// array of items -- no error
var result = {};
var params = parseParams(client_req.url);
if (params.good) {
result = filter(fileBuf, params);
client_res.writeHead(200,{'Content-Type':'application/json'});
client_res.write(JSON.stringify(result));
client_res.end();
} else {
if (client_req.url == "/") {
client_res.writeHead(200,{'Content-Type':'text/html'});
client_res.write(indexHTML);
client_res.end();
} else {
result.params = params;
client_res.writeHead(200,{'Content-Type':'application/json'});
client_res.write(JSON.stringify(result));
client_res.end();
}
}
}
var parseParams=function(url) {
url=url.replace("/", "");
var parts=url.split("&");
var LAT=false;
var lat=false;
var LON=false;
var lon=false;
var result = {};
result.good = false;
if (parts.length == 4) {
for (p in parts) {
if (parts[p].startsWith("LAT=")) { LAT=true;
result.LAT=parseFloat(parts[p].replace("LAT=",""));
}
if (parts[p].startsWith("lat=")) { lat=true;
result.lat=parseFloat(parts[p].replace("lat=",""));
}
if (parts[p].startsWith("LON=")) { LON=true;
result.LON=parseFloat(parts[p].replace("LON=",""));
}
if (parts[p].startsWith("lon=")) { lon=true;
result.lon=parseFloat(parts[p].replace("lon=",""));
}
}
if (LAT && lat && LON && lon) {
result.good = (!isNaN(result.lon) && !isNaN(result.LON) && !isNaN(result.LAT) && !isNaN(result.lat));
}
}
return result;
}
var filter=function(set, filter){
var results = {};
var found = [];
var total = 0;
if (filter.good) {
for (d in set) {
try {
if (set[d].position.lat <= filter.LAT && set[d].position.lat >= filter.lat &&
set[d].position.lon <= filter.LON && set[d].position.lon >= filter.lon) {
if (total < MAX_RETURN_SET) { found.push(set[d]); }
total++;
}
} catch (e) {console.log('element failed' + d + ':' + JSON.stringify(e));}
}
}
results.params = filter;
results.found = found;
results.total = total;
return results;
}