Commit 3027f4e6 authored by A. Koch's avatar A. Koch

update service signatures, integrate error handler

parent 156758ec
Pipeline #1084 passed with stage
......@@ -11,10 +11,10 @@ const setup = async function () {
models = require('mbjs-data-models'),
Service = require('./lib/service')
const annotations = new Service('annotations', api._app, models.Annotation, api._logger, api._acl)
const annotations = new Service('annotations', api, models.Annotation)
// annotations.on('message', message => api._sockets.write(message))
const maps = new Service('maps', api._app, models.Map, api._logger, api._acl)
const maps = new Service('maps', api, models.Map)
// annotations.on('message', message => api._sockets.write(message))
/**
......@@ -22,42 +22,42 @@ const setup = async function () {
*/
const
Metadata = require('./lib/metadata'),
metadata = new Metadata(api._app, annotations)
metadata = new Metadata(api, annotations)
/**
* Configure upload
*/
const
Uploads = require('./lib/uploads'),
uploads = new Uploads(api._app)
uploads = new Uploads(api)
/**
* Configure download
*/
const
Downloads = require('./lib/downloads'),
downloads = new Downloads(api._app)
downloads = new Downloads(api)
/**
* Configure conversion
*/
const
Conversions = require('./lib/conversions'),
conversions = new Conversions(api._app)
conversions = new Conversions(api)
/**
* Configure sequences
*/
const
Sequences = require('./lib/sequences'),
sequences = new Sequences(api._app, annotations, maps)
sequences = new Sequences(api, annotations, maps)
/**
* Configure timecode
*/
const
Timecodes = require('./lib/timecodes'),
timecodes = new Timecodes(api._app)
timecodes = new Timecodes(api)
await api.start()
}
......
......@@ -8,7 +8,7 @@ const
{ ObjectUtil } = require('mbjs-utils')
class Metadata extends TinyEmitter {
constructor (app) {
constructor (api) {
super()
const _this = this
......@@ -16,14 +16,14 @@ class Metadata extends TinyEmitter {
this._queue = new Queue('conversions', config.conversions.redisURL)
this._queue.process(parseInt(config.conversions.concurrency), require('./workers/convert'))
app.post('/conversions', async (req, res) => {
api.app.post('/conversions', async (req, res) => {
const jobId = ObjectUtil.uuid4()
req.body.uuid = ObjectUtil.uuid4()
_this._queue.add(req.body, { jobId })
_this._response(req, res, { jobId })
})
app.get('/conversions/:id', async (req, res) => {
api.app.get('/conversions/:id', async (req, res) => {
const job = await _this._queue.getJob(req.params.id)
if (!job) return _this._errorResponse(res, 404)
const jobInfo = {
......
......@@ -4,13 +4,13 @@ const
TinyEmitter = require('tiny-emitter')
class Downloads extends TinyEmitter {
constructor (app) {
constructor (api) {
super()
const _this = this
this.minioClient = new Minio.Client(config.assets.client)
app.get('/downloads/:file', async (req, res) => {
api.app.get('/downloads/:file', async (req, res) => {
const stream = await _this.minioClient.getObject(config.assets.bucket, req.params.file)
res.setHeader('Content-Type', 'application/force-download')
stream.pipe(res)
......
......@@ -19,12 +19,12 @@ const fetchMetaData = async (annotation, user, annotationsService) => {
return results.data
}, config.apiKeys)
}
catch (e) { console.error('fetchMetaData', e.message) }
catch (e) { api.captureException(e) }
return meta
}
class Metadata extends TinyEmitter {
constructor (app, annotationsService) {
constructor (api, annotationsService) {
super()
this._annotations = annotationsService
......@@ -32,7 +32,7 @@ class Metadata extends TinyEmitter {
const _this = this
app.get('/metadata/:id', async (req, res) => {
api.app.get('/metadata/:id', async (req, res) => {
let source, annotation
if (req.params.id === 'url') {
source = req.query.url
......@@ -43,7 +43,7 @@ class Metadata extends TinyEmitter {
annotation = result.data
source = annotation.body.source.id
}
catch (e) { /* ignored */ }
catch (e) { api.captureException(e) }
}
if (!source) return _this._errorResponse(res, 404)
const key = `metadata_${ObjectUtil.slug(source)}`
......@@ -51,7 +51,7 @@ class Metadata extends TinyEmitter {
if (_this._memcached) {
metadata = await new Promise((resolve, reject) => {
_this._memcached.get(key, function (err, data) {
if (err) console.error('failed to get metadata from cache', err.message)
if (err) api.captureException(err)
resolve(data)
})
})
......@@ -61,7 +61,7 @@ class Metadata extends TinyEmitter {
if (_this._memcached && metadata) {
await new Promise((resolve, reject) => {
_this._memcached.set(key, metadata, parseInt(config.metadata.lifetime.toString()), err => {
if (err) console.error('failed to store metadata in cache', err.message)
if (err) api.captureException(err)
resolve()
})
})
......
......@@ -7,7 +7,7 @@ const
{ ObjectUtil } = require('mbjs-utils')
class Sequences extends TinyEmitter {
constructor (app, annotationsService, mapsService) {
constructor (api, annotationsService, mapsService) {
super()
const _this = this
......@@ -18,7 +18,7 @@ class Sequences extends TinyEmitter {
this._queue = new Queue('sequences', config.sequences.redisURL)
this._queue.process(parseInt(config.sequences.concurrency), require('./workers/concat'))
app.post('/sequences', async (req, res) => {
api.app.post('/sequences', async (req, res) => {
let result = await _this._maps.getHandler({
params: {
id: req.body.id
......@@ -43,7 +43,7 @@ class Sequences extends TinyEmitter {
_this._response(req, res, { jobId })
})
app.get('/sequences/:id', async (req, res) => {
api.app.get('/sequences/:id', async (req, res) => {
const job = await _this._queue.getJob(req.params.id)
if (!job) return _this._errorResponse(res, 404)
const jobInfo = {
......
......@@ -6,24 +6,24 @@ const
{ MongoDB } = require('mbjs-persistence')
class Service extends TinyEmitter {
constructor (name, app, model, logger, acl) {
constructor (name, api, model) {
super()
const _this = this
this._name = name
this._acl = acl
this._logger = logger
this._acl = api.acl
this._logger = api.logger
this._Model = model
// TODO: make db adapter configurable (nedb, etc.)
this._client = new MongoDB(ObjectUtil.merge({ name, logger }, config.get('resources.mongodb')), 'uuid')
app.get(`/${this._name}`, (req, res) => _this.findHandler(req, res))
app.get(`/${this._name}/:id`, (req, res) => _this.getHandler(req, res))
app.post(`/${this._name}`, (req, res) => _this.postHandler(req, res))
app.put(`/${this._name}/:id`, (req, res) => _this.putHandler(req, res))
app.patch(`/${this._name}/:id`, (req, res) => _this.patchHandler(req, res))
app.delete(`/${this._name}/:id`, (req, res) => _this.deleteHandler(req, res))
this._client = new MongoDB(ObjectUtil.merge({ name, logger: api.logger }, config.get('resources.mongodb')), 'uuid')
api.app.get(`/${this._name}`, (req, res) => _this.findHandler(req, res))
api.app.get(`/${this._name}/:id`, (req, res) => _this.getHandler(req, res))
api.app.post(`/${this._name}`, (req, res) => _this.postHandler(req, res))
api.app.put(`/${this._name}/:id`, (req, res) => _this.putHandler(req, res))
api.app.patch(`/${this._name}/:id`, (req, res) => _this.patchHandler(req, res))
api.app.delete(`/${this._name}/:id`, (req, res) => _this.deleteHandler(req, res))
}
async findHandler (req, res) {
......@@ -38,7 +38,7 @@ class Service extends TinyEmitter {
allowed = await this._acl.isAllowed(user, entry.uuid, 'get')
}
catch (err) {
this._logger.error(`ACL error: ${err.message}`)
api.captureException(err)
}
}
if (allowed) items.push(entry)
......@@ -57,7 +57,7 @@ class Service extends TinyEmitter {
allowed = await this._acl.isAllowed(user, result.uuid, 'get')
}
catch (err) {
this._logger.error(`ACL error: ${err.message}`)
api.captureException(err)
}
}
if (allowed) {
......
......@@ -8,7 +8,7 @@ const
{ ObjectUtil } = require('mbjs-utils')
class Timecodes extends TinyEmitter {
constructor (app) {
constructor (api) {
super()
this._queue = new Queue('timecode', config.timecode.redisURL)
......@@ -18,14 +18,14 @@ class Timecodes extends TinyEmitter {
const _this = this
app.post('/timecodes', async (req, res) => {
api.app.post('/timecodes', async (req, res) => {
const jobId = ObjectUtil.uuid4()
req.body.uuid = ObjectUtil.uuid4()
_this._queue.add(req.body, { jobId })
_this._response(req, res, { jobId })
})
app.get('/timecodes/signals/ltc', async (req, res) => {
api.app.get('/timecodes/signals/ltc', async (req, res) => {
const files = []
const stream = await this._minio.listObjects('ltc', 'LTC')
stream.on('data', obj => files.push(obj.name))
......@@ -40,7 +40,7 @@ class Timecodes extends TinyEmitter {
})
})
app.get('/timecodes/:id', async (req, res) => {
api.app.get('/timecodes/:id', async (req, res) => {
const job = await _this._queue.getJob(req.params.id)
if (!job) return _this._errorResponse(res, 404)
const jobInfo = {
......
......@@ -9,13 +9,13 @@ const
TinyEmitter = require('tiny-emitter')
class Uploads extends TinyEmitter {
constructor (app) {
constructor (api) {
super()
const _this = this
const upload = multer({ dest: os.tmpdir() })
app.post('/uploads', async (req, res) => {
api.app.post('/uploads', async (req, res) => {
upload.single('file')(req, res, async () => {
const extname = path.extname(req.file.originalname)
const filename = `${ObjectUtil.uuid4()}${extname.toLowerCase()}`
......@@ -33,7 +33,7 @@ class Uploads extends TinyEmitter {
})
})
app.delete('/uploads/:file', async (req, res) => {
api.app.delete('/uploads/:file', async (req, res) => {
const minioClient = new Minio.Client(config.assets.client)
await minioClient.removeObject(config.assets.bucket, req.params.file)
_this._response(req, res)
......
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