UNPKG

grpc-server-ts

Version:

a grpc server side module built by typescrpt

79 lines 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const GRPC = require("grpc"); const FS = require("fs"); const ServiceContainer_1 = require("./ServiceContainer"); const SettingRegistry_1 = require("./SettingRegistry"); class RpcRegistry { static get port() { return this._port; } static get host() { return this._host; } static get ca() { return this._ca; } static get cert() { return this._cert; } static get key() { return this._key; } static get server() { return this._server; } static start() { this._loadSettings(); this._registryService(); this._addTls(); this._start(); } static _start() { let address = `${this.host}:${this.port}`; this.server.bind(address, this._credentials); this.server.start(); } static _loadSettings() { let { port, host, ca, cert, key } = SettingRegistry_1.SettingRegistry.settings; this._port = port; this._host = host || '127.0.0.1'; this._ca = ca; this._cert = cert; this._key = key; } static _registryService() { for (let serviceContainer of ServiceContainer_1.ServiceContainer.services) { let rpc = {}; for (let key in serviceContainer.service) { let route = serviceContainer.target.prototype[key]; if (route) { let routeContainer = ServiceContainer_1.ServiceContainer.routes.find(r => { return (r.target.constructor === serviceContainer.target && route === r.route); }); let _func = ServiceContainer_1.ServiceContainer.generateRouteFunc(serviceContainer.service, route); routeContainer.func = _func; rpc[key] = routeContainer.func; } } this.server.addService(serviceContainer.service, rpc); } } static _addTls() { if (!this.ca || !this.cert || !this.key) { this._credentials = GRPC.ServerCredentials.createInsecure(); } else { let ca = FS.readFileSync(this.ca); let cert = FS.readFileSync(this.cert); let key = FS.readFileSync(this.key); this._credentials = GRPC.ServerCredentials.createSsl(ca, [{ cert_chain: cert, private_key: key }], true); } } } RpcRegistry._server = new GRPC.Server(); exports.RpcRegistry = RpcRegistry; //# sourceMappingURL=RpcRegistry.js.map