@mdf.js/openc2-core
Version:
MMS - API Core - OpenC2
52 lines • 2.04 kB
JavaScript
;
/**
* Copyright 2024 Mytra Control S.L. All rights reserved.
*
* Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
* or at https://opensource.org/licenses/MIT.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Router = void 0;
const tslib_1 = require("tslib");
const events_1 = tslib_1.__importDefault(require("events"));
const express_1 = tslib_1.__importDefault(require("express"));
const oc2_controller_1 = require("./oc2.controller");
const oc2_model_1 = require("./oc2.model");
const oc2_service_1 = require("./oc2.service");
const PREFIX_PATH = '/openC2';
/** Router class */
class Router extends events_1.default {
/**
* Create a new instance of the Router class
* @param register - Register used by this component
* @param path - prefix path for all the routes
*/
constructor(register, path = PREFIX_PATH) {
super();
/**
* Event handler for the command event
* @param message - message to be processed
* @param done - callback to be called when the command is processed
*/
this.onCommand = (message, done) => {
this.emit('command', message, done);
};
this.model = new oc2_model_1.Model(register);
this.service = new oc2_service_1.Service(this.model);
this.controller = new oc2_controller_1.Controller(this.service);
this.router = this.buildRoutes(path);
this.service.on('command', this.onCommand);
}
/**
* Perform the instantiation of the routes in an express router
* @param path - prefix path for all the routers
*/
buildRoutes(path) {
const router = express_1.default.Router();
router.route(`${path}/command`).post(this.controller.command.bind(this.controller));
router.route(`${path}/:id`).get(this.controller.query.bind(this.controller));
return router;
}
}
exports.Router = Router;
//# sourceMappingURL=oc2.router.js.map