UNPKG

@fabrix/spool-hapi

Version:

Spool - Hapi.js. This spool binds the routes compiled in spool-router to a Hapi Server.

61 lines (60 loc) 2.87 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const server_1 = require("@fabrix/fabrix/dist/common/spools/server"); const Hapi = require("hapi"); const server_2 = require("./server"); const validator_1 = require("./validator"); const config = require("./config/index"); const pkg = require("../package.json"); const api = require("./api/index"); class HapiSpool extends server_1.ServerSpool { constructor(app) { super(app, { config: config, pkg: pkg, api: api }); } validate() { return __awaiter(this, void 0, void 0, function* () { const requiredSpools = ['router']; const spools = Object.keys(this.app.spools); if (!spools.some(v => requiredSpools.indexOf(v) >= 0)) { return Promise.reject(new Error(`spool-hapi requires spools: ${requiredSpools.join(', ')}!`)); } return validator_1.Validator.validateWebConfig(this.app.config.get('web')); }); } configure() { this.app.config.set('web.server', 'hapi'); this.app.config.set('web.routes.files.relativeTo', this.app.config.get('main.paths.root')); this.serverConfig = Object.assign({ host: this.app.config.get('web.host'), port: this.app.config.get('web.port'), routes: this.app.config.get('web.routes'), plugins: this.app.config.get('web.plugins'), router: this.app.config.get('web.router') }, this.app.config.get('web.options')); } initialize() { return __awaiter(this, void 0, void 0, function* () { this.server = new Hapi.Server(this.serverConfig); yield server_2.Server.registerPlugins(this.server, this.app); server_2.Server.registerRoutes(this.server, this.app); server_2.Server.registerViews(this.server, this.app); server_2.Server.registerExtensions(this.server, this.app); yield this.server.start(); this.app.emit('webserver:http:ready', this.server.listener); }); } unload() { return __awaiter(this, void 0, void 0, function* () { if (this.server) { yield this.server.stop(); } }); } } exports.HapiSpool = HapiSpool;