UNPKG

@darlean/webservice-suite

Version:

Webservice Suite that acts as Web/API Gateway that invokes actors to serve HTTP requests

82 lines (81 loc) 3.78 kB
"use strict"; /** * Suite that provides the WebService service that makes it possible to handle HTTP requests with actors. * * @packageDocumentation */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createWebserviceSuite = exports.WEBSERVICE_HOST_ACTOR = void 0; const base_1 = require("@darlean/base"); const utils_1 = require("@darlean/utils"); const actor_impl_1 = require("./actor.impl"); exports.WEBSERVICE_HOST_ACTOR = 'io.darlean.WebServiceHostActor'; __exportStar(require("./actor.impl"), exports); __exportStar(require("./intf"), exports); function createWebserviceSuite(config, appId) { const startActions = []; for (const host of config.hosts ?? []) { startActions.push({ name: `Webservice ${host.id ?? 'default'}`, id: [host.id ?? 'default', appId], action: 'touch' }); } return new base_1.ActorSuite([ { type: exports.WEBSERVICE_HOST_ACTOR, kind: 'singular', placement: { version: '20230202', bindIdx: -1 }, creator: (context) => { const id = context.id[0]; const hostcfg = config.hosts?.find((x) => x.id === id); if (hostcfg) { const cfg = { name: hostcfg.id ?? 'default', port: (0, utils_1.fetchConfigNumber)('DARLEAN_WEBSERVICE_PORT', '--darlean-webservice-port') ?? hostcfg.port ?? 80, handlers: [] }; for (const handler of hostcfg.handlers ?? []) { const actorType = handler.actorType ?? hostcfg.actorType; if (!actorType) { throw new Error(`No actor type configured for WebService handler`); } const actionName = handler.actionName; if (!actionName) { throw new Error('No action name configured for WebService handler'); } const actorId = handler.actorId ?? hostcfg.actorId ?? []; const h = { method: handler.method, path: handler.path, action: async (req) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const actor = context.portal.retrieve(actorType, actorId); return await actor[actionName](req); } }; cfg.handlers?.push(h); } return new actor_impl_1.WebServiceHostActor(cfg); } else throw new Error(`Host [${id}] is not configured`); }, startActions } ]); } exports.createWebserviceSuite = createWebserviceSuite;