From dbc7105350ecf143d90942485af4032554e0747a Mon Sep 17 00:00:00 2001 From: Canim <840054486@qq.com> Date: Wed, 11 Jun 2025 12:38:10 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E6=96=B0=E5=A2=9E=20koa=20=E9=9D=99?= =?UTF-8?q?=E6=80=81=E6=9C=8D=E5=8A=A1=E6=94=AF=E6=8C=81=20&=20HttpServer?= =?UTF-8?q?=20=E8=87=AA=E5=AE=9A=E4=B9=89=20filter=20=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ee-core/config/default_config.d.ts | 7 +++++++ ee-core/config/default_config.js | 9 ++++++++- ee-core/package.json | 1 + ee-core/socket/httpServer.js | 19 +++++++++++++++++-- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/ee-core/config/default_config.d.ts b/ee-core/config/default_config.d.ts index c42d3d9..c32284f 100644 --- a/ee-core/config/default_config.d.ts +++ b/ee-core/config/default_config.d.ts @@ -65,6 +65,13 @@ export declare interface HttpConfig { keepExtensions?: boolean; }; }; + static?: { + enable?: boolean, + prefix?: string, + path?: string, + options?: object + }, + filter: (uriPath: string, ctx: any) => boolean; filterRequest?: { uris?: string[]; returnData?: string; diff --git a/ee-core/config/default_config.js b/ee-core/config/default_config.js index 0830d5e..8aa19d0 100644 --- a/ee-core/config/default_config.js +++ b/ee-core/config/default_config.js @@ -1,7 +1,7 @@ 'use strict'; const path = require('path'); -const { env, getBaseDir, getLogDir } = require('../ps'); +const { env, getBaseDir, getLogDir, getExtraResourcesDir } = require('../ps'); const { SocketIO } = require('../const/channel'); /** @@ -79,6 +79,13 @@ module.exports = () => { keepExtensions: true } }, + static: { + enable: true, + prefix: '/public', + path: path.join(getExtraResourcesDir(), 'public'), + options: {} + }, + filter: null, filterRequest: { uris: [ 'favicon.ico' diff --git a/ee-core/package.json b/ee-core/package.json index fe12951..68eedcd 100644 --- a/ee-core/package.json +++ b/ee-core/package.json @@ -22,6 +22,7 @@ "koa": "^2.13.4", "koa-body": "^5.0.0", "koa-convert": "^2.0.0", + "koa-mount": "^4.2.0", "koa-static": "^5.0.0", "koa2-cors": "^2.0.6", "lodash": "^4.17.21", diff --git a/ee-core/socket/httpServer.js b/ee-core/socket/httpServer.js index 9d05d60..d234d7b 100644 --- a/ee-core/socket/httpServer.js +++ b/ee-core/socket/httpServer.js @@ -6,12 +6,14 @@ const is = require('is-type-of'); const Koa = require('koa'); const cors = require('koa2-cors'); const koaBody = require('koa-body'); +const koaMount = require('koa-mount'); +const koaStatic = require('koa-static'); const https = require('https'); const fs = require('fs'); const path = require('path'); const _ = require('lodash'); const { coreLogger } = require('../log'); -const { getBaseDir } = require('../ps'); +const { getBaseDir, getExtraResourcesDir } = require('../ps'); const { getController } = require('../controller'); const { getConfig } = require('../config'); const { getPort } = require('../utils/port'); @@ -77,6 +79,14 @@ class HttpServer { // 加载前置中间件 this._loadMiddlewares(koaApp, preMiddleware); + // 静态服务 + const staticPrefix = config.static?.prefix || '/public'; + const staticPath = config.static?.path || path.join(getExtraResourcesDir(), 'public'); + const staticOptions = config.static?.options || {}; + if(config.static?.enable) { + koaApp.use(koaMount(staticPrefix, koaStatic(staticPath, staticOptions))); + } + // 核心中间件 koaApp .use(cors(corsOptions)) @@ -111,7 +121,7 @@ class HttpServer { */ async _dispatch (ctx, next) { const controller = getController(); - const { filterRequest } = getConfig().httpServer; + const { filter, filterRequest } = getConfig().httpServer; let uriPath = ctx.request.path; const method = ctx.request.method; let params = ctx.request.query; @@ -122,6 +132,11 @@ class HttpServer { ctx.response.status = 200; try { + // 自定义 filter 过滤 http 请求 + if (filter && filter(uriPath, ctx)) { + await next(); + return; + } // 找函数 // 去除开头的 '/' if (uriPath.indexOf('/') == 0) {