You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.7 KiB
73 lines
1.7 KiB
2 years ago
|
// hive/kaleidoscope.js
|
||
|
// Copyright (C) 2022 DTP Technologies, LLC
|
||
|
// License: Apache-2.0
|
||
|
|
||
|
'use strict';
|
||
|
|
||
|
const DTP_COMPONENT_NAME = 'hive:kaleidoscope';
|
||
|
|
||
|
const express = require('express');
|
||
|
|
||
|
const { SiteController } = require('../../../lib/site-lib');
|
||
|
|
||
|
class HostController extends SiteController {
|
||
|
|
||
|
constructor (dtp) {
|
||
|
super(dtp, DTP_COMPONENT_NAME);
|
||
|
|
||
|
this.methods = [
|
||
|
{
|
||
|
name: 'postEvent',
|
||
|
url: '/kaleidoscope/event',
|
||
|
method: 'POST',
|
||
|
},
|
||
|
];
|
||
|
}
|
||
|
|
||
|
async start ( ) {
|
||
|
const router = express.Router();
|
||
|
router.use(async (req, res, next) => {
|
||
|
res.locals.currentView = 'hive';
|
||
|
res.locals.hiveView = 'kaleidoscope';
|
||
|
return next();
|
||
|
});
|
||
|
|
||
|
router.post('/core-node/connect', this.postCoreNodeConnect.bind(this));
|
||
|
router.post('/event', this.postEvent.bind(this));
|
||
|
|
||
|
router.get('/', this.getKaleidoscopeRoot.bind(this));
|
||
|
|
||
|
return router;
|
||
|
}
|
||
|
|
||
|
async postCoreNodeConnect (req, res, next) {
|
||
|
const { coreNode: coreNodeService } = this.dtp.services;
|
||
|
try {
|
||
|
await coreNodeService.connect(req.body);
|
||
|
} catch (error) {
|
||
|
this.log.error('failed to create Core Node connection', { error });
|
||
|
return next(error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async postEvent (req, res) {
|
||
|
this.log.debug('kaleidoscope event received', { event: req.body.event });
|
||
|
this.emit('kaleidoscope:event', req, res);
|
||
|
res.status(200).json({ success: true });
|
||
|
}
|
||
|
|
||
|
async getKaleidoscopeRoot (req, res) {
|
||
|
res.status(200).json({
|
||
|
component: DTP_COMPONENT_NAME,
|
||
|
version: this.dtp.pkg.version,
|
||
|
services: this.services,
|
||
|
methods: this.methods,
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = async (dtp) => {
|
||
|
let controller = new HostController(dtp);
|
||
|
return controller;
|
||
|
};
|