UNPKG

kitejs

Version:

the rpc framework Kite for Node.js

207 lines (206 loc) 5.74 kB
"use strict"; /** * @author xiangshouding * @date 2017-07-06 12:02:41 */ exports.__esModule = true; var url_1 = require("url"); var consul_1 = require("./consul"); var protocol_1 = require("./protocol/protocol"); /** * The Config Class */ var Config = /** @class */ (function () { function Config(service, idc, ctx) { this.service = service; this.idc = idc; this.ctx = ctx; this.env = 'prod'; this.searchHostType = 'LOCAL'; // or 'CONSUL' /** * the connect server timeout settings. */ this.timeout = 2000; this.retry = 1; /** * { * "bj": [ * { * 'host': '127.0.0.1', * 'port': 80888 * }, * { * ... * } * ] * } * * @var Map<string, Map<string, string[]>[]> */ this.hosts = {}; if (typeof service != 'string') { this.create(service); } else { this.loadFile(this.service); } } Config.prototype.getHosts = function (cb) { if (this.searchHostType == 'LOCAL') { var hosts = this.hosts[this.idc]; //if idc hosts not exist, return `hosts` or `[{host:, port:}]` if (Array.isArray(hosts)) { return cb(null, hosts); } if (Array.isArray(this.hosts)) { hosts = this.hosts; } else if (this.host && this.port) { hosts = [new protocol_1.Address(this.host, this.port)]; } else { cb(new Error('please given `hosts` or `host` and `port`'), []); } return cb(null, hosts); } else if (this.searchHostType == 'CONSUL') { this.getHostsWithConsul(cb); } else { return cb(new Error("unsupport '" + this.searchHostType + "' searchHostType"), []); } }; /** * git the host list with `idc` */ Config.prototype.getHostsWithIDC = function () { return []; }; Config.prototype.getProtocol = function () { if (!this.protocol) { if (this.searchHostType === 'CONSUL') { this.protocol = 'THRIFT'; } else { this.protocol = 'HTTP'; } } return this.protocol; }; Config.prototype.getConsulAddress = function () { if (!this.consul) { this.consul = { host: 'consul.service.byted.org', port: 2280 }; } return new protocol_1.Address(this.consul.host, this.consul.port); }; Config.prototype.getLogFile = function () { return this.log.logFile; }; Config.prototype.getLogLevel = function () { return this.log.logLevel; }; /** * config information load from a config file * @param {string} file * @return void */ Config.prototype.loadFile = function (file) { var config = {}; /** * if set ctx, read from ctx. * it's config is preload to context. */ if (this.ctx && this.ctx[file]) { config = this.ctx[file]; } else { config = require(file); } this.create(config); }; /** * create a instance of the Config from a Map. * * @param config Map<any> */ Config.prototype.create = function (config) { if (config['timeout']) { config['timeout'] = this.timeFromString(config['timeout']); } else { // default 2s config['timeout'] = this.timeout; } this.options = config; if (config.hosts) { for (var idc in config.hosts) { if (config.hosts.hasOwnProperty(idc)) { var hosts = config.hosts[idc]; this.hosts[idc] = hosts.map(function (address) { return new protocol_1.Address(address.host, address.port); }); } } } for (var key in config) { if (config.hasOwnProperty(key) && ['hosts'].indexOf(key) == -1) { this[key] = config[key]; } } }; /** * the time 1s\1ms to \d+(ms) * * @param s string | any */ Config.prototype.timeFromString = function (s) { if (/([\d.]+)ms/i.test(s)) { return parseFloat(RegExp.$1); } else if (/([\d.]+)s/i.test(s)) { return parseFloat(RegExp.$1) * 1000; } else { return parseFloat(s) * 1000; } }; /** * */ Config.prototype.getHostsWithConsul = function (cb) { // require consul get hosts. return consul_1.consul.get(this.getConsulAddress(), this.service, cb, this.env, null, // idc this.consul.action || 'lookup', { timeout: this.consul.timeout }); }; /** * { * 'address': '127.0.0.1:8099' * } * * host = 127.0.0.1 * port = 8099 * * if not set consul address, use it. */ Config.prototype.getHostsWithAddress = function () { if (typeof this.address != 'string') { return false; } if (this.address.length == 0) { return false; } var url_ = new url_1.URL(this.address); if (!url_) { return false; } this.host = url_.hostname; this.port = parseInt(url_.port); return true; }; return Config; }()); exports.Config = Config; ;