pao-aop-server
Version:
基于pao-aop的服务端框架
402 lines (400 loc) • 16 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (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 __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "fs", "pao-aop"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var pao_aop_1 = require("pao-aop");
/**
* 默认文件编码器
*/
var defaultFileEncoding = 'utf-8';
/**
* 移除文件中的BOM
* @author pao
* @param bin
*/
function removeBOM(bin) {
if (bin[0] === 0xEF && bin[1] === 0xBB && bin[2] === 0xBF) {
bin = bin.slice(3);
}
return bin;
}
exports.removeBOM = removeBOM;
/**
* 保存
* @author pao
* @param filePath 文件路径
*/
function saveObject(filePath, obj) {
var configString = pao_aop_1.addonSerialize(obj);
fs.writeFileSync(filePath, configString, defaultFileEncoding);
pao_aop_1.log('file', "\u914D\u7F6E\u6587\u4EF6:" + filePath + "\u4FDD\u5B58\u5B8C\u6BD5");
}
exports.saveObject = saveObject;
/**
* 读取
* @author pao
* @param filePath 文件路径
*/
function loadObject(filePath) {
var bin = fs.readFileSync(filePath);
// 移除BOM
bin = removeBOM(bin);
var buffer = bin.toString(defaultFileEncoding);
var obj = pao_aop_1.addonDeserialize(buffer);
pao_aop_1.log('file', "\u914D\u7F6E\u6587\u4EF6:" + filePath + "\u8BFB\u53D6\u5B8C\u6BD5");
return obj;
}
exports.loadObject = loadObject;
/**
* 准备配置
* @author pao
* @param defaultConfig 默认配置对象
* @param configFile 配置文件
* @param loadConfig 是否从配置文件加载
* @param saveConfig 是否保存配置文件
*/
function prepareConfig(defaultConfig, configFile, loadConfig, saveConfig) {
if (loadConfig === void 0) { loadConfig = false; }
if (saveConfig === void 0) { saveConfig = true; }
var configObject;
// 加载应用程序配置
if (loadConfig) {
configObject = loadObject(configFile);
}
else {
configObject = defaultConfig;
if (saveConfig) {
saveObject(configFile, configObject);
}
}
return configObject;
}
exports.prepareConfig = prepareConfig;
/**
* 服务信息: 包含服务名称和服务对象的信息
* @author pao
*/
var ServerInfo = /** @class */ (function () {
function ServerInfo(serverName, serverObject) {
this.serverName = serverName;
this.serverObject = serverObject;
}
ServerInfo = __decorate([
pao_aop_1.addon('ServerInfo', '服务信息', '包含服务名称和服务对象的信息'),
__metadata("design:paramtypes", [String, BaseServer])
], ServerInfo);
return ServerInfo;
}());
exports.ServerInfo = ServerInfo;
/**
* 全局服务器列表
* @author pao
*/
var GlobalServers = new Map();
/**
* 注册服务列表
* @author pao
* @param serverInfos 服务信息列表
*/
function registerServers(serverInfos) {
for (var _i = 0, serverInfos_1 = serverInfos; _i < serverInfos_1.length; _i++) {
var serverInfo = serverInfos_1[_i];
registerServer(serverInfo);
}
}
exports.registerServers = registerServers;
/**
* 注册服务
* @author pao
* @param serverInfo 服务信息
*/
function registerServer(serverInfo) {
if (!GlobalServers.get(serverInfo.serverName)) {
pao_aop_1.log('server', "\u670D\u52A1\u6CE8\u518C\uFF0C\u670D\u52A1\u540D\u79F0\uFF1A" + serverInfo.serverName);
GlobalServers.set(serverInfo.serverName, serverInfo.serverObject);
}
}
exports.registerServer = registerServer;
/**
* 编历服务器
* @param serviceFunction 服务处理方法
*/
function forEachServer(serverFunction) {
GlobalServers.forEach(serverFunction);
}
exports.forEachServer = forEachServer;
/** 根据名称获取服务
* @author pao
* @param name 名称
*/
function getServerByName(name) {
return GlobalServers.get(name);
}
exports.getServerByName = getServerByName;
/**
* 服务信息: 包含服务名称和服务对象的信息
* @author pao
*/
var ServiceInfo = /** @class */ (function () {
function ServiceInfo(serviceName, serviceObject) {
this.serviceName = serviceName;
this.serviceObject = serviceObject;
}
ServiceInfo = __decorate([
pao_aop_1.addon('ServiceInfo', '服务信息', '包含服务名称和服务对象的信息'),
__metadata("design:paramtypes", [String, BaseService])
], ServiceInfo);
return ServiceInfo;
}());
exports.ServiceInfo = ServiceInfo;
/**
* 名称:基础服务
* @description 所有服务的基类
* @author huyl
*/
var BaseService = /** @class */ (function (_super) {
__extends(BaseService, _super);
/** 基础服务 */
function BaseService() {
return _super.call(this) || this;
}
/** 调用 */
BaseService.prototype.call = function (userToken, session, functionName, args) {
return __awaiter(this, void 0, void 0, function () {
var that, result, resultString;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
// 找不到方法
if (!this[functionName]) {
throw new Error("\u5F53\u524D\u670D\u52A1\u6CA1\u6709[" + functionName + "]\u7684\u65B9\u6CD5");
}
that = this;
// 设置用户令牌和会话
this.userToken = userToken;
this.session = session;
return [4 /*yield*/, this.beforeCallFunction(userToken, session, functionName, args)];
case 1:
_a.sent();
pao_aop_1.log('service', "\u5F00\u59CB\u8C03\u7528\u51FD\u6570" + functionName);
return [4 /*yield*/, that[functionName].apply(that, args)];
case 2:
result = _a.sent();
resultString = (result || 'undefined').toString().substr(0, 50);
pao_aop_1.log('service', "\u8C03\u7528\u51FD\u6570" + functionName + "\u7ED3\u675F\uFF0C\u8FD4\u56DE:" + resultString);
return [2 /*return*/, result];
}
});
});
};
/**
* 函数调用前
* @param userToken 用户令牌
* @param session 会话
* @param functionName 函数名称
* @param args 参数
*/
BaseService.prototype.beforeCallFunction = function (userToken, session, functionName, args) {
return __awaiter(this, void 0, void 0, function () {
var error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
if (!this.authoryObject) {
/** 如果未定义授权对象,则默认为允许调用 */
return [2 /*return*/];
}
// 默认返回true,具有权限
return [4 /*yield*/, this.authoryObject.checkAuthority(userToken, session, functionName, args)];
case 1:
// 默认返回true,具有权限
_a.sent();
return [3 /*break*/, 3];
case 2:
error_1 = _a.sent();
throw new Error("\u5F53\u524D\u7528\u6237\u6267\u884C\u65B9\u6CD5" + functionName + "\u65F6\u6388\u6743\u5931\u8D25, \u9519\u8BEF:" + error_1);
case 3: return [2 /*return*/];
}
});
});
};
BaseService = __decorate([
pao_aop_1.addon('BaseService', '基础服务', '所有服务的基类'),
__metadata("design:paramtypes", [])
], BaseService);
return BaseService;
}(pao_aop_1.BaseAddon));
exports.BaseService = BaseService;
/**
* 名称:基础服务器
* @description 所有服务器的基类,服务器是可以启动和停止的应用程序,服务器程序不应当阻塞主线程
* @author pao
*/
var BaseServer = /** @class */ (function (_super) {
__extends(BaseServer, _super);
function BaseServer() {
var _this = _super !== null && _super.apply(this, arguments) || this;
/**
* 服务器状态
*/
_this.status = 'stopped';
return _this;
}
/**
* 服务启动
*/
BaseServer.prototype.start = function () {
try {
this.onStart();
this.status = 'running';
}
catch (error) {
this.status = "error: " + error;
}
};
/**
* 服务停止
*/
BaseServer.prototype.stop = function () {
try {
this.onStop();
this.status = 'stopped';
}
catch (error) {
this.status = "error: " + error;
}
};
BaseServer.prototype.onStart = function () {
};
BaseServer.prototype.onStop = function () {
};
/**
* 服务重置
*/
BaseServer.prototype.reset = function () {
this.stop();
this.start();
};
BaseServer = __decorate([
pao_aop_1.addon('BaseServer', '基础服务器', '所有服务器的基类')
], BaseServer);
return BaseServer;
}(pao_aop_1.BaseAddon));
exports.BaseServer = BaseServer;
/**
* 服务数据
*/
var ServerData = /** @class */ (function () {
function ServerData() {
}
return ServerData;
}());
exports.ServerData = ServerData;
/**
* 名称: 服务管理接口
* @description 提供服务管理功能的接口
* @author pao
*/
var IServerManagement = /** @class */ (function (_super) {
__extends(IServerManagement, _super);
function IServerManagement() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* 获取服务器列表
*/
IServerManagement.prototype.getServers = function () {
return undefined;
};
/**
* 获取服务明细信息
*/
IServerManagement.prototype.getServerDetailInfo = function () {
return undefined;
};
/**
* 启动服务
*/
IServerManagement.prototype.startServer = function (serverName) {
return undefined;
};
/**
* 停止服务
*/
IServerManagement.prototype.stopServer = function (serverName) {
return undefined;
};
IServerManagement = __decorate([
pao_aop_1.addon('IServerManagement', '服务管理接口', '提供服务管理功能的接口')
], IServerManagement);
return IServerManagement;
}(pao_aop_1.BaseAddon));
exports.IServerManagement = IServerManagement;
});
//# sourceMappingURL=index.js.map