db-rest/index.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-10-23 23:13:53 +02:00
'use strict'
2019-02-04 16:09:30 +01:00
const {readFileSync} = require('fs')
const {join} = require('path')
2018-10-25 21:49:17 +02:00
const createHafas = require('db-hafas')
2017-12-16 09:06:35 +01:00
const createApi = require('hafas-rest-api')
2018-11-21 00:00:55 +01:00
const createHealthCheck = require('hafas-client-health-check')
2017-10-23 23:13:53 +02:00
2017-12-16 09:06:35 +01:00
const pkg = require('./package.json')
const stations = require('./routes/stations')
const station = require('./routes/station')
2017-10-23 23:13:53 +02:00
2019-02-04 16:09:30 +01:00
const docsAsMarkdown = readFileSync(join(__dirname, 'docs', 'index.md'), {encoding: 'utf8'})
2018-10-25 21:49:17 +02:00
const hafas = createHafas(pkg.name)
2018-11-21 00:00:55 +01:00
const berlinHbf = '8011160'
const healthCheck = createHealthCheck(hafas, berlinHbf)
2018-10-25 21:49:17 +02:00
const modifyRoutes = (routes) => {
routes['/stations'] = stations
routes['/stations/:id'] = station
return routes
}
2017-12-16 09:06:35 +01:00
const config = {
hostname: process.env.HOSTNAME || 'localhost',
2018-10-25 22:56:45 +02:00
port: process.env.PORT ? parseInt(process.env.PORT) : 3000,
2017-12-16 09:06:35 +01:00
name: pkg.name,
2018-03-07 02:33:49 +01:00
description: pkg.description,
2018-01-09 16:42:30 +01:00
homepage: pkg.homepage,
2018-10-25 21:49:17 +02:00
version: pkg.version,
docsAsMarkdown,
2019-02-04 16:09:30 +01:00
docsLink: '/docs',
2018-03-07 02:33:49 +01:00
logging: true,
2019-02-04 16:09:30 +01:00
aboutPage: true,
etags: 'strong',
healthCheck,
modifyRoutes,
2018-10-25 21:49:17 +02:00
}
const api = createApi(hafas, config, () => {})
2017-12-16 09:06:35 +01:00
api.listen(config.port, (err) => {
const {logger} = api.locals
2017-10-23 23:13:53 +02:00
if (err) {
logger.error(err)
process.exit(1)
2017-10-23 23:13:53 +02:00
} else {
logger.info(`Listening on ${config.hostname}:${config.port}.`)
2017-10-23 23:13:53 +02:00
}
})