From 8fb85942582ba7f292ee2ae5d42a5ec96811c8d2 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Mon, 23 Oct 2017 23:13:53 +0200 Subject: [PATCH] init from vbb-rest --- .editorconfig | 13 ++++++++++++ .gitignore | 6 ++++++ Dockerfile | 13 ++++++++++++ api.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 18 ++++++++++++++++ license.md | 5 +++++ package.json | 39 +++++++++++++++++++++++++++++++++++ readme.md | 22 ++++++++++++++++++++ 8 files changed, 173 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 api.js create mode 100644 index.js create mode 100644 license.md create mode 100644 package.json create mode 100644 readme.md diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..965bc87 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# editorconfig.org +root = true + +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +# Use tabs in JavaScript. +[**.{js}] +indent_style = tab +indent_size = 4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d6fb75e --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.DS_Store +Thumbs.db + +.nvm-version +node_modules +npm-debug.log diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5165806 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM node + +WORKDIR /app +ADD . /app + +RUN npm install --production + +EXPOSE 3000 + +ENV HOSTNAME db.transport.rest +ENV PORT 3000 + +CMD ["npm", "start"] diff --git a/api.js b/api.js new file mode 100644 index 0000000..9d91814 --- /dev/null +++ b/api.js @@ -0,0 +1,57 @@ +'use strict' + +const express = require('express') +const hsts = require('hsts') +const morgan = require('morgan') +const shorthash = require('shorthash').unique +const corser = require('corser') +const compression = require('compression') +const nocache = require('nocache') + +const pkg = require('./package.json') +const departures = require('./lib/departures') +const journeys = require('./lib/journeys') + + + +const api = express() +module.exports = api + +api.use(hsts({maxAge: 24 * 60 * 60 * 1000})) + +morgan.token('id', (req, res) => req.headers['x-identifier'] || shorthash(req.ip)) +api.use(morgan(':date[iso] :id :method :url :status :response-time ms')) + +const allowed = corser.simpleRequestHeaders.concat(['User-Agent', 'X-Identifier']) +api.use(corser.create({requestHeaders: allowed})) // CORS + +api.use(compression()) + +api.use((req, res, next) => { + if (!res.headersSent) + res.setHeader('X-Powered-By', pkg.name + ' ' + pkg.homepage) + next() +}) + + + +const noCache = nocache() + +api.get('/stations/:id/departures', noCache, departures) +api.get('/journeys', noCache, journeys) + + + +api.use((err, req, res, next) => { + if (process.env.NODE_ENV === 'dev') console.error(err) + if (res.headersSent) return next(err) + + let msg = err.message + let code = err.statusCode + if (err.isHafasError) { + msg = 'DB error: ' + msg + code = 502 + } + res.status(code).json({error: true, msg}) + next() +}) diff --git a/index.js b/index.js new file mode 100644 index 0000000..c4d96ad --- /dev/null +++ b/index.js @@ -0,0 +1,18 @@ +'use strict' + +const http = require('http') + +const api = require('./api') +const server = http.createServer(api) + +const port = process.env.PORT || 3000 +const hostname = process.env.HOSTNAME || '' + +server.listen(port, (err) => { + if (err) { + console.error(err) + process.exit(1) + } else { + console.info(`Listening on ${hostname}:${port}.`) + } +}) diff --git a/license.md b/license.md new file mode 100644 index 0000000..f6f1f94 --- /dev/null +++ b/license.md @@ -0,0 +1,5 @@ +Copyright (c) 2017, Jannis R + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/package.json b/package.json new file mode 100644 index 0000000..ee92242 --- /dev/null +++ b/package.json @@ -0,0 +1,39 @@ +{ + "private": true, + "name": "db-rest", + "description": "Work in progress.", + "version": "0.1.0", + "main": "index.js", + "files": [ + "index.js", + "api.js", + "lib" + ], + "author": "Jannis R ", + "homepage": "https://github.com/derhuerst/db-rest", + "repository": "derhuerst/db-rest", + "bugs": "https://github.com/derhuerst/db-rest/issues", + "license": "ISC", + "keywords": [ + "public", + "transport", + "api", + "http", + "rest" + ], + "engines": { + "node": ">=6" + }, + "dependencies": { + "compression": "^1.7.1", + "corser": "^2.0.1", + "express": "^4.16.2", + "hsts": "^2.1.0", + "morgan": "^1.9.0", + "nocache": "^2.0.0", + "shorthash": "^0.0.2" + }, + "scripts": { + "start": "node index.js" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..577d9cf --- /dev/null +++ b/readme.md @@ -0,0 +1,22 @@ +# db-rest + +Work in progress. + +[![dependency status](https://img.shields.io/david/derhuerst/db-rest.svg)](https://david-dm.org/derhuerst/db-rest) +![ISC-licensed](https://img.shields.io/github/license/derhuerst/db-rest.svg) +[![gitter channel](https://badges.gitter.im/derhuerst/db-rest.svg)](https://gitter.im/derhuerst/db-rest) + + +## Installing + +``` +git clone https://github.com/derhuerst/db-rest.git +cd db-rest +npm install --production +npm start +``` + + +## Contributing + +If you **have a question**, **found a bug** or want to **propose a feature**, have a look at [the issues page](https://github.com/derhuerst/db-rest/issues).