diff --git a/index.js b/index.js index 00bf075..50eaa86 100644 --- a/index.js +++ b/index.js @@ -7,9 +7,8 @@ const createApi = require('hafas-rest-api') const createHealthCheck = require('hafas-client-health-check') const pkg = require('./package.json') -const stations = require('./lib/stations') -const allStations = require('./lib/all-stations') -const station = require('./lib/station') +const stations = require('./routes/stations') +const station = require('./routes/station') const docsAsMarkdown = readFileSync(join(__dirname, 'docs', 'index.md'), {encoding: 'utf8'}) @@ -18,6 +17,12 @@ 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, @@ -25,26 +30,23 @@ const config = { description: pkg.description, homepage: pkg.homepage, version: pkg.version, + docsAsMarkdown, docsLink: '/docs', logging: true, - healthCheck, aboutPage: true, - docsAsMarkdown + etags: 'strong', + healthCheck, + modifyRoutes, } -const attachAdditionalHandlers = (api) => { - api.get('/stations', stations) - api.get('/stations/all', allStations) - api.get('/stations/:id', station) -} - -const api = createApi(hafas, config, attachAdditionalHandlers) +const api = createApi(hafas, config, () => {}) api.listen(config.port, (err) => { + const {logger} = api.locals if (err) { - console.error(err) - process.exitCode = 1 + logger.error(err) + process.exit(1) } else { - console.info(`Listening on ${config.hostname}:${config.port}.`) + logger.info(`Listening on ${config.hostname}:${config.port}.`) } }) diff --git a/package.json b/package.json index 5fae7ec..1674b9b 100644 --- a/package.json +++ b/package.json @@ -19,18 +19,21 @@ "db" ], "engines": { - "node": ">=6" + "node": ">=10" }, "dependencies": { "cli-native": "^1.0.0", - "db-hafas": "^3.0.1", - "db-stations": "^2.4.0", - "db-stations-autocomplete": "^2.1.0", + "db-hafas": "^5.0.2", + "db-stations": "^3.0.0", + "db-stations-autocomplete": "^2.2.0", "etag": "^1.8.1", - "hafas-client-health-check": "^1.0.1", - "hafas-rest-api": "^1.2.1", + "hafas-client-health-check": "^2.1.1", + "hafas-rest-api": "^3.3.0", "serve-buffer": "^2.0.0" }, + "devDependencies": { + "pino-pretty": "^4.0.0" + }, "scripts": { "start": "node index.js" } diff --git a/routes/stations.js b/routes/stations.js index 33cc3d9..b67a15f 100644 --- a/routes/stations.js +++ b/routes/stations.js @@ -109,4 +109,27 @@ const stationsRoute = (req, res, next) => { .catch(next) } +stationsRoute.queryParameters = { + query: { + description: 'Find stations by name using [`db-stations-autocomplete`](https://npmjs.com/package/db-stations-autocomplete).', + type: 'string', + defaultStr: '–', + }, + limit: { + description: '*If `query` is used:* Return at most `n` stations.', + type: 'number', + default: 3, + }, + fuzzy: { + description: '*If `query` is used:* Find stations despite typos.', + type: 'boolean', + default: false, + }, + completion: { + description: '*If `query` is used:* Autocomplete stations.', + type: 'boolean', + default: true, + }, +} + module.exports = stationsRoute