Commit 08dbe20a authored by Christian Paul's avatar Christian Paul 🦊

Use superagent for requests and make the API URLs more configurable

parent c5f16af8
Pipeline #63183649 failed with stage
in 1 minute and 5 seconds
#PORT=3000
#NOMINATIM_URL=https://nominatim.openstreetmap.org/
#OVERPASS_URL=http://overpass-api.de/api/
#USER_AGENT=https://gitlab.com/webthings/webthing-osm/
# WebThing: OpenStreetMap
This server exposes Points of interest on OpenStreetMap as WebThings.
## Server setup
## Configuration
Copy the file `.env-sample` to `.env` and configure the server and integrations.
* `PORT` – The TCP port this server will listen to. (default: 3000)
* `NOMINATIM_URL` – A URL to send (Nominatim)[https://wiki.openstreetmap.org/wiki/Nominatim] requests to
* `OVERPASS_URL` – A URL to send (Overpass)[https://wiki.openstreetmap.org/wiki/Overpass_API] requests to
* `USER_AGENT` – Define a user agent for all requests to external APIs. They commonly expect you to specify a string unique to your project / server.
## First start
```bash
npm ci # Installs dependencies
npm start # Runs the server
......
......@@ -682,8 +682,7 @@
"asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=",
"dev": true
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
},
"atob": {
"version": "2.1.2",
......@@ -1065,7 +1064,6 @@
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"dev": true,
"requires": {
"delayed-stream": "~1.0.0"
}
......@@ -1078,8 +1076,7 @@
"component-emitter": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.0.tgz",
"integrity": "sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg==",
"dev": true
"integrity": "sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg=="
},
"concat-map": {
"version": "0.0.1",
......@@ -1127,6 +1124,11 @@
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
"integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
},
"cookiejar": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.2.tgz",
"integrity": "sha512-Mw+adcfzPxcPeI+0WlvRrr/3lGVO0bD75SxX6811cxSh1Wbxx7xZBGK1eVtDf6si8rg2lhnUjsVLMFMfbRIuwA=="
},
"copy-descriptor": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/copy-descriptor/-/copy-descriptor-0.1.1.tgz",
......@@ -1283,8 +1285,7 @@
"delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=",
"dev": true
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk="
},
"depd": {
"version": "1.1.2",
......@@ -1317,6 +1318,11 @@
"webidl-conversions": "^4.0.2"
}
},
"dotenv": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.0.0.tgz",
"integrity": "sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg=="
},
"ecc-jsbn": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz",
......@@ -1764,13 +1770,17 @@
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.3.tgz",
"integrity": "sha512-1lLKB2Mu3aGP1Q/2eCOx0fNbRMe7XdwktwOruhfqqd0rIJWwN4Dh+E3hrPSlDCXnSR7UtZ1N38rVXm+6+MEhJQ==",
"dev": true,
"requires": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.6",
"mime-types": "^2.1.12"
}
},
"formidable": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/formidable/-/formidable-1.2.1.tgz",
"integrity": "sha512-Fs9VRguL0gqGHkXS5GQiMCr1VhZBxz0JnJs4JmMp/2jL18Fmbzvv7vOFRU+U8TBkHEE/CX1qDXzJplVULgsLeg=="
},
"forwarded": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz",
......@@ -5103,6 +5113,63 @@
"resolved": "https://registry.npmjs.org/suncalc/-/suncalc-1.8.0.tgz",
"integrity": "sha1-HZiYEJVjB4dQ9JlKlZ5lTYdqy/U="
},
"superagent": {
"version": "5.0.5",
"resolved": "https://registry.npmjs.org/superagent/-/superagent-5.0.5.tgz",
"integrity": "sha512-5fnihpNqJfDJHxeIkqlXKFDOW5t4WUFV8Ba1erxdWT0RySqjgAW6Hska0xNYEpAfaoMzubn77GROm/XlkssOoQ==",
"requires": {
"component-emitter": "^1.3.0",
"cookiejar": "^2.1.2",
"debug": "^4.1.1",
"form-data": "^2.3.3",
"formidable": "^1.2.1",
"methods": "^1.1.2",
"mime": "^2.4.2",
"qs": "^6.7.0",
"readable-stream": "^3.3.0",
"semver": "^6.0.0"
},
"dependencies": {
"debug": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
"integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
"requires": {
"ms": "^2.1.1"
}
},
"mime": {
"version": "2.4.3",
"resolved": "https://registry.npmjs.org/mime/-/mime-2.4.3.tgz",
"integrity": "sha512-QgrPRJfE+riq5TPZMcHZOtm8c6K/yYrMbKIoRfapfiGLxS8OTeIfRhUGW5LU7MlRa52KOAGCfUNruqLrIBvWZw=="
},
"ms": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz",
"integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg=="
},
"qs": {
"version": "6.7.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",
"integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ=="
},
"readable-stream": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.3.0.tgz",
"integrity": "sha512-EsI+s3k3XsW+fU8fQACLN59ky34AZ14LoeVZpYwmZvldCFo0r0gnelwF2TcMjLor/BTL5aDJVBMkss0dthToPw==",
"requires": {
"inherits": "^2.0.3",
"string_decoder": "^1.1.1",
"util-deprecate": "^1.0.1"
}
},
"semver": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/semver/-/semver-6.1.0.tgz",
"integrity": "sha512-kCqEOOHoBcFs/2Ccuk4Xarm/KiWRSLEX9CAZF8xkJ6ZPlIoTZ8V5f7J16vYLJqDbR7KrxTJpR2lqjIEm2Qx9cQ=="
}
}
},
"supports-color": {
"version": "5.5.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz",
......
......@@ -14,16 +14,18 @@
"author": "Christian Paul",
"license": "AGPL-3.0-or-later",
"scripts": {
"start": "node src/app.js",
"start": "node src/main.js",
"test": "jest",
"test:watch": "jest --watch"
},
"main": "src/webthing.js",
"dependencies": {
"dotenv": "^8.0.0",
"express": "^4.16.4",
"geo-tz": "^5.0.4",
"luxon": "^1.15.0",
"opening_hours": "^3.5.0"
"opening_hours": "^3.5.0",
"superagent": "^5.0.5"
},
"devDependencies": {
"jest": "^24.8.0",
......
......@@ -21,4 +21,4 @@ const corsWildcard = (req, res, next) => {
app.use('/osm', corsWildcard, serverOsm);
app.listen(3000);
module.exports = app;
require('dotenv').config();
const app = require('./app.js');
const port = process.env.PORT || 3000;
app.listen(port);
console.log(`Listing on port ${port}`);
'use strict';
const https = require('https');
const superagent = require('superagent');
function askNominatim(elementIds) {
return new Promise((resolve) => {
// An object of options to indicate where to post to
var options = {
host: process.env.NOMINATIM_URL || 'nominatim.openstreetmap.org',
port: '443',
path: `/lookup?format=json&osm_ids=${elementIds.join(',')}`,
method: 'GET',
headers: {
'User-Agent': 'https://gitlab.com/webthings/webthing-osm/',
}
};
// Set up the request
var post_req = https.request(options, function(res) {
let data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
resolve(data);
});
});
post_req.end();
});
return superagent
.get(`${process.env.NOMINATIM_URL}/lookup?format=json&osm_ids=${elementIds.join(',')}`)
.set('User-Agent', 'https://gitlab.com/webthings/webthing-osm/')
.set('Accept', 'application/json');
}
async function getElementById(type, id) {
if (!['node', 'way', 'relation'].includes(type)) throw new Error(`Unknown type: ${type}`)
const elementId = `${type.charAt(0).toUpperCase()}${id}`;
const result = JSON.parse(await askNominatim([elementId]));
const element = result.find((element) => (
const result = await askNominatim([elementId]);
const element = result.body.find((element) => (
element.osm_id === id && element.osm_type === type
));
return element;
......
'use strict';
const querystring = require('querystring');
const http = require('http');
const superagent = require('superagent');
function askOverpass(codestring) {
return new Promise((resolve) => {
// Build the post string from an object
var post_data = querystring.stringify({
'data' : codestring,
});
// An object of options to indicate where to post to
var post_options = {
host: process.env.OVERPASS_URL || 'overpass-api.de',
port: '80',
path: '/api/interpreter',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data),
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
let data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
resolve(data);
});
});
// post the data
post_req.write(post_data);
post_req.end();
});
return superagent
.post(`${process.env.OVERPASS_URL}/interpreter`)
.send(codestring)
.set('User-Agent', 'https://gitlab.com/webthings/webthing-osm/')
.set('Accept', 'application/json');
}
async function getElementById(type, id) {
......@@ -46,8 +16,8 @@ async function getElementById(type, id) {
(${type}(${id}););
out body;>;out skel qt;
`;
const result = JSON.parse(await askOverpass(query));
const element = result.elements.find((element) => (
const result = await askOverpass(query);
const element = result.data.elements.find((element) => (
element.id === id && element.type === type
));
return element;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment