mm_os 2.9.0 → 2.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/core/com/api/drive.js +9 -89
  2. package/core/com/api/index.js +3 -31
  3. package/core/com/cmd/drive.js +10 -1
  4. package/core/com/cmd/index.js +15 -1
  5. package/core/com/component/drive.js +1 -1
  6. package/core/com/component/index.js +16 -12
  7. package/core/com/db/drive.js +1 -1
  8. package/core/com/db/index.js +15 -17
  9. package/core/com/event/drive.js +3 -1
  10. package/core/com/event/index.js +74 -61
  11. package/core/com/eventer/com.js +2 -2
  12. package/core/com/middleware/com.js +41 -16
  13. package/core/com/mqtt/drive.js +4 -3
  14. package/core/com/mqtt/index.js +8 -6
  15. package/core/com/nav/drive.js +1 -19
  16. package/core/com/nav/index.js +1 -1
  17. package/core/com/param/drive.js +1 -1
  18. package/core/com/param/index.js +1 -27
  19. package/core/com/plugin/drive.js +18 -19
  20. package/core/com/plugin/index.js +16 -21
  21. package/core/com/plugin/script.js +109 -58
  22. package/core/com/rpc/com.json +4 -0
  23. package/core/com/rpc/drive.js +160 -0
  24. package/core/com/rpc/index.js +87 -0
  25. package/core/com/socket/drive.js +0 -1
  26. package/core/com/socket/index.js +5 -15
  27. package/core/com/sql/drive.js +7 -7
  28. package/core/com/sql/index.js +4 -31
  29. package/core/com/static/drive.js +15 -52
  30. package/core/com/static/index.js +15 -7
  31. package/core/com/task/index.js +14 -36
  32. package/core/com/tpl/com.js +2 -2
  33. package/index.js +9 -8
  34. package/middleware/cors/index.js +1 -1
  35. package/middleware/cors/middleware.json +3 -1
  36. package/middleware/log/index.js +1 -1
  37. package/middleware/log/middleware.json +1 -1
  38. package/middleware/mqtt_base/index.js +1 -1
  39. package/middleware/mqtt_base/middleware.json +3 -1
  40. package/middleware/waf/index.js +1 -1
  41. package/middleware/waf/middleware.json +1 -1
  42. package/middleware/waf_ip/index.js +1 -1
  43. package/middleware/waf_ip/middleware.json +1 -1
  44. package/middleware/web_after/index.js +20 -0
  45. package/middleware/web_after/middleware.json +9 -0
  46. package/middleware/web_base/index.js +1 -1
  47. package/middleware/web_base/middleware.json +1 -1
  48. package/middleware/web_before/index.js +27 -0
  49. package/middleware/web_before/middleware.json +9 -0
  50. package/middleware/web_check/index.js +22 -0
  51. package/middleware/web_check/middleware.json +9 -0
  52. package/middleware/web_main/index.js +22 -0
  53. package/middleware/web_main/middleware.json +9 -0
  54. package/middleware/web_proxy/index.js +1 -1
  55. package/middleware/web_proxy/middleware.json +1 -1
  56. package/middleware/web_render/index.js +81 -0
  57. package/middleware/web_render/middleware.json +9 -0
  58. package/middleware/web_socket/index.js +1 -1
  59. package/middleware/web_socket/middleware.json +1 -1
  60. package/middleware/web_static/index.js +1 -1
  61. package/middleware/web_static/middleware.json +1 -1
  62. package/package.json +2 -2
  63. package/demo/index.js +0 -28
  64. package/middleware/web_event/index.js +0 -413
  65. package/middleware/web_event/middleware.json +0 -10
  66. package/middleware/web_router/index.js +0 -33
  67. package/middleware/web_router/middleware.json +0 -10
  68. /package/core/com/{api → rpc}/rpc.js +0 -0
@@ -1,7 +1,7 @@
1
- const Item = require('mm_machine').Item;
2
-
3
1
  const fs = require("fs");
4
2
  const send = require('koa-send');
3
+ const conf = require('mm_config');
4
+ const Item = require('mm_machine').Item;
5
5
 
6
6
  /**
7
7
  * Plugin插件驱动类
@@ -15,10 +15,11 @@ class Drive extends Item {
15
15
  * @param {Object} config 配置
16
16
  * @constructor
17
17
  */
