db-rest/index.js

47 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-10-23 23:13:53 +02:00
'use strict'
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')
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
2018-10-25 21:49:17 +02:00
const hafas = createHafas(pkg.name)
const healthCheck = () => {
return hafas.station('8011306')
.then(st => !!st)
}
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',
2017-12-16 09:06:35 +01:00
port: process.env.PORT || 3000,
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,
docsLink: 'https://github.com/derhuerst/db-rest/blob/2/docs/index.md',
2018-03-07 02:33:49 +01:00
logging: true,
2018-10-25 21:49:17 +02:00
healthCheck,
2018-03-07 02:33:49 +01:00
aboutPage: true
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
}
})