Commit ac9aacf9 authored by A. Koch's avatar A. Koch

dded config, basic resource service, models and persistence added

parent 0c2a5cf8
{
"http": {
"host": "0.0.0.0",
"port": 3030
},
"resources": {
"mongodb": {
"prefix": "res_",
"url": "mongodb://localhost:27017/motionbank-api",
"dbName": "motionbank-api"
}
}
}
......@@ -296,6 +296,32 @@
"eslint-visitor-keys": "^1.0.0"
}
},
"babel-polyfill": {
"version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-polyfill/-/babel-polyfill-6.26.0.tgz",
"integrity": "sha1-N5k3q8Z9eJWXCtxiHyhM2WbPIVM=",
"requires": {
"babel-runtime": "^6.26.0",
"core-js": "^2.5.0",
"regenerator-runtime": "^0.10.5"
}
},
"babel-runtime": {
"version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-6.26.0.tgz",
"integrity": "sha1-llxwWGaOgrVde/4E/yM3vItWR/4=",
"requires": {
"core-js": "^2.4.0",
"regenerator-runtime": "^0.11.0"
},
"dependencies": {
"regenerator-runtime": {
"version": "0.11.1",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz",
"integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg=="
}
}
},
"babylon": {
"version": "7.0.0-beta.44",
"resolved": "https://registry.npmjs.org/babylon/-/babylon-7.0.0-beta.44.tgz",
......@@ -308,6 +334,26 @@
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
"dev": true
},
"basic-auth": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.0.tgz",
"integrity": "sha1-AV2z81PgLlY3d1X5YnQuiYHnu7o=",
"requires": {
"safe-buffer": "5.1.1"
},
"dependencies": {
"safe-buffer": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz",
"integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg=="
}
}
},
"bignumber.js": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-6.0.0.tgz",
"integrity": "sha512-x247jIuy60/+FtMRvscqfxtVHQf8AGx2hm9c6btkgC0x/hp9yt+teISNhvF8WlwRkCc5yF2fDECH8SIMe8j+GA=="
},
"binary-search-tree": {
"version": "0.2.5",
"resolved": "https://registry.npmjs.org/binary-search-tree/-/binary-search-tree-0.2.5.tgz",
......@@ -319,8 +365,7 @@
"bluebird": {
"version": "3.5.1",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz",
"integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA==",
"dev": true
"integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA=="
},
"body-parser": {
"version": "1.18.3",
......@@ -540,6 +585,15 @@
"typedarray": "^0.0.6"
}
},
"config": {
"version": "1.30.0",
"resolved": "https://registry.npmjs.org/config/-/config-1.30.0.tgz",
"integrity": "sha1-HWCp81NIoTwXV5jThOgaWhbDum4=",
"requires": {
"json5": "0.4.0",
"os-homedir": "1.0.2"
}
},
"connected": {
"version": "0.0.2",
"resolved": "https://registry.npmjs.org/connected/-/connected-0.0.2.tgz",
......@@ -556,6 +610,11 @@
"resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz",
"integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA=="
},
"core-js": {
"version": "2.5.7",
"resolved": "https://registry.npmjs.org/core-js/-/core-js-2.5.7.tgz",
"integrity": "sha512-RszJCAxg/PP6uzXVXL6BsxSXx/B05oJAQ2vkJRjyjrEcNVycaqOmNb5OTxZPE3xa5gwZduqza6L9JOCenh/Ecw=="
},
"core-util-is": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
......@@ -1107,6 +1166,11 @@
"integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=",
"dev": true
},
"harmony-reflect": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/harmony-reflect/-/harmony-reflect-1.6.0.tgz",
"integrity": "sha512-0kZ1XcoelFOLEjEtvWAZyq/1S55eDSieWEJwme311MNVNcRpvjlr2zA66kBV6WAB8C1XI1p1cXCnFPqd1BxlPg=="
},
"has": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",
......@@ -1173,6 +1237,11 @@
"integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=",
"dev": true
},
"in-publish": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/in-publish/-/in-publish-2.0.0.tgz",
"integrity": "sha1-4g/146KvwmkDILbcVSaCqcf631E="
},
"inflight": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
......@@ -1354,6 +1423,11 @@
"integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=",
"dev": true
},
"json5": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/json5/-/json5-0.4.0.tgz",
"integrity": "sha1-BUNS5MTIDIbAkjh31EneF2pzLI0="
},
"kind-of": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz",
......@@ -1440,8 +1514,7 @@
"lodash": {
"version": "4.17.10",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz",
"integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==",
"dev": true
"integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg=="
},
"loose-envify": {
"version": "1.3.1",
......@@ -1475,6 +1548,33 @@
"@arr/every": "^1.0.0"
}
},
"mbjs-data-models": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/mbjs-data-models/-/mbjs-data-models-0.0.3.tgz",
"integrity": "sha512-ntpZ3on29EFenGWqCjm+DJIukoB1yAwa5rlVTZ6tEOREq8zCGdZpk8T2vZotC8elBwxtD5/Kib7i82fkguAQfA==",
"requires": {
"bignumber.js": "^6.0.0",
"bluebird": "^3.5.1",
"mbjs-utils": "^0.0.1",
"schema-object": "^4.0.11",
"slug": "^0.9.1",
"validator": "^9.4.1"
},
"dependencies": {
"mbjs-utils": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/mbjs-utils/-/mbjs-utils-0.0.1.tgz",
"integrity": "sha512-kp3p7VrtrFmy7D0OFWHpcWHF1e0Gf9PP83+iXAvQ3OQhVLvj9wxVtibcBEy5AeUm7tmxYEv33iv6xs3pahh9sQ==",
"requires": {
"deep-extend": "^0.5.0",
"luxon": "^1.0.0",
"slug": "^0.9.1",
"uuid": "^3.2.1",
"validator": "^9.4.1"
}
}
}
},
"mbjs-persistence": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/mbjs-persistence/-/mbjs-persistence-0.2.0.tgz",
......@@ -1602,6 +1702,28 @@
"require_optional": "^1.0.1"
}
},
"morgan": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.0.tgz",
"integrity": "sha1-0B+mxlhZt2/PMbPLU6OCGjEdgFE=",
"requires": {
"basic-auth": "~2.0.0",
"debug": "2.6.9",
"depd": "~1.1.1",
"on-finished": "~2.3.0",
"on-headers": "~1.0.1"
},
"dependencies": {
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"requires": {
"ms": "2.0.0"
}
}
}
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
......@@ -1669,6 +1791,11 @@
"ee-first": "1.1.1"
}
},
"on-headers": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.1.tgz",
"integrity": "sha1-ko9dD0cNSTQmUepnlLCFfBAGk/c="
},
"once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
......@@ -1701,6 +1828,11 @@
"wordwrap": "~1.0.0"
}
},
"os-homedir": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz",
"integrity": "sha1-/7xJiDNuDoM94MFox+8VISGqf7M="
},
"os-tmpdir": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz",
......@@ -1943,6 +2075,11 @@
"util-deprecate": "~1.0.1"
}
},
"regenerator-runtime": {
"version": "0.10.5",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.10.5.tgz",
"integrity": "sha1-M2w+/BIgrc7dosn6tntaeVWjNlg="
},
"regexpp": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/regexpp/-/regexpp-1.1.0.tgz",
......@@ -2044,6 +2181,17 @@
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
"schema-object": {
"version": "4.0.11",
"resolved": "https://registry.npmjs.org/schema-object/-/schema-object-4.0.11.tgz",
"integrity": "sha1-gmRoTKiJvGA4aSjfBRIYGbPlTfI=",
"requires": {
"babel-polyfill": "^6.16.0",
"harmony-reflect": "^1.5.0",
"in-publish": "^2.0.0",
"lodash": "^4.17.2"
}
},
"semver": {
"version": "5.5.0",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz",
......@@ -2157,6 +2305,14 @@
"is-fullwidth-code-point": "^2.0.0"
}
},
"slug": {
"version": "0.9.1",
"resolved": "https://registry.npmjs.org/slug/-/slug-0.9.1.tgz",
"integrity": "sha1-rwj2CKfBFRa2F3iqgA3OhMUYz9o=",
"requires": {
"unicode": ">= 0.3.1"
}
},
"sort-asc": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/sort-asc/-/sort-asc-0.2.0.tgz",
......
......@@ -29,8 +29,12 @@
"dependencies": {
"@polka/send-type": "^0.4.0",
"body-parser": "^1.18.3",
"config": "^1.30.0",
"mbjs-data-models": "0.0.3",
"mbjs-persistence": "^0.2.0",
"mbjs-utils": "0.0.5",
"mongodb": "^3.0.10",
"morgan": "^1.9.0",
"nedb": "^1.8.0",
"polka": "^0.4.0",
"primus": "^7.2.2",
......
const
config = require('config'),
http = require('http'),
polka = require('polka'),
{ json } = require('body-parser'),
Primus = require('primus')
Primus = require('primus'),
morgan = require('morgan'),
{ json } = require('body-parser')
/**
* HTTP server
*/
const
{ PORT = 3030, HOST = '0.0.0.0' } = process.env,
host = config.get('http.host'),
port = config.get('http.port'),
server = http.createServer()
/**
......@@ -35,18 +38,20 @@ primus.on('disconnection', spark => {
* Setup API server (Polka)
*/
const
models = require('mbjs-data-models'),
Service = require('./service'),
app = polka({ server })
app.use(json())
app.use(morgan(process.env.NODE_ENV === 'production' ? 'common' : 'dev'))
/**
* Configure resources
*/
const annotations = new Service('annotations', app)
const annotations = new Service('annotations', app, models.Annotation, winston)
annotations.on('message', message => primus.write(message))
/**
* Start server
*/
app.listen(PORT, HOST).then(() => winston.log('info', `API started on ${HOST}:${PORT}`))
app.listen(port, host).then(() => winston.log('info', `API started on ${host}:${port}`))
const
config = require('config'),
send = require('@polka/send-type'),
TinyEmitter = require('tiny-emitter')
TinyEmitter = require('tiny-emitter'),
{ ObjectUtil } = require('mbjs-utils'),
{ MongoDB } = require('mbjs-persistence')
class Service extends TinyEmitter {
constructor (name, app) {
constructor (name, app, model, logger) {
super()
this._name = name
this._Model = model
// TODO: make db adapter configurable (nedb, etc.)
this._client = new MongoDB(ObjectUtil.merge({ name, logger }, config.get('resources.mongodb')), 'uuid')
const _this = this
app.get(`/${this._name}.json`, (req, res) => {
_this._response(req, res, [{ hello: 'world' }])
app.get(`/${this._name}.json`, async (req, res) => {
const results = await this.client.find(req.params.query || {}, req.params)
_this._response(req, res, results)
})
app.get(`/${this._name}/:id.json`, (req, res) => {
_this._response(req, res, { hello: 'world' })
app.get(`/${this._name}/:id.json`, async (req, res) => {
const result = await this.client.get(req.params.id, req.params)
if (result) {
const instance = new this.ModelConstructor(result, `${req.params.id}`)
return _this._response(req, res, instance)
}
send(res, 404)
})
app.post(`/${this._name}.json`, (req, res) => {
const data = req.body
_this._response(req, res, data)
app.post(`/${this._name}.json`, async (req, res) => {
const
ctx = this,
data = req.body
if (Array.isArray(data)) {
const results = await Promise.all(data.map(entry => {
return ctx.create(entry, req.params)
}))
return _this._response(req, res, results)
}
// TODO: allow for full array inserts instead just single requests
const instance = new this.ModelConstructor(data),
result = await this.client.create(instance, req.params)
instance.update(result)
_this._response(req, res, instance)
})
app.put(`/${this._name}/:id.json`, (req, res) => {
app.put(`/${this._name}/:id.json`, async (req, res) => {
const data = req.body
_this._response(req, res, data)
let result = await this.get(req.params.id)
if (result) {
// TODO: transactions anyone?!
let instance = new this.ModelConstructor(data, req.params.id)
result = await this.client.update(req.params.id, instance, req.params)
instance = new this.ModelConstructor(result, req.params.id)
return _this._response(req, res, instance)
}
send(res, 404)
})
app.patch(`/${this._name}/:id.json`, (req, res) => {
app.patch(`/${this._name}/:id.json`, async (req, res) => {
const data = req.body
_this._response(req, res, data)
let instance = await this.get(req.params.id)
if (instance) {
instance.update(data)
await this.client.update(req.params.id, instance, req.params)
return _this._response(req, res, instance)
}
send(res, 404)
})
app.delete(`/${this._name}/:id.json`, (req, res) => {
const data = req.body
_this._response(req, res, data)
app.delete(`/${this._name}/:id.json`, async (req, res) => {
const result = await this.client.remove(req.params.id, req.params)
if (result) {
return _this._response(req, res, result)
}
send(res, 404)
})
}
......@@ -43,6 +84,14 @@ class Service extends TinyEmitter {
this.emit('message', { method: req.method, id: data.id })
send(res, 200, data)
}
get client () {
return this._client
}
get ModelConstructor () {
return this._Model
}
}
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