geolocation api rate-limit: improve the delay mechanism to be sure to don't hit provider more than 1 time/s, add memory-cache to save response data.

This commit is contained in:
sedum
2023-01-19 01:29:24 +01:00
parent ebb3fcbd93
commit 6ee96fb07c
5 changed files with 115 additions and 67 deletions

View File

@@ -7,9 +7,6 @@ const { version } = require('../../../package.json')
const log = require('../../log')
const { Op, where, col, fn, cast } = require('sequelize')
const NOMINATIM_URL = 'https://nominatim.openstreetmap.org/search'
const PHOTON_URL = 'https://photon.komoot.io/api/'
const axios = require('axios')
module.exports = {
@@ -75,45 +72,6 @@ module.exports = {
// TOFIX: don't know why limit does not work
return res.json(places.slice(0, 10))
},
async _nominatim (req, res) {
const details = req.params.place_details
const countrycodes = res.locals.settings.geocoding_countrycodes || []
const geocoding_provider = res.locals.settings.geocoding_provider || NOMINATIM_URL
// ?limit=3&format=json&namedetails=1&addressdetails=1&q=
const ret = await axios.get(`${geocoding_provider}`, {
params: {
countrycodes: countrycodes.join(','),
q: details,
limit: 3,
format: 'json',
addressdetails: 1,
namedetails: 1,
},
headers: { 'User-Agent': `gancio ${version}` }
})
return res.json(ret.data)
},
async _photon (req, res) {
const details = req.params.place_details
const geocoding_provider = res.locals.settings.geocoding_provider || PHOTON_URL
const ret = await axios.get(`${geocoding_provider}`, {
params: {
q: details,
limit: 3,
},
headers: { 'User-Agent': `gancio ${version}` }
})
// console.log(ret)
return res.json(ret.data)
},
}
}