db-rest/index.js
Jannis R a47e20c1d4
Node 10+ 💥, db-hafas@3, update deps & dev deps
- db-stations@3
- hafas-client-health-check@2
- hafas-rest-api@3
- add pino-pretty@4 for debug logging
2020-05-01 20:13:45 +02:00

52 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict'
const {readFileSync} = require('fs')
const {join} = require('path')
const createHafas = require('db-hafas')
const createApi = require('hafas-rest-api')
const createHealthCheck = require('hafas-client-health-check')
const pkg = require('./package.json')
const stations = require('./routes/stations')
const station = require('./routes/station')
const docsAsMarkdown = readFileSync(join(__dirname, 'docs', 'index.md'), {encoding: 'utf8'})
const hafas = createHafas(pkg.name)
const berlinHbf = '8011160'
const healthCheck = createHealthCheck(hafas, berlinHbf)
const modifyRoutes = (routes) => {
routes['/stations'] = stations
routes['/stations/:id'] = station
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,
docsAsMarkdown,
docsLink: '/docs',
logging: true,
aboutPage: true,
etags: 'strong',
healthCheck,
modifyRoutes,
}
const api = createApi(hafas, config, () => {})
api.listen(config.port, (err) => {
const {logger} = api.locals
if (err) {
logger.error(err)
process.exit(1)
} else {
logger.info(`Listening on ${config.hostname}:${config.port}.`)
}
})