1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | 1x 1x 1x 1x 1x 1x 3x 3x 6x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 9x 9x 1x 1x 1x | "use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; Iif (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) Eif (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments)).next()); }); }; const Koa = require("koa"); const Debug = require("debug"); const bodyParser = require("koa-bodyparser"); const override_1 = require("../middleware/override"); const timer_1 = require("../decorators/timer"); const overlander_1 = require("../middleware/overlander"); const http_1 = require("http"); const https_1 = require("https"); const debug = Debug('overland:core'); class Overland extends Koa { constructor(settings) { super(); /** * Settings object. */ this.apps = new Map(); this.controllers = new Map(); this.helpers = {}; this.settings = settings; } createRenderContext(ctx, data) { const app = ctx.state.controller.app; const thisHelpers = this.helpers[app]; const helpers = Object.assign({}, this.helpers, thisHelpers); const __ = ctx.i18n ? ctx.i18n.__.bind(ctx.i18n) : s => s; return Object.assign({ __ }, data, helpers); } /** * Loads middleware, routes, etc. */ boot() { return __awaiter(this, void 0, void 0, function* () { const { keys, middleware, apps } = this.settings; Iif (keys) { this.keys = keys; } debug(`loading middleware`); // Load core middleware (overlander, override); debug(` - core`); this.use(overlander_1.default(this)); this.use(override_1.default(this)); // Load contrib middleware debug(` - contrib`); this.use(bodyParser()); // Load user middleware debug(` - user`); for (const fn of middleware) { this.use(fn); } // put app Ctors in map for (const App of apps) { this.apps.set(App.app, App); } // reduce all controllers to single array, add to map apps.map(App => App.controllers) .reduce((a, ctrls) => a.concat(ctrls), []) .filter(a => !!a) .forEach(ctrl => this.controllers.set(`${ctrl.app}.${ctrl.controller}`, ctrl)); // load user apps debug(`loading apps`); const promises = apps.map(App => { const app = new App(); debug(` - ${App.app}`); return app.init(this); }); yield Promise.all(promises); return this; }); } listen() { const servers = { http: undefined, https: undefined }; // boot https server (only if SSL config provided, unnecessary for rev. proxy setup) if (this.settings.ssl) { if (!this.settings.ssl.key || !this.settings.ssl.cert) { throw new Error('Invalid SSL configuration.'); } else { debug(`https listening on ${this.settings.ports.https}`); servers.https = https_1.createServer(this.settings.ssl, this.callback()).listen(this.settings.ports.https); } } // boot http server debug(`http listening on ${this.settings.ports.http}`); servers.http = http_1.createServer(this.callback()).listen(this.settings.ports.http); // return servers to user return servers; } use(middleware) { debug(` - use ${middleware.name || '-'}`); return super.use(middleware); } } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = Overland; __decorate([ timer_1.default('boot') ], Overland.prototype, "boot", null); |