UNPKG

kobp

Version:
98 lines 3.05 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BootstrapLoader = void 0; const koa_1 = __importDefault(require("koa")); const _compileCustomization = (options) => { const allOpts = options; return { onInit: async () => { for (const opt of allOpts) { if (opt.onInit) { await opt.onInit(); } } }, onAppCreated: (app) => { for (const opt of allOpts) { if (opt.onAppCreated) { opt.onAppCreated(app); } } }, middlewares: (app) => { for (const opt of allOpts) { if (opt.middlewares) { opt.middlewares(app); } } }, onSignalReceived: async (signal, app) => { for (const opt of allOpts) { if (opt.onSignalReceived) { await opt.onSignalReceived(signal, app); } } } }; }; class BootstrapLoader { constructor() { this.modules = []; } addModule(module) { this.modules.push(module); return this; } buildSync(serviceRoutes, appCustomization) { const opts = _compileCustomization([ ...(this.modules.map((o) => o.customization())), appCustomization, ]); // Actual Bootstraping const app = new koa_1.default(); // Fork let initPromise = opts.onInit(); app.use(async (context, next) => { await initPromise; await next(); }); this._launchKoa(serviceRoutes, app, opts); return app; } async build(serviceRoutes, appCustomization) { const opts = _compileCustomization([ ...(this.modules.map((o) => o.customization())), appCustomization, ]); await opts.onInit(); // Actual Bootstraping const app = new koa_1.default(); this._launchKoa(serviceRoutes, app, opts); return app; } _launchKoa(serviceRoutes, koa, opts) { // Fork opts.middlewares && opts.middlewares(koa); // Register actual application koa.use(serviceRoutes.routes()); koa.use(serviceRoutes.allowedMethods()); opts.onAppCreated && opts.onAppCreated(koa); if (opts.onSignalReceived) { // register onSignalReceived handler. process.on('SIGTERM', (signal) => { opts.onSignalReceived(signal, koa) .then(() => { process.exit(0); }) .catch((e) => { process.exit(1); }); }); } } } exports.BootstrapLoader = BootstrapLoader; //# sourceMappingURL=bootstrap.js.map