This commit is contained in:
2026-01-15 18:59:06 +08:00
parent a5ce44ba70
commit 6a375db5a1
7 changed files with 1343 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
import { fileProxy, httpProxy, createApiProxy, ProxyInfo } from '@/module/assistant/index.ts';
import { fileProxy, httpProxy, createApiProxy, ProxyInfo, proxy } from '@/module/assistant/index.ts';
import http from 'node:http';
import { LocalProxy } from './local-proxy.ts';
import { assistantConfig, app } from '@/app.ts';
@@ -110,12 +110,19 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp
// client, api, v1, serve 开头的拦截
const apiProxy = _assistantConfig?.api?.proxy || [];
const defaultApiProxy = createApiProxy(_assistantConfig?.app?.url || 'https://kevisual.cn');
const apiBackendProxy = [...apiProxy, ...defaultApiProxy].find((item) => pathname.startsWith(item.path));
const allProxy = [...apiProxy, ...defaultApiProxy];
const apiBackendProxy = allProxy.find((item) => pathname.startsWith(item.path));
// console.log('apiBackendProxy', allProxy, apiBackendProxy, pathname, apiProxy[0].path);
if (apiBackendProxy) {
log.debug('apiBackendProxy', { apiBackendProxy, url: req.url });
return httpProxy(req, res, {
// 设置 CORS 头
// res.setHeader('Access-Control-Allow-Origin', '*');
// res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
// res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
return proxy(req, res, {
path: apiBackendProxy.path,
target: apiBackendProxy.target,
...apiBackendProxy
});
}
logger.debug('proxyRoute handle by router', { url: req.url }, noAdmin);