Commit 0c2a5cf8 authored by A. Koch's avatar A. Koch

refactoring, updates

parent 9fa35605
This diff is collapsed.
...@@ -27,11 +27,15 @@ ...@@ -27,11 +27,15 @@
"metapak-motionbank": "^1.12.0" "metapak-motionbank": "^1.12.0"
}, },
"dependencies": { "dependencies": {
"@polka/send-type": "^0.4.0",
"body-parser": "^1.18.3",
"mbjs-persistence": "^0.2.0", "mbjs-persistence": "^0.2.0",
"mongodb": "^3.0.10", "mongodb": "^3.0.10",
"nedb": "^1.8.0", "nedb": "^1.8.0",
"polka": "^0.4.0", "polka": "^0.4.0",
"primus": "^7.2.2", "primus": "^7.2.2",
"serve-static": "^1.13.2",
"tiny-emitter": "^2.0.2",
"uws": "^10.148.0", "uws": "^10.148.0",
"winston": "^2.4.2" "winston": "^2.4.2"
} }
......
const const
http = require('http'), http = require('http'),
polka = require('polka'), polka = require('polka'),
Primus = require('primus'), { json } = require('body-parser'),
winston = require('winston') Primus = require('primus')
/**
* HTTP server
*/
const const
port = process.env.PORT || 3030, { PORT = 3030, HOST = '0.0.0.0' } = process.env,
host = process.env.HOST || '0.0.0.0', server = http.createServer()
server = http.createServer(),
primus = new Primus(server, { transformer: 'uws' }),
app = polka({ server })
/**
* Logger
*/
const winston = require('winston')
winston.level = process.env.NODE_ENV === 'production' ? 'error' : 'info' winston.level = process.env.NODE_ENV === 'production' ? 'error' : 'info'
winston.level = process.env.LOG_LEVEL || winston.level winston.level = process.env.LOG_LEVEL || winston.level
/**
* Set up WebSockets (Primus)
*/
const primus = new Primus(server, { transformer: 'uws' })
primus.on('connection', spark => { primus.on('connection', spark => {
winston.log('debug', `Spark ${spark.id} connected with address ${spark.address}`) winston.log('debug', `Spark ${spark.id} connected with address ${spark.address}`)
}) })
...@@ -22,9 +31,22 @@ primus.on('disconnection', spark => { ...@@ -22,9 +31,22 @@ primus.on('disconnection', spark => {
winston.log('debug', `Spark ${spark.id} disconnected with address ${spark.address}`) winston.log('debug', `Spark ${spark.id} disconnected with address ${spark.address}`)
}) })
app.get('/annotations.json', (req, res) => { /**
res.setHeader('Content-Type', 'application/json') * Setup API server (Polka)
res.end(JSON.stringify({ test: 'asdf' })) */
}) const
Service = require('./service'),
app = polka({ server })
app.use(json())
/**
* Configure resources
*/
const annotations = new Service('annotations', app)
annotations.on('message', message => primus.write(message))
app.listen(port, host).then(() => winston.log('info', `API started on ${host}:${port}`)) /**
* Start server
*/
app.listen(PORT, HOST).then(() => winston.log('info', `API started on ${HOST}:${PORT}`))
const
send = require('@polka/send-type'),
TinyEmitter = require('tiny-emitter')
class Service extends TinyEmitter {
constructor (name, app) {
super()
this._name = name
const _this = this
app.get(`/${this._name}.json`, (req, res) => {
_this._response(req, res, [{ hello: 'world' }])
})
app.get(`/${this._name}/:id.json`, (req, res) => {
_this._response(req, res, { hello: 'world' })
})
app.post(`/${this._name}.json`, (req, res) => {
const data = req.body
_this._response(req, res, data)
})
app.put(`/${this._name}/:id.json`, (req, res) => {
const data = req.body
_this._response(req, res, data)
})
app.patch(`/${this._name}/:id.json`, (req, res) => {
const data = req.body
_this._response(req, res, data)
})
app.delete(`/${this._name}/:id.json`, (req, res) => {
const data = req.body
_this._response(req, res, data)
})
}
_response (req, res, data = {}) {
this.emit('message', { method: req.method, id: data.id })
send(res, 200, data)
}
}
module.exports = Service
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment