-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
app.js
84 lines (77 loc) · 2.25 KB
/
app.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
'use strict';
var express = require('express');
var cors = require('cors');
var app = express();
var geocoder = require('./index.js');
var isGeocodeInitialized = false;
app.use(cors());
app.get('/healthcheck', function (req, res) {
return res.status(200).send('OK');
});
app.get('/deep-healthcheck', function (req, res) {
if (isGeocodeInitialized) {
return res.status(200).send('OK');
} else {
return res.status(503).send('Not ready yet.');
}
});
app.get('/geocode', function (req, res) {
if (!isGeocodeInitialized) {
return res.status(503).send('Not ready yet.');
}
var lat = req.query.latitude || false;
var lon = req.query.longitude || false;
var maxResults = req.query.maxResults || 1;
if (!lat || !lon) {
return res.status(400).send('Bad Request');
}
var points = [];
if (Array.isArray(lat) && Array.isArray(lon)) {
if (lat.length !== lon.length) {
return res.status(400).send('Bad Request');
}
for (var i = 0, lenI = lat.length; i < lenI; i++) {
points[i] = { latitude: lat[i], longitude: lon[i] };
}
} else {
points[0] = { latitude: lat, longitude: lon };
}
geocoder.lookUp(points, maxResults, function (err, addresses) {
if (err) {
return res.status(500).send(err);
}
return res.send(addresses);
});
});
var port = Number(process.env.PORT || 3000);
app.listen(port, function () {
console.log('Local reverse geocoder listening on port ' + port);
console.log('Initializing Geocoder…');
console.log(
'(This may take a long time and will download ~2.29GB worth of data by default.)'
);
geocoder.init(
{
citiesFileOverride: 'cities500',
load: {
admin1: true,
admin2: true,
admin3And4: true,
alternateNames: true,
},
countries: [],
},
function () {
console.log('Geocoder initialized and ready.');
console.log('Endpoints:');
console.log(`- http://localhost:${port}/healthcheck`);
console.log(`- http://localhost:${port}/deep-healthcheck`);
console.log(`- http://localhost:${port}/geocode`);
console.log('Examples:');
console.log(
`- http://localhost:${port}/geocode?latitude=54.6875248&longitude=9.7617254`
);
isGeocodeInitialized = true;
}
);
});