Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ee-core/config/default_config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion ee-core/config/default_config.js
Original file line number Diff line number Diff line change
@@ -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');

/**
Expand Down Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions ee-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 17 additions & 2 deletions ee-core/socket/httpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down