mm_os
Version:
这是超级美眉服务端框架,用于快速构建应用程序。
306 lines (282 loc) • 6.68 kB
JavaScript
require('mm_logs');
require('mm_ret');
$.Tpl = require('mm_tpl');
$.mongodb_admin = require("mm_mongodb").mongoDB_admin;
$.redis_admin = require("mm_redis").redis_admin;
$.mysql_admin = require('mm_mysql').mysql_admin;
const conf = require("mm_config");
var Com = require("./lib/com.js");
var WEB = require("./core/base/web/index.js");
var MQTT = require("./core/base/mqtt/index.js");
/**
* 服务端系统
*/
class OS {
/**
* 构造函数
* @param {Object} config 配置参数
*/
constructor(config) {
this.config = {
"runPath": "",
"sys": {
"name": "mm",
"title": "超级美眉",
"cache": "memory",
"lang": "zh_CN",
"game": false,
"task": false,
"hot_reload": true,
"log": false
},
"web": {
"state": true,
"host": "0.0.0.0",
"port": 8000,
"socket": true,
"compress": true,
"event": true,
"log": true,
"static": true,
"maxAge": 7200,
"static_path": "./static",
"proxy": {}
},
"mqtt": {
"state": true,
"socket_port": 1883,
"http_port": 8083,
"cache": "redis",
"db": 12
},
"mqtt_client": {
"state": true,
"host": "127.0.0.1",
"port": "8083",
"protocol": "ws",
"clientId": "system",
"username": "admin",
"password": "asd123",
"clean": false
},
"redis": {
"host": "127.0.0.1",
"port": 6379,
"password": "asd159357",
"database": 0,
"prefix": "mm_"
},
"mongodb": {
"host": "127.0.0.1",
"port": 27017,
"database": "mm",
"user": "admin",
"password": "asd123"
}
};
this.init(config);
}
}
/**
* 初始化数据
* @param {Object} config 配置参数
*/
OS.prototype.initData = function(config) {
var p = config.runPath;
if (!p) {
config.runPath = $.runPath
} else if (p.substring(p.length - 1) !== $.slash) {
config.runPath += $.slash;
}
$.runPath = config.runPath;
// 初始化公共函数
$.func = {};
// 初始化变量
$.var = {};
$.conf = conf(null, '/conf.json'.fullname());
$.config = config;
// 修改默认模板路径,改为static下,方便读取css和js
$.Tpl.prototype.init_main = function(cg) {
if (cg) {
if (!cg.default_dir) {
cg.default_dir = "./template/".fullname(config.web.static_path)
}
} else {
cg = {
default_dir: "./template/".fullname(config.web.static_path)
}
}
this.set_config(cg);
this.dir = cg.default_dir;
}
}
/**
* 加载核心模块
* @param {Object} cg 配置参数
*/
OS.prototype.loadModule = function(cg) {
$.com = new Com();
this.com = $.com;
$.task = $.task_admin('sys');
if (cg.sys) {
var cache = cg.sys.cache;
// 选择缓存方式,默认memory缓存
if (cache === 'redis') {
// 将Api的缓存改为redis方式,如果不用redis可以将以下4行注释掉
var redis = $.redis_admin('sys');
redis.setConfig(cg.redis);
redis.open();
$.cache = redis;
} else if (cache === 'cache') {
// 将Api的缓存改为cache方式, 本地缓存方式
$.cache_admin = require('mm_cache').cache_admin;
$.push($.cache, $.cache_admin('sys'), true);
} else if (cache === 'mongodb') {
var mongodb = $.mongodb_admin('sys');
mongodb.setConfig(cg.mongodb);
mongodb.open();
$.cache = mongodb;
}
}
$.sql = $.mysql_admin('sys', $.runPath);
if (cg.mysql) {
$.sql.setConfig(cg.mysql);
}
if (!cg.sys.hot_reload) {
$.mod.config.watch = false;
}
}
/**
* 初始化全局错误处理机制
*/
OS.prototype.initErrorHandler = function() {
// 全局未捕获异常处理
process.on('uncaughtException', (error) => {
if ($.log && $.log.error) {
$.log.error('全局未捕获异常:', error);
} else {
console.error('全局未捕获异常:', error);
}
});
// 全局未处理Promise拒绝处理
process.on('unhandledRejection', (reason, promise) => {
if ($.log && $.log.error) {
$.log.error('全局未处理Promise拒绝:', reason);
} else {
console.error('全局未处理Promise拒绝:', reason);
}
});
};
/**
* 初始化
* @param {Object} config - 配置参数
*/
OS.prototype.init = function(config) {
this.config = Object.assign(this.config, config);
this.initData(this.config);
this.loadModule(this.config);
// 提供简单的日志功能替代方案
if (!$.log) {
$.log = {
debug: console.debug,
info: console.info,
warn: console.warn,
error: console.error,
log: console.log
};
}
this.initBase(this.config);
// 初始化全局错误处理机制
this.initErrorHandler();
}
/**
* 运行基础数据
* @param {Object} cg - 配置对象
*/
OS.prototype.initBase = function(cg) {
var middleware = $.middleware;
middleware.update();
var mqtt, web;
if (cg.web && cg.web.state) {
web = new WEB(cg.web);
web.list = middleware.list.filter((o) => {
return !o.mode || o.mode == "web"
});
web.init();
this.web = web;
}
if (cg.mqtt && cg.mqtt.state) {
mqtt = new MQTT(Object.assign(cg.mqtt, {
redis: cg.redis,
mongodb: cg.mongodb
}));
mqtt.list = middleware.list.filter((o) => {
return o.mode == "mqtt"
});
mqtt.init();
this.mqtt = mqtt;
}
}
/**
* 运行主程序
* @param {String} state 状态
*/
OS.prototype.main = async function(state) {
// 启动计时器
$.timer.run();
var cg = this.config;
var web_server = this.web_server;
var mqtt_server = this.mqtt_server;
var tip = "启动";
if (cg.web && cg.web.state) {
tip += " web";
if (cg.web.socket) {
tip += " socket";
}
var eventer = $.event_admin('api');
eventer.update();
this.web.run();
}
if (cg.mqtt && cg.mqtt.state) {
this.mqtt.run();
tip += " mqtt";
}
if (cg.sys && cg.sys.task) {
$.task.update().then(() => {
$.task.run();
});
tip += " task";
}
console.log(tip);
};
/**
* 运行主程序前
* @param {String} state 状态
*/
OS.prototype.before = async function(state) {
var mysql = this.config.mysql;
if (mysql && mysql.state) {
await $.sql.open();
}
this.app = $.app_admin('sys');
await this.app.update();
};
/**
* 运行主程序后
* @param {String} state 状态
*/
OS.prototype.after = async function(state) {
this.app.exec(null, 'init').then(() => {
this.app.run(null, 'start');
});
};
/**
* 运行
* @param {String} state 状态
*/
OS.prototype.run = async function(state = 'start') {
await this.before(state);
await this.main(state);
await this.after(state);
};
module.exports = OS;