2020-05-01 20:12:24 +02:00
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
|
|
const createHafas = require('db-hafas')
|
|
|
|
|
const createApi = require('hafas-rest-api')
|
|
|
|
|
const createHealthCheck = require('hafas-client-health-check')
|
2021-11-28 13:38:16 +01:00
|
|
|
|
const Redis = require('ioredis')
|
2020-05-01 20:15:27 +02:00
|
|
|
|
const withCache = require('cached-hafas-client')
|
|
|
|
|
const redisStore = require('cached-hafas-client/stores/redis')
|
2020-10-02 18:33:38 +02:00
|
|
|
|
const {join: pathJoin} = require('path')
|
|
|
|
|
const serveStatic = require('serve-static')
|
2020-05-01 20:12:24 +02:00
|
|
|
|
const pkg = require('./package.json')
|
|
|
|
|
const stations = require('./routes/stations')
|
|
|
|
|
const station = require('./routes/station')
|
|
|
|
|
|
2020-10-02 18:33:38 +02:00
|
|
|
|
const docsRoot = pathJoin(__dirname, 'docs')
|
|
|
|
|
|
2020-05-01 20:12:24 +02:00
|
|
|
|
const berlinHbf = '8011160'
|
2020-05-01 20:15:27 +02:00
|
|
|
|
|
|
|
|
|
let hafas = createHafas(pkg.name)
|
|
|
|
|
let healthCheck = createHealthCheck(hafas, berlinHbf)
|
|
|
|
|
|
|
|
|
|
if (process.env.REDIS_URL) {
|
2021-11-28 13:38:16 +01:00
|
|
|
|
const redis = new Redis(process.env.REDIS_URL || null)
|
|
|
|
|
hafas = withCache(hafas, redisStore(redis), {
|
|
|
|
|
cachePeriods: {
|
|
|
|
|
locations: 6 * 60 * 60 * 1000, // 6h
|
|
|
|
|
},
|
2020-05-01 20:15:27 +02:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const checkHafas = healthCheck
|
|
|
|
|
const checkRedis = () => new Promise((resolve, reject) => {
|
|
|
|
|
setTimeout(reject, 1000, new Error('didn\'t receive a PONG'))
|
2021-11-28 13:38:16 +01:00
|
|
|
|
redis.ping().then(
|
|
|
|
|
res => resolve(res === 'PONG'),
|
|
|
|
|
reject,
|
|
|
|
|
)
|
2020-05-01 20:15:27 +02:00
|
|
|
|
})
|
|
|
|
|
healthCheck = async () => (
|
|
|
|
|
(await checkHafas()) === true &&
|
|
|
|
|
(await checkRedis()) === true
|
|
|
|
|
)
|
|
|
|
|
}
|
2020-05-01 20:12:24 +02:00
|
|
|
|
|
|
|
|
|
const modifyRoutes = (routes) => {
|
|
|
|
|
routes['/stations/:id'] = station
|
|
|
|
|
routes['/stations'] = stations
|
|
|
|
|
return routes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
|
hostname: process.env.HOSTNAME || 'localhost',
|
|
|
|
|
port: process.env.PORT ? parseInt(process.env.PORT) : 3000,
|
|
|
|
|
name: pkg.name,
|
|
|
|
|
description: pkg.description,
|
|
|
|
|
homepage: pkg.homepage,
|
|
|
|
|
version: pkg.version,
|
|
|
|
|
docsLink: 'https://github.com/derhuerst/db-rest/blob/5/docs/readme.md',
|
2021-02-04 19:24:13 +01:00
|
|
|
|
openapiSpec: true,
|
2020-05-01 20:12:24 +02:00
|
|
|
|
logging: true,
|
2020-10-02 18:33:38 +02:00
|
|
|
|
aboutPage: false,
|
2020-05-01 20:12:24 +02:00
|
|
|
|
etags: 'strong',
|
2020-10-04 13:46:14 +02:00
|
|
|
|
csp: `default-src 'none' style-src 'self' 'unsafe-inline' img-src https:`,
|
2020-05-01 20:12:24 +02:00
|
|
|
|
healthCheck,
|
|
|
|
|
modifyRoutes,
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-02 18:33:38 +02:00
|
|
|
|
const api = createApi(hafas, config, (api) => {
|
|
|
|
|
api.use('/', serveStatic(docsRoot, {
|
|
|
|
|
extensions: ['html', 'htm'],
|
|
|
|
|
}))
|
|
|
|
|
})
|
2020-05-01 20:12:24 +02:00
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
config,
|
|
|
|
|
api,
|
|
|
|
|
}
|