db-rest/index.js

51 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('./lib/stations')
const allStations = require('./lib/all-stations')
2018-01-09 16:51:42 +01:00
const station = require('./lib/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
2017-12-16 09:06:35 +01:00
const config = {
2018-01-09 16:42:30 +01:00
hostname: process.env.HOSTNAME || '2.db.transport.rest',
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,
2019-02-04 16:09:30 +01:00
docsLink: '/docs',
2018-03-07 02:33:49 +01:00
logging: true,
2018-10-25 21:49:17 +02:00
healthCheck,
2019-02-04 16:09:30 +01:00
aboutPage: true,
docsAsMarkdown
2017-12-16 09:06:35 +01:00
}
2017-10-23 23:13:53 +02:00
2018-10-25 21:49:17 +02:00
const attachAdditionalHandlers = (api) => {
2018-01-09 16:42:30 +01:00
api.get('/stations', stations)
api.get('/stations/all', allStations)
2018-01-09 16:51:42 +01:00
api.get('/stations/:id', station)
2018-10-25 21:49:17 +02:00
}
const api = createApi(hafas, config, attachAdditionalHandlers)
2017-12-16 09:06:35 +01:00
api.listen(config.port, (err) => {
2017-10-23 23:13:53 +02:00
if (err) {
console.error(err)
2017-12-16 09:06:35 +01:00
process.exitCode = 1
2017-10-23 23:13:53 +02:00
} else {
2017-12-16 09:06:35 +01:00
console.info(`Listening on ${config.hostname}:${config.port}.`)
2017-10-23 23:13:53 +02:00
}
})