init from vbb-rest

This commit is contained in:
Jannis R 2017-10-23 23:13:53 +02:00
commit 8fb8594258
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
8 changed files with 173 additions and 0 deletions

13
.editorconfig Normal file
View file

@ -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

6
.gitignore vendored Normal file
View file

@ -0,0 +1,6 @@
.DS_Store
Thumbs.db
.nvm-version
node_modules
npm-debug.log

13
Dockerfile Normal file
View file

@ -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"]

57
api.js Normal file
View file

@ -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()
})

18
index.js Normal file
View file

@ -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}.`)
}
})

5
license.md Normal file
View file

@ -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.

39
package.json Normal file
View file

@ -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 <mail@jannisr.de>",
"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"
}
}

22
readme.md Normal file
View file

@ -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).