mm_os 3.3.1 → 4.0.1
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.
- package/LICENSE +21 -201
- package/README.md +498 -99
- package/README_EN.md +505 -0
- package/adapter/adapter.js +431 -0
- package/adapter/custom_persistence.js +660 -0
- package/adapter/mqtt.js +273 -0
- package/adapter/socket.js +113 -0
- package/adapter/web.js +67 -0
- package/adapter/websocket.js +146 -0
- package/com/api/com.json +5 -0
- package/{core/com → com}/api/config.tpl.json +8 -8
- package/com/api/drive.js +708 -0
- package/com/api/index.js +198 -0
- package/com/api/oauth.js +200 -0
- package/com/api/script.tpl.js +32 -0
- package/com/cmd/README.md +11 -0
- package/com/cmd/com.json +5 -0
- package/com/cmd/config.tpl.json +122 -0
- package/com/cmd/drive.js +1548 -0
- package/com/cmd/index.js +1066 -0
- package/com/cmd/msg.json +48 -0
- package/com/cmd/nlp.js +525 -0
- package/com/cmd/script.tpl.js +32 -0
- package/com/db/com.json +5 -0
- package/com/db/drive.js +1999 -0
- package/com/db/index.js +242 -0
- package/{core/com → com}/event/README.md +4 -4
- package/com/event/com.json +5 -0
- package/{core/com → com}/event/config.tpl.json +18 -18
- package/com/event/drive.js +59 -0
- package/com/event/index.js +409 -0
- package/com/event/script.tpl.js +23 -0
- package/com/mqtt/com.json +5 -0
- package/{core/com → com}/mqtt/config.tpl.json +3 -5
- package/com/mqtt/drive.js +676 -0
- package/com/mqtt/index.js +822 -0
- package/com/mqtt/mm_mqtt.js +425 -0
- package/com/mqtt/script.tpl.js +723 -0
- package/com/nav/com.json +5 -0
- package/com/nav/config.tpl.json +84 -0
- package/com/nav/drive.js +702 -0
- package/com/nav/index.js +231 -0
- package/{core/com → com}/nav/tpl/admin_pc/page_config.vue +280 -280
- package/{core/com → com}/nav/tpl/admin_pc/page_config_form.vue +194 -194
- package/com/nav/tpl/admin_pc/page_form.vue +180 -0
- package/com/nav/tpl/admin_pc/page_view.vue +124 -0
- package/com/nav/tpl/dev_pc/page_default.vue +247 -0
- package/com/nav/tpl/dev_pc/page_type.vue +313 -0
- package/com/nav/tpl/home_pc/page_default.vue +234 -0
- package/com/nav/tpl/home_pc/page_form.vue +137 -0
- package/com/nav/tpl/home_pc/page_list.vue +234 -0
- package/com/nav/tpl/home_pc/page_nav.vue +221 -0
- package/com/nav/tpl/home_pc/page_type.vue +234 -0
- package/com/nav/tpl/home_pc/page_view.vue +125 -0
- package/com/nav/tpl/home_phone/page_channel.vue +234 -0
- package/com/nav/tpl/home_phone/page_default.vue +234 -0
- package/com/nav/tpl/home_phone/page_form.vue +137 -0
- package/com/nav/tpl/home_phone/page_nav.vue +237 -0
- package/com/nav/tpl/home_phone/page_type.vue +234 -0
- package/com/nav/tpl/home_phone/page_view.vue +125 -0
- package/com/nav/viewmodel.js +446 -0
- package/com/param/com.json +5 -0
- package/{core/com → com}/param/config.tpl.json +7 -1
- package/com/param/drive.js +502 -0
- package/com/param/index.js +155 -0
- package/com/param/script.tpl.js +12 -0
- package/com/pendant/com.json +5 -0
- package/{core/com/component → com/pendant}/config.tpl.json +15 -13
- package/com/pendant/drive.js +204 -0
- package/com/pendant/index.js +441 -0
- package/com/pendant/pendant.html +16 -0
- package/com/pendant/script.tpl.js +18 -0
- package/com/socket/com.json +5 -0
- package/com/socket/config.tpl.json +12 -0
- package/com/socket/drive.js +651 -0
- package/com/socket/index.js +351 -0
- package/com/socket/script.tpl.js +41 -0
- package/com/sql/com.json +5 -0
- package/{core/com → com}/sql/config.tpl.json +13 -9
- package/com/sql/drive.js +1259 -0
- package/com/sql/index.js +150 -0
- package/com/sql/script.tpl.js +47 -0
- package/com/static/com.json +5 -0
- package/{core/com → com}/static/config.tpl.json +10 -6
- package/com/static/drive.js +194 -0
- package/com/static/index.js +226 -0
- package/com/static/script.tpl.js +28 -0
- package/com/task/com.json +5 -0
- package/{core/com → com}/task/config.tpl.json +4 -6
- package/com/task/drive.js +405 -0
- package/com/task/index.js +148 -0
- package/com/task/script.tpl.js +37 -0
- package/com/template/com.json +5 -0
- package/com/template/config.tpl.json +16 -0
- package/com/template/drive.js +80 -0
- package/com/template/index.js +141 -0
- package/com.js +156 -0
- package/common/README.md +2 -0
- package/common/handler/msg/handler.json +22 -0
- package/common/handler/msg/index.js +23 -0
- package/common/handler/player/handler.json +22 -0
- package/common/handler/player/index.js +287 -0
- package/common/handler/user/handler.json +22 -0
- package/common/handler/user/index.js +23 -0
- package/common/middleware/web_after/index.js +29 -0
- package/common/middleware/web_after/middleware.json +9 -0
- package/common/middleware/web_base/index.js +113 -0
- package/common/middleware/web_base/middleware.json +19 -0
- package/common/middleware/web_before/index.js +33 -0
- package/common/middleware/web_before/middleware.json +9 -0
- package/common/middleware/web_cors/index.js +87 -0
- package/common/middleware/web_cors/middleware.json +24 -0
- package/common/middleware/web_error/index.js +119 -0
- package/common/middleware/web_error/middleware.json +18 -0
- package/common/middleware/web_ip/index.js +15 -0
- package/common/middleware/web_ip/middleware.json +14 -0
- package/common/middleware/web_logger/index.js +156 -0
- package/common/middleware/web_logger/middleware.json +14 -0
- package/common/middleware/web_main/index.js +24 -0
- package/common/middleware/web_main/middleware.json +9 -0
- package/common/middleware/web_static/index.js +73 -0
- package/common/middleware/web_static/middleware.json +54 -0
- package/common/middleware/web_waf/index.js +385 -0
- package/common/middleware/web_waf/middleware.json +13 -0
- package/common/model/msg/index.js +88 -0
- package/common/model/msg/model.json +401 -0
- package/common/model/player/index.js +63 -0
- package/common/model/player/model.json +185 -0
- package/common/model/user/index.js +11 -0
- package/common/model/user/model.json +219 -0
- package/core/app/config.tpl.json +67 -0
- package/core/app/index.js +632 -0
- package/core/app/script.tpl.js +52 -0
- package/core/channel/index.js +899 -0
- package/core/channel/matcher.js +585 -0
- package/core/com/config.tpl.json +16 -0
- package/core/com/index.js +74 -0
- package/core/com/script.tpl.js +5 -0
- package/core/component/component.js +42 -0
- package/core/component/config.tpl.json +63 -0
- package/core/component/index.js +273 -0
- package/core/component/script.tpl.js +19 -0
- package/core/controller/config.tpl.json +14 -0
- package/core/controller/index.js +373 -0
- package/core/controller/script.tpl.js +27 -0
- package/core/factory/config.tpl.json +14 -0
- package/core/factory/entity.js +275 -0
- package/core/factory/index.js +241 -0
- package/core/factory/script.tpl.js +16 -0
- package/core/game/bat/index.js +137 -0
- package/core/game/bat/world.js +622 -0
- package/core/game/config.tpl.json +16 -0
- package/core/game/entity_admin.js +230 -0
- package/core/game/index.js +186 -0
- package/core/handler/config.tpl.json +22 -0
- package/core/handler/index.js +181 -0
- package/core/handler/script.tpl.js +23 -0
- package/core/logic/config.tpl.json +14 -0
- package/core/logic/index.js +59 -0
- package/core/logic/script.tpl.js +19 -0
- package/core/middleware/config.tpl.json +16 -0
- package/core/middleware/index.js +125 -0
- package/core/middleware/script.tpl.js +37 -0
- package/core/mod/config.tpl.json +22 -0
- package/core/mod/index.js +130 -0
- package/core/mod/script.tpl.js +34 -0
- package/core/model/config.tpl.json +219 -0
- package/core/model/index.js +272 -0
- package/core/model/model.js +27 -0
- package/core/model/script.tpl.js +20 -0
- package/core/notifier/config.tpl.json +14 -0
- package/core/notifier/index.js +77 -0
- package/core/notifier/script.tpl.js +20 -0
- package/core/plugin/config.tpl.json +24 -0
- package/core/plugin/index.js +232 -0
- package/core/plugin/script.tpl.js +51 -0
- package/core/pusher/config.tpl.json +14 -0
- package/core/pusher/index.js +161 -0
- package/core/pusher/script.tpl.js +20 -0
- package/core/room/bat/index.js +170 -0
- package/core/room/bat/room.js +524 -0
- package/core/room/config.tpl.json +20 -0
- package/core/room/index.js +249 -0
- package/core/room/room.js +61 -0
- package/core/scene/config.tpl.json +14 -0
- package/core/scene/index.js +466 -0
- package/core/scene/loop.js +1255 -0
- package/core/scene/map.js +28 -0
- package/core/scene/script.tpl.js +22 -0
- package/core/sender/config.tpl.json +14 -0
- package/core/sender/index.js +79 -0
- package/core/sender/script.tpl.js +20 -0
- package/core/service/config.tpl.json +14 -0
- package/core/service/index.js +100 -0
- package/core/service/script.tpl.js +25 -0
- package/core/store/config.tpl.json +26 -0
- package/core/store/index.js +1755 -0
- package/core/store/script.tpl.js +22 -0
- package/core/store/sql.js +1464 -0
- package/core/system/config.tpl.json +18 -0
- package/core/system/index.js +312 -0
- package/core/system/script.tpl.js +77 -0
- package/core/view/config.tpl.json +14 -0
- package/core/view/index.js +91 -0
- package/core/view/script.tpl.js +20 -0
- package/core/zone/bat/index.js +725 -0
- package/core/zone/config.tpl.json +54 -0
- package/core/zone/index.js +614 -0
- package/core/zone/script.tpl.js +10 -0
- package/core/zone/zone_bat.js +136 -0
- package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +24 -0
- package/index.js +17 -333
- package/os.js +57 -0
- package/package.json +65 -55
- package/server.js +598 -0
- package/README.en.md +0 -36
- package/conf.json +0 -3
- package/core/base/mqtt/index.js +0 -1110
- package/core/base/mqtt/lib.js +0 -40
- package/core/base/web/index.js +0 -245
- package/core/com/api/com.json +0 -4
- package/core/com/api/drive.js +0 -668
- package/core/com/api/index.js +0 -108
- package/core/com/api/oauth.js +0 -158
- package/core/com/api/script.js +0 -32
- package/core/com/app/README.md +0 -3
- package/core/com/app/com.json +0 -4
- package/core/com/app/config.tpl.json +0 -16
- package/core/com/app/drive.js +0 -309
- package/core/com/app/index.js +0 -211
- package/core/com/app/script.js +0 -155
- package/core/com/cmd/com.json +0 -4
- package/core/com/cmd/config.tpl.json +0 -66
- package/core/com/cmd/drive.js +0 -513
- package/core/com/cmd/index.js +0 -354
- package/core/com/cmd/old/5w2h.js +0 -54
- package/core/com/cmd/old/drive.js +0 -423
- package/core/com/cmd/script.js +0 -11
- package/core/com/component/README.md +0 -3
- package/core/com/component/com.json +0 -4
- package/core/com/component/component.html +0 -16
- package/core/com/component/drive.js +0 -197
- package/core/com/component/index.js +0 -312
- package/core/com/component/script.js +0 -18
- package/core/com/db/com.json +0 -4
- package/core/com/db/drive.js +0 -1160
- package/core/com/db/index.js +0 -176
- package/core/com/event/com.json +0 -4
- package/core/com/event/drive.js +0 -133
- package/core/com/event/index.js +0 -345
- package/core/com/event/script.js +0 -26
- package/core/com/eventer/com.js +0 -477
- package/core/com/eventer/com.json +0 -4
- package/core/com/middleware/com.js +0 -154
- package/core/com/middleware/com.json +0 -4
- package/core/com/middleware/config.tpl.json +0 -8
- package/core/com/middleware/script.js +0 -9
- package/core/com/mqtt/com.json +0 -4
- package/core/com/mqtt/drive.js +0 -600
- package/core/com/mqtt/index.js +0 -572
- package/core/com/mqtt/mm_mqtt.js +0 -330
- package/core/com/mqtt/script.js +0 -604
- package/core/com/msg/com.js +0 -296
- package/core/com/msg/com.json +0 -4
- package/core/com/nav/com.json +0 -4
- package/core/com/nav/config.tpl.json +0 -75
- package/core/com/nav/drive.js +0 -549
- package/core/com/nav/index.js +0 -182
- package/core/com/nav/tpl/admin_pc/page_form.vue +0 -180
- package/core/com/nav/tpl/admin_pc/page_view.vue +0 -124
- package/core/com/nav/tpl/dev_pc/page_default.vue +0 -247
- package/core/com/nav/tpl/dev_pc/page_type.vue +0 -313
- package/core/com/nav/tpl/home_pc/page_default.vue +0 -234
- package/core/com/nav/tpl/home_pc/page_form.vue +0 -137
- package/core/com/nav/tpl/home_pc/page_list.vue +0 -234
- package/core/com/nav/tpl/home_pc/page_nav.vue +0 -221
- package/core/com/nav/tpl/home_pc/page_type.vue +0 -234
- package/core/com/nav/tpl/home_pc/page_view.vue +0 -125
- package/core/com/nav/tpl/home_phone/page_channel.vue +0 -234
- package/core/com/nav/tpl/home_phone/page_default.vue +0 -234
- package/core/com/nav/tpl/home_phone/page_form.vue +0 -137
- package/core/com/nav/tpl/home_phone/page_nav.vue +0 -237
- package/core/com/nav/tpl/home_phone/page_type.vue +0 -234
- package/core/com/nav/tpl/home_phone/page_view.vue +0 -125
- package/core/com/nav/viewmodel.js +0 -296
- package/core/com/param/drive.js +0 -366
- package/core/com/param/index.js +0 -80
- package/core/com/param/script.js +0 -12
- package/core/com/param/test.js +0 -98
- package/core/com/plugin/README.md +0 -3
- package/core/com/plugin/com.json +0 -4
- package/core/com/plugin/config.tpl.json +0 -26
- package/core/com/plugin/drive.js +0 -536
- package/core/com/plugin/index.js +0 -259
- package/core/com/plugin/script.js +0 -213
- package/core/com/rpc/com.json +0 -4
- package/core/com/rpc/drive.js +0 -160
- package/core/com/rpc/index.js +0 -87
- package/core/com/rpc/rpc.js +0 -118
- package/core/com/socket/com.json +0 -4
- package/core/com/socket/config.tpl.json +0 -14
- package/core/com/socket/drive.js +0 -403
- package/core/com/socket/index.js +0 -62
- package/core/com/socket/script.js +0 -42
- package/core/com/sql/drive.js +0 -1087
- package/core/com/sql/index.js +0 -83
- package/core/com/sql/script.js +0 -48
- package/core/com/static/com.json +0 -4
- package/core/com/static/drive.js +0 -220
- package/core/com/static/index.js +0 -149
- package/core/com/static/script.js +0 -28
- package/core/com/task/com.json +0 -4
- package/core/com/task/drive.js +0 -403
- package/core/com/task/index.js +0 -110
- package/core/com/task/script.js +0 -37
- package/core/com/timer/com.js +0 -217
- package/core/com/timer/com.json +0 -4
- package/core/com/tpl/com.js +0 -19
- package/core/com/tpl/com.json +0 -4
- package/lib/actions.js +0 -50
- package/lib/base.js +0 -361
- package/lib/com.js +0 -29
- package/lib/ref.js +0 -121
- package/middleware/cors/index.js +0 -119
- package/middleware/cors/middleware.json +0 -20
- package/middleware/csrf/index.js +0 -202
- package/middleware/csrf/middleware.json +0 -24
- package/middleware/ip_firewall/index.js +0 -476
- package/middleware/ip_firewall/middleware.json +0 -109
- package/middleware/mqtt_base/index.js +0 -10
- package/middleware/mqtt_base/middleware.json +0 -11
- package/middleware/security_audit/index.js +0 -543
- package/middleware/security_audit/middleware.json +0 -48
- package/middleware/waf/index.js +0 -343
- package/middleware/waf/middleware.json +0 -10
- package/middleware/waf_ddos/index.js +0 -520
- package/middleware/waf_ddos/middleware.json +0 -38
- package/middleware/waf_xss/index.js +0 -269
- package/middleware/waf_xss/middleware.json +0 -18
- package/middleware/web_after/index.js +0 -33
- package/middleware/web_after/middleware.json +0 -10
- package/middleware/web_base/index.js +0 -90
- package/middleware/web_base/middleware.json +0 -10
- package/middleware/web_before/index.js +0 -27
- package/middleware/web_before/middleware.json +0 -10
- package/middleware/web_check/index.js +0 -28
- package/middleware/web_check/middleware.json +0 -10
- package/middleware/web_main/index.js +0 -28
- package/middleware/web_main/middleware.json +0 -10
- package/middleware/web_proxy/index.js +0 -37
- package/middleware/web_proxy/middleware.json +0 -10
- package/middleware/web_render/index.js +0 -87
- package/middleware/web_render/middleware.json +0 -10
- package/middleware/web_socket/index.js +0 -34
- package/middleware/web_socket/middleware.json +0 -10
- package/middleware/web_static/index.js +0 -115
- package/middleware/web_static/middleware.json +0 -10
- /package/{core/com → com}/api/README.md +0 -0
- /package/{core/com → com}/db/README.md +0 -0
- /package/{core/com → com}/mqtt/README.md +0 -0
- /package/{core/com → com}/nav/README.md +0 -0
- /package/{core/com → com}/nav/tpl/admin_pc/page_channel.vue +0 -0
- /package/{core/com → com}/nav/tpl/admin_pc/page_default.vue +0 -0
- /package/{core/com → com}/nav/tpl/admin_pc/page_lang.vue +0 -0
- /package/{core/com → com}/nav/tpl/admin_pc/page_nav.vue +0 -0
- /package/{core/com → com}/nav/tpl/admin_pc/page_table.vue +0 -0
- /package/{core/com → com}/nav/tpl/admin_pc/page_type.vue +0 -0
- /package/{core/com → com}/nav/tpl/dev_pc/page_channel.vue +0 -0
- /package/{core/com → com}/nav/tpl/dev_pc/page_config.vue +0 -0
- /package/{core/com → com}/nav/tpl/dev_pc/page_form.vue +0 -0
- /package/{core/com → com}/nav/tpl/dev_pc/page_nav.vue +0 -0
- /package/{core/com → com}/nav/tpl/dev_pc/page_table.vue +0 -0
- /package/{core/com → com}/nav/tpl/home_pc/page_channel.vue +0 -0
- /package/{core/com → com}/nav/tpl/home_phone/page_list.vue +0 -0
- /package/{core/com → com}/param/README.md +0 -0
- /package/{core/com/cmd → com/pendant}/README.md +0 -0
- /package/{core/com → com}/socket/README.md +0 -0
- /package/{core/com → com}/sql/README.md +0 -0
- /package/{core/com → com}/static/README.md +0 -0
- /package/{core/com → com}/task/README.md +0 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 中间件
|
|
3
|
+
*/
|
|
4
|
+
module.exports = {
|
|
5
|
+
/**
|
|
6
|
+
* 中间件初始化
|
|
7
|
+
* @param {object} apapter 适配器
|
|
8
|
+
* @param {object} eventer 事件处理器
|
|
9
|
+
*/
|
|
10
|
+
async _init(apapter, eventer) {
|
|
11
|
+
var event = $.admin.event('api', 'API事件');
|
|
12
|
+
await event.call('update');
|
|
13
|
+
},
|
|
14
|
+
/**
|
|
15
|
+
* 主要逻辑
|
|
16
|
+
* @param {object} ctx Koa上下文
|
|
17
|
+
* @param {Function} next 下一个中间件
|
|
18
|
+
*/
|
|
19
|
+
async main(ctx, next) {
|
|
20
|
+
await next();
|
|
21
|
+
if (ctx.path !== '/favicon.ico') {
|
|
22
|
+
var event = $.admin.event('api');
|
|
23
|
+
var ret = await event.after(ctx.path, ctx, ctx.db);
|
|
24
|
+
if (ret) {
|
|
25
|
+
ctx.body = ret;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
const compress = require('koa-compress');
|
|
2
|
+
const send = require('koa-send');
|
|
3
|
+
const { koaBody } = require('koa-body');
|
|
4
|
+
const xmlParser = require('mm_xml');
|
|
5
|
+
const { session } = require('mm_session');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* web基础
|
|
9
|
+
* @param {object} server 服务
|
|
10
|
+
*/
|
|
11
|
+
module.exports = {
|
|
12
|
+
/**
|
|
13
|
+
* 初始化
|
|
14
|
+
* @param {object} adapter 适配器集合
|
|
15
|
+
*/
|
|
16
|
+
init(adapter) {
|
|
17
|
+
// 参数校验
|
|
18
|
+
if (typeof adapter !== 'object') {
|
|
19
|
+
throw new TypeError('参数类型错误');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// 获取web服务
|
|
23
|
+
var web = adapter.web;
|
|
24
|
+
|
|
25
|
+
// 获取配置
|
|
26
|
+
let cg = { ...this.config, ...adapter.web.config };
|
|
27
|
+
this._setupFileSend(web);
|
|
28
|
+
this._setupSession(web, cg);
|
|
29
|
+
this._setupCompress(web, cg);
|
|
30
|
+
this._setupParsers(web, cg);
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 设置文件发送
|
|
35
|
+
* @private
|
|
36
|
+
* @param {object} web web服务
|
|
37
|
+
*/
|
|
38
|
+
_setupFileSend(web) {
|
|
39
|
+
web.use(async (ctx, next) => {
|
|
40
|
+
// 设置文件发送方法,错误由web_error中间件统一处理
|
|
41
|
+
ctx.send = async function (src) {
|
|
42
|
+
await send(ctx, src);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
await next();
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 设置会话
|
|
51
|
+
* @private
|
|
52
|
+
* @param {object} web web服务
|
|
53
|
+
* @param {object} cg 配置
|
|
54
|
+
*/
|
|
55
|
+
_setupSession(web, cg) {
|
|
56
|
+
// 设置session 保存时长2小时
|
|
57
|
+
web.use(session({
|
|
58
|
+
max_age: $.login_period || cg.max_age || 7200
|
|
59
|
+
}));
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 设置压缩
|
|
64
|
+
* @private
|
|
65
|
+
* @param {object} web web服务
|
|
66
|
+
* @param {object} cg 配置
|
|
67
|
+
*/
|
|
68
|
+
_setupCompress(web, cg) {
|
|
69
|
+
if (cg.compress) {
|
|
70
|
+
web.use(compress({
|
|
71
|
+
filter: (content_type) => /text|json|javascript|css|xml/.test(content_type),
|
|
72
|
+
thresh: cg.threshold || 1024,
|
|
73
|
+
gzip: {
|
|
74
|
+
flush: require('zlib').constants.Z_SYNC_FLUSH
|
|
75
|
+
},
|
|
76
|
+
brotli: {
|
|
77
|
+
flush: require('zlib').constants.BROTLI_OPERATION_FLUSH
|
|
78
|
+
}
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 设置解析器
|
|
85
|
+
* @private
|
|
86
|
+
* @param {object} web web服务
|
|
87
|
+
* @param {object} cg 配置
|
|
88
|
+
*/
|
|
89
|
+
_setupParsers(web, cg) {
|
|
90
|
+
// 解析 text/xml
|
|
91
|
+
web.use(xmlParser());
|
|
92
|
+
// 解析请求体
|
|
93
|
+
web.use(koaBody({
|
|
94
|
+
jsonLimit: cg.json_limit || '20mb',
|
|
95
|
+
multipart: true, // 必须开启才能解析 form-data
|
|
96
|
+
urlencoded: true, // 可选:解析 URL 编码的表单
|
|
97
|
+
text: true,
|
|
98
|
+
json: true, // 可选:解析 JSON
|
|
99
|
+
patchNode: true,
|
|
100
|
+
patchKoa: true,
|
|
101
|
+
formidable: {
|
|
102
|
+
maxFileSize: cg.max_file_size || 2000 * 1024 * 1024
|
|
103
|
+
}
|
|
104
|
+
}));
|
|
105
|
+
|
|
106
|
+
web.use(async (ctx, next) => {
|
|
107
|
+
if (!ctx.request.body) {
|
|
108
|
+
ctx.request.body = {};
|
|
109
|
+
}
|
|
110
|
+
await next();
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "web_base",
|
|
3
|
+
"title": "web基本功能",
|
|
4
|
+
"description": "用于网站常规功能",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "web",
|
|
7
|
+
"state": 1,
|
|
8
|
+
"sort": 40,
|
|
9
|
+
// 发送的JSON最大大小
|
|
10
|
+
"json_limit": "20mb",
|
|
11
|
+
// 缓存最大时长
|
|
12
|
+
"max_age": 7200,
|
|
13
|
+
// 最大文件大小
|
|
14
|
+
"max_file_fize": 2097152000,
|
|
15
|
+
// 是否brotli压缩
|
|
16
|
+
"brotli": true,
|
|
17
|
+
// 是否gzip压缩
|
|
18
|
+
"gzip": true
|
|
19
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 中间件
|
|
3
|
+
*/
|
|
4
|
+
module.exports = {
|
|
5
|
+
/**
|
|
6
|
+
* 主要逻辑
|
|
7
|
+
* @param {object} ctx Koa上下文
|
|
8
|
+
* @param {Function} next 下一个中间件
|
|
9
|
+
*/
|
|
10
|
+
async main(ctx, next) {
|
|
11
|
+
if (!ctx.db) {
|
|
12
|
+
ctx.db = {
|
|
13
|
+
ret: null
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
if (ctx.path !== '/favicon.ico') {
|
|
17
|
+
var event = $.admin.event('api');
|
|
18
|
+
var ret = await event.before(ctx.path, ctx, ctx.db);
|
|
19
|
+
if (ret) {
|
|
20
|
+
ctx.body = ret;
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
ret = await event.check(ctx.path, ctx, ctx.db);
|
|
25
|
+
if (ret) {
|
|
26
|
+
ctx.body = ret;
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
await next();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// 导出符合系统期望的函数
|
|
2
|
+
module.exports = {
|
|
3
|
+
/**
|
|
4
|
+
* 初始化
|
|
5
|
+
* @param {object} adapter 适配器对象
|
|
6
|
+
*/
|
|
7
|
+
init(adapter) {
|
|
8
|
+
// 获取web服务
|
|
9
|
+
// var web = adapter.web;
|
|
10
|
+
|
|
11
|
+
// 获取配置
|
|
12
|
+
this.config = { ...this.config, ...adapter.web.config };
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* CORS跨域中间件
|
|
17
|
+
* 处理跨域资源共享(CORS)配置
|
|
18
|
+
* @param {object} ctx Koa上下文
|
|
19
|
+
* @param {object} config CORS配置
|
|
20
|
+
*/
|
|
21
|
+
_setCorsHeaders(ctx, config) {
|
|
22
|
+
// 设置Access-Control-Allow-Origin
|
|
23
|
+
if (config.origin === '*') {
|
|
24
|
+
ctx.set('Access-Control-Allow-Origin', '*');
|
|
25
|
+
} else if (Array.isArray(config.origin)) {
|
|
26
|
+
let request_origin = ctx.get('Origin');
|
|
27
|
+
if (config.origin.includes(request_origin)) {
|
|
28
|
+
ctx.set('Access-Control-Allow-Origin', request_origin);
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
ctx.set('Access-Control-Allow-Origin', config.origin);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 设置Access-Control-Allow-Headers
|
|
35
|
+
if (config.headers === '*') {
|
|
36
|
+
ctx.set('Access-Control-Allow-Headers', '*');
|
|
37
|
+
} else if (Array.isArray(config.headers)) {
|
|
38
|
+
ctx.set('Access-Control-Allow-Headers', config.headers.join(', '));
|
|
39
|
+
} else {
|
|
40
|
+
ctx.set('Access-Control-Allow-Headers', config.headers);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 设置Access-Control-Allow-Methods
|
|
44
|
+
ctx.set('Access-Control-Allow-Methods', config.methods.join(', '));
|
|
45
|
+
|
|
46
|
+
// 设置Access-Control-Allow-Credentials
|
|
47
|
+
if (config.credentials) {
|
|
48
|
+
ctx.set('Access-Control-Allow-Credentials', 'true');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 设置Access-Control-Max-Age
|
|
52
|
+
if (config.max_age > 0) {
|
|
53
|
+
ctx.set('Access-Control-Max-Age', config.max_age.toString());
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 设置Access-Control-Expose-Headers
|
|
57
|
+
if (config.expose_headers.length > 0) {
|
|
58
|
+
ctx.set('Access-Control-Expose-Headers', config.expose_headers.join(', '));
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
async main(ctx, next) {
|
|
63
|
+
let config = this.config;
|
|
64
|
+
|
|
65
|
+
if (!config.enable) {
|
|
66
|
+
return await next();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 检查是否应该忽略该路径
|
|
70
|
+
let path = ctx.path;
|
|
71
|
+
if (config.ignore_paths.some(p => path.startsWith(p))) {
|
|
72
|
+
return await next();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// 设置CORS头
|
|
76
|
+
this._setCorsHeaders(ctx, config);
|
|
77
|
+
|
|
78
|
+
// 处理预检请求
|
|
79
|
+
if (ctx.method === 'OPTIONS') {
|
|
80
|
+
ctx.status = 204;
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
await next();
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "web_cors",
|
|
3
|
+
"title": "CORS跨域中间件",
|
|
4
|
+
"description": "处理跨域资源共享(CORS)配置,支持灵活的跨域策略",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "web",
|
|
7
|
+
"state": 1,
|
|
8
|
+
"sort": 30,
|
|
9
|
+
"enable": true,
|
|
10
|
+
"origin": "*",
|
|
11
|
+
"headers": "*",
|
|
12
|
+
"methods": [
|
|
13
|
+
"GET",
|
|
14
|
+
"POST",
|
|
15
|
+
"PUT",
|
|
16
|
+
"DELETE",
|
|
17
|
+
"OPTIONS",
|
|
18
|
+
"HEAD"
|
|
19
|
+
],
|
|
20
|
+
"credentials": false,
|
|
21
|
+
"max_age": 3600,
|
|
22
|
+
"expose_headers": [],
|
|
23
|
+
"ignore_paths": []
|
|
24
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// 导出错误处理中间件
|
|
2
|
+
module.exports = {
|
|
3
|
+
/**
|
|
4
|
+
* 初始化
|
|
5
|
+
* @param {object} adapter 适配器对象
|
|
6
|
+
*/
|
|
7
|
+
init(adapter) {
|
|
8
|
+
// 获取配置
|
|
9
|
+
this.config = { ...this.config, ...adapter.web.config};
|
|
10
|
+
},
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 获取错误消息
|
|
14
|
+
* @private
|
|
15
|
+
* @param {object} config 配置对象
|
|
16
|
+
* @param {number} status 状态码
|
|
17
|
+
* @returns {string} 错误消息
|
|
18
|
+
*/
|
|
19
|
+
_getErrorMessage(config, status) {
|
|
20
|
+
if (config.error_codes && config.error_codes[status]) {
|
|
21
|
+
return config.error_codes[status];
|
|
22
|
+
}
|
|
23
|
+
return config.default_message || 'Internal Server Error';
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 格式化错误信息
|
|
28
|
+
* @private
|
|
29
|
+
* @param {object} config 配置对象
|
|
30
|
+
* @param {Error} err 错误对象
|
|
31
|
+
* @returns {object} 格式化后的错误信息
|
|
32
|
+
*/
|
|
33
|
+
_formatError(config, err) {
|
|
34
|
+
let errorInfo = {
|
|
35
|
+
message: err.message,
|
|
36
|
+
status: err.status || err.statusCode || config.default_status,
|
|
37
|
+
stack: err.stack
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
if (err.code) {
|
|
41
|
+
errorInfo.code = err.code;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (err.details) {
|
|
45
|
+
errorInfo.details = err.details;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (err.name) {
|
|
49
|
+
errorInfo.name = err.name;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return errorInfo;
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 处理错误
|
|
57
|
+
* @private
|
|
58
|
+
* @param {object} ctx Koa上下文
|
|
59
|
+
* @param {Error} err 错误对象
|
|
60
|
+
*/
|
|
61
|
+
async _handleError(ctx, err) {
|
|
62
|
+
// 记录错误日志到控制台
|
|
63
|
+
let error_info = this._formatError(this.config, err);
|
|
64
|
+
this.log('error', `Error occurred: ${err.message}`, error_info);
|
|
65
|
+
|
|
66
|
+
// 设置响应状态码
|
|
67
|
+
let status = err.status || err.status_code || this.config.default_status;
|
|
68
|
+
|
|
69
|
+
// 特殊错误处理:文件不存在错误返回404
|
|
70
|
+
if (err.code === 'ENOENT') {
|
|
71
|
+
status = 404;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
ctx.status = status;
|
|
75
|
+
|
|
76
|
+
let message = err.message || this._getErrorMessage(this.config, status);
|
|
77
|
+
let error_response = $.ret.error(status, message);
|
|
78
|
+
|
|
79
|
+
// 如果配置允许,暴露堆栈信息(仅在开发环境使用)
|
|
80
|
+
if (this.config.expose_trace && err.stack) {
|
|
81
|
+
error_response.trace = err.stack;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 添加额外的错误信息
|
|
85
|
+
if (err.code) {
|
|
86
|
+
error_response.error_code = err.code;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (err.details) {
|
|
90
|
+
error_response.details = err.details;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 设置响应体
|
|
94
|
+
ctx.body = error_response;
|
|
95
|
+
|
|
96
|
+
// 设置响应头
|
|
97
|
+
ctx.set('Content-Type', 'application/json');
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 中间件主方法
|
|
102
|
+
* @param {object} ctx Koa上下文
|
|
103
|
+
* @param {Function} next 下一个中间件
|
|
104
|
+
* @returns {Promise<void>}
|
|
105
|
+
*/
|
|
106
|
+
async main(ctx, next) {
|
|
107
|
+
try {
|
|
108
|
+
await next();
|
|
109
|
+
|
|
110
|
+
// 处理404错误
|
|
111
|
+
if (ctx.status === 404 && !ctx.body) {
|
|
112
|
+
ctx.status = 404;
|
|
113
|
+
ctx.body = $.ret.error(404, 'Not Found');
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
await this._handleError(ctx, error);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "web_error",
|
|
3
|
+
"description": "错误处理中间件",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"type": "web",
|
|
6
|
+
"state": 1,
|
|
7
|
+
"sort": 10,
|
|
8
|
+
"expose_stacktrace": false,
|
|
9
|
+
"default_status": 500,
|
|
10
|
+
"default_message": "Internal Server Error",
|
|
11
|
+
"error_codes": {
|
|
12
|
+
"400": "Bad Request",
|
|
13
|
+
"401": "Unauthorized",
|
|
14
|
+
"403": "Forbidden",
|
|
15
|
+
"404": "Not Found",
|
|
16
|
+
"500": "Internal Server Error"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// 导出中间件主方法
|
|
2
|
+
module.exports = {
|
|
3
|
+
_getClientIp(ctx) {
|
|
4
|
+
return ctx.headers['x-forwarded-for']?.split(',')[0]?.trim() ||
|
|
5
|
+
ctx.headers['x-real-ip'] ||
|
|
6
|
+
ctx.ip ||
|
|
7
|
+
'unknown';
|
|
8
|
+
},
|
|
9
|
+
async main(ctx, next) {
|
|
10
|
+
if (ctx.ip === undefined) {
|
|
11
|
+
ctx.ip = this._getClientIp(ctx);
|
|
12
|
+
}
|
|
13
|
+
await next();
|
|
14
|
+
}
|
|
15
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "web_ip",
|
|
3
|
+
"title": "HTTP请求IP中间件",
|
|
4
|
+
"description": "用于记录HTTP请求IP地址",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "web",
|
|
7
|
+
"state": 1,
|
|
8
|
+
"sort": 11,
|
|
9
|
+
"level": "info",
|
|
10
|
+
"format": "default",
|
|
11
|
+
"request": true,
|
|
12
|
+
"response": true,
|
|
13
|
+
"duration": true
|
|
14
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// 导出中间件主方法
|
|
2
|
+
module.exports = {
|
|
3
|
+
/**
|
|
4
|
+
* 初始化
|
|
5
|
+
* @param {object} adapter 适配器集合
|
|
6
|
+
*/
|
|
7
|
+
init(adapter) {
|
|
8
|
+
// 获取web服务
|
|
9
|
+
var web = adapter.web;
|
|
10
|
+
|
|
11
|
+
// 注册中间件
|
|
12
|
+
web.use(async (ctx, next) => {
|
|
13
|
+
if (ctx.path === '/favicon.ico') {
|
|
14
|
+
await next();
|
|
15
|
+
} else {
|
|
16
|
+
ctx.ip = this._getClientIp(ctx);
|
|
17
|
+
ctx.start_time = Date.now();
|
|
18
|
+
let log = {
|
|
19
|
+
method: ctx.method,
|
|
20
|
+
path: ctx.path,
|
|
21
|
+
ip: ctx.ip || this._getClientIp(ctx),
|
|
22
|
+
request: this._format(ctx),
|
|
23
|
+
user_agent: this._getUserAgent(ctx)
|
|
24
|
+
};
|
|
25
|
+
try {
|
|
26
|
+
await next();
|
|
27
|
+
|
|
28
|
+
// 请求后置处理
|
|
29
|
+
log.duration = Date.now() - ctx.start_time;
|
|
30
|
+
log.response = this._sanitizeBody(ctx.body) || {};
|
|
31
|
+
log.status = ctx.status;
|
|
32
|
+
// 请求前置处理
|
|
33
|
+
this._record(log);
|
|
34
|
+
} catch (err) {
|
|
35
|
+
// 记录错误日志
|
|
36
|
+
log.duration = Date.now() - ctx.start_time;
|
|
37
|
+
log.status = 500;
|
|
38
|
+
log.response = {
|
|
39
|
+
error: {
|
|
40
|
+
message: err.message,
|
|
41
|
+
stack: err.stack
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
this._record(log);
|
|
45
|
+
|
|
46
|
+
// 重新抛出错误,由web_error中间件统一处理
|
|
47
|
+
throw err;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
_getClientIp(ctx) {
|
|
53
|
+
return ctx.headers['x-forwarded-for']?.split(',')[0]?.trim() ||
|
|
54
|
+
ctx.headers['x-real-ip'] ||
|
|
55
|
+
'unknown';
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 获取用户代理信息
|
|
60
|
+
* @private
|
|
61
|
+
* @param {object} ctx Koa上下文
|
|
62
|
+
* @returns {string} 用户代理信息
|
|
63
|
+
*/
|
|
64
|
+
_getUserAgent(ctx) {
|
|
65
|
+
let ua = ctx.headers['user-agent'] || '';
|
|
66
|
+
|
|
67
|
+
// 简化的 UA 解析,只提取浏览器和 OS
|
|
68
|
+
if (ua.includes('Chrome')) return 'Chrome';
|
|
69
|
+
if (ua.includes('Firefox')) return 'Firefox';
|
|
70
|
+
if (ua.includes('Safari')) return 'Safari';
|
|
71
|
+
if (ua.includes('Postman')) return 'Postman';
|
|
72
|
+
if (ua.includes('curl')) return 'curl';
|
|
73
|
+
|
|
74
|
+
return ua.length > 20 ? ua.substring(0, 20) + '...' : ua;
|
|
75
|
+
},
|
|
76
|
+
/**
|
|
77
|
+
* 格式化请求信息
|
|
78
|
+
* @private
|
|
79
|
+
* @param {object} ctx Koa上下文
|
|
80
|
+
* @returns {object} 格式化后的请求数据
|
|
81
|
+
*/
|
|
82
|
+
_format(ctx) {
|
|
83
|
+
// const headers = this._sanitizeHeaders(ctx.headers);
|
|
84
|
+
let body = ctx.request.body ? this._sanitizeBody(ctx.request.body) : undefined;
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
query: ctx.query,
|
|
88
|
+
body: body
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 清理敏感头信息
|
|
94
|
+
* @private
|
|
95
|
+
* @param {object} headers 请求头
|
|
96
|
+
* @returns {object} 清理后的请求头
|
|
97
|
+
*/
|
|
98
|
+
_sanitizeHeaders(headers) {
|
|
99
|
+
const SENS_HEADERS = ['authorization', 'cookie', 'x-api-key'];
|
|
100
|
+
const SAN_VAL = {};
|
|
101
|
+
|
|
102
|
+
for (var k in headers) {
|
|
103
|
+
if (SENS_HEADERS.includes(k.toLowerCase())) {
|
|
104
|
+
SAN_VAL[k] = '[REDACTED]';
|
|
105
|
+
} else {
|
|
106
|
+
SAN_VAL[k] = headers[k];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return SAN_VAL;
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 清理敏感请求体信息
|
|
115
|
+
* @private
|
|
116
|
+
* @param {object} body 请求体
|
|
117
|
+
* @returns {object} 清理后的请求体
|
|
118
|
+
*/
|
|
119
|
+
_sanitizeBody(body) {
|
|
120
|
+
const SENS_KEYS = ['password', 'token', 'secret', 'key', 'credit_card'];
|
|
121
|
+
let by = {};
|
|
122
|
+
if (typeof(body) === 'object') {
|
|
123
|
+
for (var k in body) {
|
|
124
|
+
if (SENS_KEYS.some(field => k.toLowerCase().includes(field))) {
|
|
125
|
+
by[k] = '[REDACTED]';
|
|
126
|
+
} else {
|
|
127
|
+
by[k] = body[k];
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return by;
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 记录请求日志
|
|
136
|
+
* @private
|
|
137
|
+
* @param {object} log 日志对象
|
|
138
|
+
*/
|
|
139
|
+
_record(log) {
|
|
140
|
+
var status = log.status;
|
|
141
|
+
this.log('http', `[${log.ip}] [${log.method}] ${log.path} ${status} ${log.duration}ms`,
|
|
142
|
+
log.request, log.response);
|
|
143
|
+
// 时间 time '2022-11-11 12:00:00'
|
|
144
|
+
// 信息级别 level 'warn'
|
|
145
|
+
// 来源IP ip '127.0.0.1'
|
|
146
|
+
// 浏览器 user_agent 'Chrome' X
|
|
147
|
+
// 查询条件 query '{'id':1}' X
|
|
148
|
+
// 提交内容 body '{'name':'test'}' X
|
|
149
|
+
// 错误信息 error 'Error: test' X
|
|
150
|
+
// 请求方法 method 'GET'
|
|
151
|
+
// 请求路径 path '/test'
|
|
152
|
+
// 响应时长 duration '38ms'
|
|
153
|
+
// 响应状态 status 200
|
|
154
|
+
// 用户ID user_id 1
|
|
155
|
+
}
|
|
156
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "web_logger",
|
|
3
|
+
"title": "HTTP请求日志中间件",
|
|
4
|
+
"description": "用于记录HTTP请求日志,包括请求方法、URL、状态码、响应时间等",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "web",
|
|
7
|
+
"state": 1,
|
|
8
|
+
"sort": 20,
|
|
9
|
+
"level": "info",
|
|
10
|
+
"format": "default",
|
|
11
|
+
"request": true,
|
|
12
|
+
"response": true,
|
|
13
|
+
"duration": true
|
|
14
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 中间件
|
|
3
|
+
*/
|
|
4
|
+
module.exports = {
|
|
5
|
+
/**
|
|
6
|
+
* 主要逻辑
|
|
7
|
+
* @param {object} ctx Koa上下文
|
|
8
|
+
* @param {Function} next 下一个中间件
|
|
9
|
+
*/
|
|
10
|
+
async main(ctx, next) {
|
|
11
|
+
if (ctx.path !== '/favicon.ico') {
|
|
12
|
+
var event = $.admin.event('api');
|
|
13
|
+
var ret = await event.main(ctx.path, ctx, ctx.db);
|
|
14
|
+
if (ret) {
|
|
15
|
+
ctx.body = ret;
|
|
16
|
+
ret = await event.render(ctx.path, ctx, ctx.db);
|
|
17
|
+
if (ret) {
|
|
18
|
+
ctx.body = ret;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
await next();
|
|
23
|
+
}
|
|
24
|
+
};
|