18
- constructor(dir, config) {
18
+ constructor(dir) {
19
19
  super(dir, __dirname);
20
20
  this.default_file = "./static.json";
21
-
21
+ // 更新并重载
22
+ this.mode = 3;
22
23
  /* 通用项 */
23
24
  // 配置参数
24
25
  this.config = {
@@ -57,25 +58,6 @@ class Drive extends Item {
57
58
  // 是否将ES6转换AMD
58
59
  "convert_amd": true
59
60
  };
60
-
61
- if (config) {
62
- $.push(this.config, config);
63
- this.dir = config.root.fullname();
64
- var f = this.default_file.fullname(this.dir);
65
- if (f.hasFile()) {
66
- var obj = f.loadJson();
67
- if (obj) {
68
- this.loadObj(obj);
69
- } else {
70
- var text = "./config.tpl.json".loadText(__dirname);
71
- text = text.replace('"path": "/"', '"path": "' + config.path + '"');
72
- f.saveText(text);
73
- }
74
- }
75
- }
76
- if (this.config.convert_amd) {
77
- this.run_mode(true);
78
- }
79
61
  }
80
62
  }
81
63
 
@@ -85,36 +67,17 @@ class Drive extends Item {
85
67
  Drive.prototype.send = send;
86
68
 
87
69
  /**
88
- * 加载配置对象
89
- * @param {Object} obj 配置对象
70
+ * 加载配置对象前
71
+ * @param {Object} config 配置对象
90
72
  */
91
- Drive.prototype.loadObj = function(obj) {
92
- if (obj.maxAge) {
93
- obj.maxAge *= 1000;
94
- }
95
- $.push(this.config, obj, true);
96
- var cg = this.config;
97
- if (cg.convert_amd) {
98
- this.run_mode(true);
99
- }
100
- var f = cg.func_file;
101
- if (f) {
102
- var file = f.fullname(this.dir);
103
- if (file.hasFile()) {
104
- var cs = require(file);
105
- if (cs) {
106
- var name = cg.func_name;
107
- if (name) {
108
- this.main = cs[name];
109
- } else {
110
- $.push(this, cs);
111
- }
112
- }
113
- } else {
114
- var fl = __dirname + "/script.js";
115
- fl.copyFile(file);
73
+ Drive.prototype.set_config = function(config) {
74
+ if (config) {
75
+ if (config.maxAge) {
76
+ config.maxAge *= 1000;
116
77
  }
117
78
  }
79
+ this.config = conf(Object.assign({}, this.config, config || {}), this.filename);
80
+ this.run_mode(this.config.convert_amd);
118
81
  };
119
82
 
120
83
  /**
@@ -150,7 +113,7 @@ Drive.prototype.run_mode = async function(convert_amd) {
150
113
  if (convert_amd) {
151
114
  this.main = async function main(ctx, path) {
152
115
  if (path.endsWith('.vue') || path.endsWith('.js')) {
153
- var file = (this.dir + path).fullname();
116
+ var file = ("." + path).fullname(this.dir);
154
117
  var code;
155
118
  var str = file.loadText();
156
119
  try {
@@ -223,7 +186,7 @@ Drive.prototype.run = async function(ctx, path, next) {
223
186
 
224
187
  // 取到物理路径
225
188
  var root = this.dir;
226
- var dir = (root + p).fullname();
189
+ var dir = p.fullname(root);
227
190
  if (!p) {
228
191
  file = dir + '/' + cg.index;
229
192
  } else if (p.endWith('/')) {
@@ -21,6 +21,9 @@ class Static extends Index {
21
21
  constructor(scope, title) {
22
22
  super(scope, __dirname);
23
23
  this.Drive = Drive;
24
+
25
+ // 更新并重载
26
+ this.mode = 3;
24
27
  this.type = "static";
25
28
  this.title = title;
26
29
  var $this = this;
@@ -87,8 +90,8 @@ class Static extends Index {
87
90
  * @param {Object} dir 目录
88
91
  * @return {Drive} 静态文件驱动类
89
92
  */
90
- Static.prototype.getObj = function(dir) {
91
- var d = join(dir).replace($.runPath, '/');
93
+ Static.prototype.getObj = async function(dir) {
94
+ var d = join(dir).replace(process.cwd() + $.slash, '/');
92
95
  var app = d.between(join('app/'), join('/'));
93
96
  var plugin = d.between(join('/plugin/'), join('/'));
94
97
  var path = '/' + app;
@@ -101,14 +104,18 @@ Static.prototype.getObj = function(dir) {
101
104
  root: d.substring(0, d.length - 1).fullname(),
102
105
  path: path
103
106
  };
104
- return new Drive(dir, obj);
107
+ var file = ("./" + this.type + ".json").fullname(obj.root);
108
+ var drive = new Drive(dir, obj);
109
+ await drive.exec('load_config', file);
110
+ await drive.exec('set_config', obj);
111
+ return drive
105
112
  };
106
113
 
107
114
  /**
108
115
  * 加载接口
109
116
  * @param {String} path 加载的路径
110
117
  */
111
- Static.prototype.load = function(path) {
118
+ Static.prototype.update_config_all = async function(path) {
112
119
  if (!path) {
113
120
  path = $.runPath + 'app' + $.slash;
114
121
  }
@@ -117,10 +124,11 @@ Static.prototype.load = function(path) {
117
124
  var list_scope = $.dir.getAll(path, search_dir);
118
125
  // 遍历目录路径
119
126
  var _this = this;
120
- list_scope.map(function(o) {
121
- var obj = _this.getObj(o);
127
+ for (var i = 0; i < list_scope.length; i++) {
128
+ var dir = list_scope[i];
129
+ var obj = await _this.getObj(dir);
122
130
  _this.list.push(obj);
123
- });
131
+ }
124
132
  };
125
133
 
126
134
  /**
@@ -31,7 +31,7 @@ Task.prototype.run = async function(name) {
31
31
  for (var i = 0, o; o = lt[i++];) {
32
32
  if (o.config.state === 1 && o.config.name === name) {
33
33
  try {
34
- o.run();
34
+ await o.run();
35
35
  } catch (error) {
36
36
  $.log.error("定时任务错误", o.name, error);
37
37
  }
@@ -42,7 +42,7 @@ Task.prototype.run = async function(name) {
42
42
  for (var i = 0, o; o = lt[i++];) {
43
43
  if (o.config.state === 1) {
44
44
  try {
45
- o.run();
45
+ await o.run();
46
46
  } catch (error) {
47
47
  $.log.error("定时任务错误", o.name, error);
48
48
  }
@@ -57,44 +57,22 @@ Task.prototype.run = async function(name) {
57
57
  * @param {Object} cg 配置参数
58
58
  * @param {String} file 配置文件
59
59
  */
60
- Task.prototype.load_item = function(dir, cg, file) {
61
- var drive = new this.Drive(dir);
62
- drive.loadObj(cg);
63
- drive.filename = file;
60
+ Task.prototype.load_item = async function(dir, cg, file) {
64
61
  var _this = this;
62
+ var drive = new this.Drive(dir, this.dir_base.fullname());
63
+ drive.mode = this.mode;
64
+ if (cg) {
65
+ await drive.exec('load_config', file, cg.name);
66
+ await drive.exec('set_config', cg);
67
+ } else {
68
+ await drive.exec('load_config', file);
69
+ }
70
+
65
71
  drive.admin = function() {
66
- return this;
72
+ return _this;
67
73
  };
68
74
  this.list.push(drive);
69
- };
70
-
71
- /**
72
- * 加载列表
73
- * @param {Array} list 文件列表
74
- */
75
- Task.prototype.load_list = function(list) {
76
- var _this = this;
77
- // 遍历文件路径
78
- list.map(function(file) {
79
- var dir = file.dirname();
80
- // 载入文件
81
- var obj = file.loadJson(dir);
82
- if (obj) {
83
- if (obj.constructor == Array) {
84
- obj.map(function(o) {
85
- // 实例化一个驱动
86
- _this.load_item(dir, o, file);
87
- });
88
- } else {
89
- _this.load_item(dir, obj, file);
90
- }
91
- } else {
92
- var fl = _this.dir_base + "/config.tpl.json";
93
- if (fl.hasFile()) {
94
- fl.copyFile(file);
95
- }
96
- }
97
- });
75
+ return drive;
98
76
  };
99
77
 
100
78
  exports.Task = Task;
@@ -8,11 +8,11 @@ class Controller extends Base {
8
8
  super();
9
9
 
10
10
  // 检索的文件路径
11
- this.path = "./apps/controller".fullname();
11
+ this.path = "./app/controller".fullname();
12
12
  // 文件拓展名
13
13
  this.extension = "controller.json";
14
14
  // 文件存放目录
15
- this.dir = "./apps";
15
+ this.dir = "./app";
16
16
  }
17
17
  }
18
18
 
package/index.js CHANGED
@@ -87,15 +87,14 @@ class OS {
87
87
  */
88
88
  OS.prototype.initData = function(config) {
89
89
  var p = config.runPath;
90
- if(!p) {
90
+ if (!p) {
91
91
  config.runPath = $.runPath
92
- }
93
- else if (p.substring(p.length - 1) !== $.slash) {
92
+ } else if (p.substring(p.length - 1) !== $.slash) {
94
93
  config.runPath += $.slash;
95
94
  }
96
-
95
+
97
96
  $.runPath = config.runPath;
98
-
97
+
99
98
  // 修改默认模板路径,改为static下,方便读取css和js
100
99
  $.Tpl.prototype.init_main = function(cg) {
101
100
  if (cg) {
@@ -159,7 +158,7 @@ OS.prototype.init = function(config) {
159
158
  */
160
159
  OS.prototype.initBase = function(cg) {
161
160
  var middleware = $.middleware;
162
- middleware.init();
161
+ middleware.update();
163
162
  var mqtt, web;
164
163
  if (cg.web && cg.web.state) {
165
164
  web = new WEB(cg.web);
@@ -203,8 +202,8 @@ OS.prototype.initBase = function(cg) {
203
202
  }
204
203
  }
205
204
 
205
+ $.sql = $.mysql_admin('sys', $.runPath);
206
206
  if (cg.mysql) {
207
- $.sql = $.mysql_admin('sys', $.runPath);
208
207
  $.sql.setConfig(cg.mysql);
209
208
  if (cg.mysql.state === 1) {
210
209
  $.sql.open();
@@ -215,7 +214,6 @@ OS.prototype.initBase = function(cg) {
215
214
  $.timer.run();
216
215
  }
217
216
 
218
-
219
217
  /**
220
218
  * 运行主程序
221
219
  * @param {String} state 状态
@@ -230,6 +228,8 @@ OS.prototype.main = async function(state) {
230
228
  if (cg.web.socket) {
231
229
  tip += " socket";
232
230
  }
231
+ var eventer = $.event_admin('api');
232
+ eventer.update();
233
233
  this.web.run();
234
234
  }
235
235
 
@@ -242,6 +242,7 @@ OS.prototype.main = async function(state) {
242
242
  $.task.update().then(() => {
243
243
  $.task.run();
244
244
  });
245
+ tip += " task";
245
246
  }
246
247
  console.log(tip);
247
248
  };
@@ -1,5 +1,5 @@
1
1
  /**
2
- * 应用
2
+ * 跨域请求
3
3
  * @param {Object} server 服务
4
4
  * @param {Object} config 配置参数
5
5
  */
@@ -3,5 +3,7 @@
3
3
  "title": "web跨域请求",
4
4
  "description": "用于跨域请求限制",
5
5
  "version": "1.0",
6
- "sort": 4
6
+ "type": "web",
7
+ "process_type": "common_before",
8
+ "sort": 50
7
9
  }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * 应用
2
+ * web日志
3
3
  * @param {Object} server 服务
4
4
  * @param {Object} config 配置参数
5
5
  */
@@ -5,5 +5,5 @@
5
5
  "version": "1.0",
6
6
  "type": "web",
7
7
  "process_type": "common_before",
8
- "sort": 13
8
+ "sort": 40
9
9
  }
@@ -1,7 +1,7 @@
1
1
  const MQTT = require('mm_mqtt');
2
2
 
3
3
  /**
4
- * 中间件
4
+ * MQTT基础
5
5
  * @param {Object} server 服务
6
6
  * @param {Object} config 配置参数
7
7
  */
@@ -4,5 +4,7 @@
4
4
  "title": "MQTT通讯器",
5
5
  "description": "用于MQTT通讯",
6
6
  "version": "1.0",
7
- "sort": 3
7
+ "type": "web",
8
+ "process_type": "common_before",
9
+ "sort": 30
8
10
  }
@@ -44,7 +44,7 @@ function getClientIP(req) {
44
44
  };
45
45
 
46
46
  /**
47
- * 应用
47
+ * web防火墙
48
48
  * @param {Object} server 服务
49
49
  * @param {Object} config 配置参数
50
50
  */
@@ -5,5 +5,5 @@
5
5
  "version": "1.0",
6
6
  "type": "web",
7
7
  "process_type": "common_before",
8
- "sort": 3
8
+ "sort": 20
9
9
  }
@@ -48,7 +48,7 @@ function setting_blacklist(ip) {
48
48
  }
49
49
 
50
50
  /**
51
- * 应用
51
+ * IP防火墙
52
52
  * @param {Object} server 服务
53
53
  * @param {Object} config 配置参数
54
54
  */
@@ -5,6 +5,6 @@
5
5
  "version": "1.0",
6
6
  "type": "web",
7
7
  "process_type": "common_before",
8
- "sort": 1,
8
+ "sort": 10,
9
9
  "state": 0
10
10
  }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * web请求之后
3
+ * @param {Object} server 服务
4
+ * @param {Object} config 配置参数
5
+ */
6
+ module.exports = function(server, config) {
7
+ server.use(async (ctx, next) => {
8
+ await next();
9
+ if (ctx.path !== "/favicon.ico") {
10
+ await $.eventer.run('web_after', ctx, ctx.db);
11
+ var event = $.event_admin('api');
12
+ var ret = await event.after(ctx.path, ctx, ctx.db);
13
+ if (ret) {
14
+ ctx.body = ret;
15
+ }
16
+ }
17
+ });
18
+
19
+ return server;
20
+ };
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "web_after",
3
+ "title": "web请求后",
4
+ "description": "用于追加处理",
5
+ "version": "1.0",
6
+ "type": "web",
7
+ "process_type": "common_before",
8
+ "sort": 150
9
+ }
@@ -5,7 +5,7 @@ const { koaBody } = require('koa-body');
5
5
  const send = require('koa-send');
6
6
 
7
7
  /**
8
- * 应用
8
+ * web基础
9
9
  * @param {Object} server 服务
10
10
  * @param {Object} config 配置参数
11
11
  */
@@ -5,5 +5,5 @@
5
5
  "version": "1.0",
6
6
  "type": "web",
7
7
  "process_type": "common_before",
8
- "sort": 12
8
+ "sort": 70
9
9
  }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * web请求之前
3
+ * @param {Object} server 服务
4
+ * @param {Object} config 配置参数
5
+ */
6
+ module.exports = function(server, config) {
7
+ // 使用路由(主要)
8
+ server.use(async (ctx, next) => {
9
+ if (!ctx.db) {
10
+ ctx.db = {
11
+ ret: null
12
+ };
13
+ }
14
+ if (ctx.path !== "/favicon.ico") {
15
+ await $.eventer.run('web_before', ctx, ctx.db);
16
+ var event = $.event_admin('api');
17
+ var ret = await event.before(ctx.path, ctx, ctx.db);
18
+ // console.log("before阶段", ret);
19
+ if (ret) {
20
+ ctx.body = ret;
21
+ }
22
+ }
23
+ await next();
24
+ });
25
+
26
+ return server;
27
+ };
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "web_berofe",
3
+ "title": "web请求前",
4
+ "description": "用于",
5
+ "version": "1.0",
6
+ "type": "web",
7
+ "process_type": "common_before",
8
+ "sort": 80
9
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * web验证
3
+ * @param {Object} server 服务
4
+ * @param {Object} config 配置参数
5
+ */
6
+ module.exports = function(server, config) {
7
+ // 使用路由(主要)
8
+ server.use(async (ctx, next) => {
9
+ if (ctx.path !== "/favicon.ico") {
10
+ await $.eventer.run('web_check', ctx, ctx.db);
11
+ var event = $.event_admin('api');
12
+ var ret = await event.check(ctx.path, ctx, ctx.db);
13
+ // console.log("check阶段", ret);
14
+ if (ret) {
15
+ ctx.body = ret;
16
+ }
17
+ }
18
+ await next();
19
+ });
20
+
21
+ return server;
22
+ };
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "web_check",
3
+ "title": "web请求验证",
4
+ "description": "用于验证请求,拦截不符合调条件的请求",
5
+ "version": "1.0",
6
+ "type": "web",
7
+ "process_type": "common_before",
8
+ "sort": 90
9
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * web请求
3
+ * @param {Object} server 服务
4
+ * @param {Object} config 配置参数
5
+ */
6
+ module.exports = function(server, config) {
7
+ // 使用路由(主要)
8
+ server.use(async (ctx, next) => {
9
+ if (ctx.path !== "/favicon.ico") {
10
+ await $.eventer.run('web_main', ctx, ctx.db);
11
+ var event = $.event_admin('api');
12
+ var ret = await event.main(ctx.path, ctx, ctx.db);
13
+ // console.log("main阶段", ret);
14
+ if (ret) {
15
+ ctx.body = ret;
16
+ }
17
+ }
18
+ await next();
19
+ });
20
+
21
+ return server;
22
+ };
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "web_main",
3
+ "title": "web请求主要",
4
+ "description": "用于处理主要请求",
5
+ "version": "1.0",
6
+ "type": "web",
7
+ "process_type": "common_before",
8
+ "sort": 100
9
+ }
@@ -5,7 +5,7 @@ const {
5
5
  } = require('mm_koa_proxy');
6
6
 
7
7
  /**
8
- * 应用
8
+ * 代理请求
9
9
  * @param {Object} server 服务
10
10
  * @param {Object} config 配置参数
11
11
  */
@@ -5,5 +5,5 @@
5
5
  "version": "1.0",
6
6
  "type": "web",
7
7
  "process_type": "worker",
8
- "sort": 6
8
+ "sort": 110
9
9
  }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * 调用函数
3
+ * @param {Object} ret 设置响应结果
4
+ * @param {Object} res 响应器
5
+ * @param {String} t 请求类型
6
+ * @return {String} 处理后的响应结果
7
+ */
8
+ function render_body(ret, res, t) {
9
+ var type;
10
+ if (res.type) {
11
+ type = res.type;
12
+ }
13
+ var tp = typeof(ret);
14
+ if (tp === "object") {
15
+ if (!type) {
16
+ /// 设置响应头
17
+ if (t.indexOf('xml') !== -1) {
18
+ type = t;
19
+ } else {
20
+ type = "application/json; charset=utf-8";
21
+ }
22
+ res.type = type;
23
+ } else {
24
+ if (type.indexOf('json') !== -1) {
25
+ type = "application/json; charset=utf-8";
26
+ } else if (type.indexOf('html') !== -1) {
27
+ type = "text/html";
28
+ } else if (type.indexOf('xml') !== -1) {
29
+ type = "text/xml; charset=utf-8";
30
+ } else {
31
+ type = "text/plain; charset=utf-8";
32
+ }
33
+ res.type = type;
34
+ }
35
+
36
+ if (type.indexOf('/xml') !== -1) {
37
+ return $.toXml(ret);
38
+ } else {
39
+ return JSON.stringify(ret);
40
+ }
41
+ } else if (tp === "string") {
42
+ ret = ret.trim();
43
+ if (!type) {
44
+ if (ret.startWith('{') && ret.endWith('}')) {
45
+ res.type = "application/json; charset=utf-8";
46
+ } else if (ret.startWith('[') && ret.endWith(']')) {
47
+ res.type = "application/json; charset=utf-8";
48
+ } else if (ret.endWith("</html>")) {
49
+ res.type = "text/html";
50
+ } else {
51
+ res.type = "text/plain; charset=utf-8";
52
+ }
53
+ }
54
+ }
55
+ return ret;
56
+ };
57
+
58
+ /**
59
+ * web渲染
60
+ * @param {Object} server 服务
61
+ * @param {Object} config 配置参数
62
+ */
63
+ module.exports = function(server, config) {
64
+ // 使用路由(主要)
65
+ server.use(async (ctx, next) => {
66
+ if (ctx.path !== "/favicon.ico") {
67
+ await $.eventer.run('web_render', ctx, ctx.db);
68
+ var event = $.event_admin('api');
69
+ var ret = await event.render(ctx.path, ctx, ctx.db);
70
+ if (!ctx.body && ret) {
71
+ ctx.body = ret;
72
+ }
73
+ if (ctx.body) {
74
+ ctx.body = render_body(ctx.body, ctx.response, ctx.request.type);
75
+ }
76
+ }
77
+ await next();
78
+ });
79
+
80
+ return server;
81
+ };
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "web_render",
3
+ "title": "web请求渲染",
4
+ "description": "用于响应请求是加工响应的数据",
5
+ "version": "1.0",
6
+ "type": "web",
7
+ "process_type": "common_before",
8
+ "sort": 120
9
+ }