parent
ab9f1af672
commit
509504c7c8
4 changed files with 38 additions and 3 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -6,3 +6,4 @@ node_modules
|
|||
npm-debug.log
|
||||
|
||||
/package-lock.json
|
||||
/dump.rdb
|
||||
|
|
32
api.js
32
api.js
|
@ -3,15 +3,41 @@
|
|||
const createHafas = require('db-hafas')
|
||||
const createApi = require('hafas-rest-api')
|
||||
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 stations = require('./routes/stations')
|
||||
const station = require('./routes/station')
|
||||
|
||||
const hafas = createHafas(pkg.name)
|
||||
|
||||
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) => {
|
||||
routes['/stations/:id'] = station
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
"node": ">=10"
|
||||
},
|
||||
"dependencies": {
|
||||
"cached-hafas-client": "^3.1.1",
|
||||
"cli-native": "^1.0.0",
|
||||
"db-hafas": "^5.0.2",
|
||||
"db-stations": "^3.0.0",
|
||||
|
@ -29,6 +30,7 @@
|
|||
"etag": "^1.8.1",
|
||||
"hafas-client-health-check": "^2.1.1",
|
||||
"hafas-rest-api": "^3.3.0",
|
||||
"redis": "^3.0.2",
|
||||
"serve-buffer": "^2.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
|
||||
## 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
|
||||
|
||||
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
|
||||
```
|
||||
|
||||
*Note:* The Docker image does not contain the Redis server.
|
||||
|
||||
### manually
|
||||
|
||||
```shell
|
||||
|
@ -30,6 +34,8 @@ git clone https://github.com/derhuerst/db-rest.git
|
|||
cd db-rest
|
||||
git checkout 5
|
||||
npm install --production
|
||||
|
||||
redis-server &
|
||||
npm start
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in a new issue