caching using cached-hafas-client

see also derhuerst/hvv-rest@8db740b
This commit is contained in:
Jannis R 2020-05-01 20:15:27 +02:00
parent ab9f1af672
commit 509504c7c8
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
4 changed files with 38 additions and 3 deletions

1
.gitignore vendored
View file

@ -6,3 +6,4 @@ node_modules
npm-debug.log npm-debug.log
/package-lock.json /package-lock.json
/dump.rdb

32
api.js
View file

@ -3,15 +3,41 @@
const createHafas = require('db-hafas') const createHafas = require('db-hafas')
const createApi = require('hafas-rest-api') const createApi = require('hafas-rest-api')
const createHealthCheck = require('hafas-client-health-check') const createHealthCheck = require('hafas-client-health-check')
const {createClient: createRedis} = require('redis')
const withCache = require('cached-hafas-client')
const redisStore = require('cached-hafas-client/stores/redis')
const pkg = require('./package.json') const pkg = require('./package.json')
const stations = require('./routes/stations') const stations = require('./routes/stations')
const station = require('./routes/station') const station = require('./routes/station')
const hafas = createHafas(pkg.name)
const berlinHbf = '8011160' const berlinHbf = '8011160'
const healthCheck = createHealthCheck(hafas, berlinHbf)
let hafas = createHafas(pkg.name)
let healthCheck = createHealthCheck(hafas, berlinHbf)
if (process.env.REDIS_URL) {
const redis = createRedis({
url: process.env.REDIS_URL,
})
redis.on('error', (err) => {
api.locals.logger.error(err)
})
hafas = withCache(hafas, redisStore(redis))
const checkHafas = healthCheck
const checkRedis = () => new Promise((resolve, reject) => {
setTimeout(reject, 1000, new Error('didn\'t receive a PONG'))
redis.ping((err, res) => {
if (err) reject(err)
else resolve(res === 'PONG')
})
})
healthCheck = async () => (
(await checkHafas()) === true &&
(await checkRedis()) === true
)
}
const modifyRoutes = (routes) => { const modifyRoutes = (routes) => {
routes['/stations/:id'] = station routes['/stations/:id'] = station

View file

@ -22,6 +22,7 @@
"node": ">=10" "node": ">=10"
}, },
"dependencies": { "dependencies": {
"cached-hafas-client": "^3.1.1",
"cli-native": "^1.0.0", "cli-native": "^1.0.0",
"db-hafas": "^5.0.2", "db-hafas": "^5.0.2",
"db-stations": "^3.0.0", "db-stations": "^3.0.0",
@ -29,6 +30,7 @@
"etag": "^1.8.1", "etag": "^1.8.1",
"hafas-client-health-check": "^2.1.1", "hafas-client-health-check": "^2.1.1",
"hafas-rest-api": "^3.3.0", "hafas-rest-api": "^3.3.0",
"redis": "^3.0.2",
"serve-buffer": "^2.0.0" "serve-buffer": "^2.0.0"
}, },
"devDependencies": { "devDependencies": {

View file

@ -15,6 +15,8 @@
## installing & running ## installing & running
`bvg-rest` expects a [Redis](https://redis.io/) server running on `127.0.0.1:6379` (default port), but you can set the `REDIS_URL` environment variable to change this.
### via Docker ### via Docker
A Docker image [is available as `derhuerst/db-rest:5`](https://hub.docker.com/r/derhuerst/db-rest:5). A Docker image [is available as `derhuerst/db-rest:5`](https://hub.docker.com/r/derhuerst/db-rest:5).
@ -23,6 +25,8 @@ A Docker image [is available as `derhuerst/db-rest:5`](https://hub.docker.com/r/
docker run -d -p 3000:3000 derhuerst/db-rest:5 docker run -d -p 3000:3000 derhuerst/db-rest:5
``` ```
*Note:* The Docker image does not contain the Redis server.
### manually ### manually
```shell ```shell
@ -30,6 +34,8 @@ git clone https://github.com/derhuerst/db-rest.git
cd db-rest cd db-rest
git checkout 5 git checkout 5
npm install --production npm install --production
redis-server &
npm start npm start
``` ```