2022-12-19 13:23:13 +01:00
|
|
|
// todo: use import assertions once they're supported by Node.js & ESLint
|
|
|
|
// https://github.com/tc39/proposal-import-assertions
|
|
|
|
import {createRequire} from 'node:module'
|
|
|
|
const require = createRequire(import.meta.url)
|
|
|
|
|
|
|
|
import {dirname, join as pathJoin} from 'node:path'
|
|
|
|
import {fileURLToPath} from 'node:url'
|
2022-12-21 14:30:00 +01:00
|
|
|
import {createDbHafas as createHafas} from 'db-hafas'
|
|
|
|
import {createHafasRestApi} from 'hafas-rest-api'
|
2022-12-19 13:23:13 +01:00
|
|
|
import createHealthCheck from 'hafas-client-health-check'
|
|
|
|
import Redis from 'ioredis'
|
2022-12-21 14:30:00 +01:00
|
|
|
import {createCachedHafasClient} from 'cached-hafas-client'
|
|
|
|
import {createRedisStore} from 'cached-hafas-client/stores/redis.js'
|
2022-12-19 13:23:13 +01:00
|
|
|
import serveStatic from 'serve-static'
|
|
|
|
import {parseBoolean} from 'hafas-rest-api/lib/parse.js'
|
|
|
|
import {loyaltyCardParser} from './lib/loyalty-cards.js'
|
|
|
|
import {route as stations} from './routes/stations.js'
|
|
|
|
import {route as station} from './routes/station.js'
|
2020-05-01 20:12:24 +02:00
|
|
|
|
|
|
|
const pkg = require('./package.json')
|
|
|
|
|
2022-12-19 13:23:13 +01:00
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
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)
|
2022-12-21 14:30:00 +01:00
|
|
|
hafas = createCachedHafasClient(hafas, createRedisStore(redis), {
|
2021-11-28 13:38:16 +01:00
|
|
|
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
|
|
|
|
2022-04-02 17:02:25 +02:00
|
|
|
const mapRouteParsers = (route, parsers) => {
|
|
|
|
if (route !== 'journeys') return parsers
|
|
|
|
return {
|
|
|
|
...parsers,
|
|
|
|
loyaltyCard: loyaltyCardParser,
|
|
|
|
firstClass: {
|
|
|
|
description: 'Search for first-class options?',
|
|
|
|
type: 'boolean',
|
|
|
|
default: 'false',
|
|
|
|
parse: parseBoolean,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const modifyRoutes = (routes, hafas, config) => {
|
2020-05-01 20:12:24 +02:00
|
|
|
routes['/stations/:id'] = station
|
|
|
|
routes['/stations'] = stations
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
|
|
|
const config = {
|
2022-04-03 14:15:36 +02:00
|
|
|
hostname: process.env.HOSTNAME || 'localhost',
|
|
|
|
port: process.env.PORT ? parseInt(process.env.PORT) : 3000,
|
2020-05-01 20:12:24 +02:00
|
|
|
name: pkg.name,
|
|
|
|
description: pkg.description,
|
|
|
|
homepage: pkg.homepage,
|
|
|
|
version: pkg.version,
|
2022-12-19 13:18:25 +01:00
|
|
|
docsLink: 'https://github.com/derhuerst/db-rest/blob/6/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,
|
2022-04-02 17:02:25 +02:00
|
|
|
mapRouteParsers,
|
2020-05-01 20:12:24 +02:00
|
|
|
modifyRoutes,
|
|
|
|
}
|
|
|
|
|
2022-12-21 14:30:00 +01:00
|
|
|
const api = await createHafasRestApi(hafas, config, (api) => {
|
2020-10-02 18:33:38 +02:00
|
|
|
api.use('/', serveStatic(docsRoot, {
|
|
|
|
extensions: ['html', 'htm'],
|
|
|
|
}))
|
|
|
|
})
|
2020-05-01 20:12:24 +02:00
|
|
|
|
2022-12-19 13:23:13 +01:00
|
|
|
export {
|
2022-04-03 13:44:22 +02:00
|
|
|
hafas,
|
2020-05-01 20:12:24 +02:00
|
|
|
config,
|
|
|
|
api,
|
|
|
|
}
|