This commit is contained in:
2025-04-26 03:15:11 +08:00
parent 9eb4d06939
commit bcc12209e0
28 changed files with 1708 additions and 126 deletions

View File

@@ -1,14 +1,14 @@
import { fileProxy, apiProxy, createApiProxy } from '@/module/assistant/index.ts';
import { fileProxy, httpProxy, createApiProxy, wsProxy } from '@/module/assistant/index.ts';
import http from 'http';
import { LocalProxy } from './local-proxy.ts';
import { assistantConfig } from '@/app.ts';
import { assistantConfig, app } from '@/app.ts';
import { log } from '@/module/logger.ts';
const localProxy = new LocalProxy({
assistantConfig,
});
export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResponse) => {
const _assistantConfig = assistantConfig.getCacheAssistantConfig();
const appDir = assistantConfig.configPath?.appDir;
const appDir = assistantConfig.configPath?.pageDir;
const url = new URL(req.url, 'http://localhost');
const pathname = url.pathname;
if (pathname.startsWith('/favicon.ico')) {
@@ -17,7 +17,7 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp
return;
}
if (pathname.startsWith('/client')) {
console.log('handle by router');
console.debug('handle by router');
return;
}
// client, api, v1, serve 开头的拦截
@@ -25,8 +25,8 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp
const defaultApiProxy = createApiProxy(_assistantConfig?.pageApi || 'https://kevisual.cn');
const apiBackendProxy = [...apiProxyList, ...defaultApiProxy].find((item) => pathname.startsWith(item.path));
if (apiBackendProxy) {
console.log('apiBackendProxy', apiBackendProxy, req.url);
return apiProxy(req, res, {
log.debug('apiBackendProxy', { apiBackendProxy, url: req.url });
return httpProxy(req, res, {
path: apiBackendProxy.path,
target: apiBackendProxy.target,
});
@@ -45,13 +45,19 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp
}
const proxyApiList = _assistantConfig?.proxy || [];
const proxyApi = proxyApiList.find((item) => pathname.startsWith(item.path));
if (proxyApi) {
log.log('proxyApi', { proxyApi, pathname });
const { user, key } = proxyApi;
if (proxyApi && proxyApi.type === 'file') {
log.debug('proxyApi', { proxyApi, pathname });
const _indexPath = proxyApi.indexPath || `${_user}/${_app}/index.html`;
const _rootPath = proxyApi.rootPath;
if (!_rootPath) {
log.error('Not Found rootPath', { proxyApi, pathname });
return res.end(`Not Found [${proxyApi.path}] rootPath`);
}
return fileProxy(req, res, {
path: proxyApi.path, // 代理路径, 比如/root/center
rootPath: appDir, // 根路径
indexPath: `${user}/${key}/index.html`, // 首页路径
rootPath: proxyApi.rootPath,
...proxyApi,
indexPath: _indexPath, // 首页路径
});
}
const localProxyProxyList = localProxy.getLocalProxyList();
@@ -60,26 +66,31 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp
log.log('localProxyProxy', { localProxyProxy, url: req.url });
return fileProxy(req, res, {
path: localProxyProxy.path,
rootPath: assistantConfig.configPath?.appDir,
rootPath: appDir,
indexPath: localProxyProxy.indexPath,
});
}
console.log('handle by router 404', req.url);
const creatCenterProxy = createApiProxy(_assistantConfig?.pageApi || 'https://kevisual.cn', ['/root']);
const centerProxy = creatCenterProxy.find((item) => pathname.startsWith(item.path));
if (centerProxy) {
console.log('centerProxy', centerProxy, req.url);
return apiProxy(req, res, {
return httpProxy(req, res, {
path: centerProxy.path,
target: centerProxy.target,
type: 'static',
type: 'http',
});
}
log.debug('handle by router 404', req.url);
res.statusCode = 404;
res.end('Not Found Proxy');
// console.log('getCacheAssistantConfig().pageApi', getCacheAssistantConfig().pageApi);
// return apiProxy(req, res, {
// path: url.pathname,
// target: getCacheAssistantConfig().pageApi,
// });
};
export const proxyWs = () => {
const apiProxyList = assistantConfig.getCacheAssistantConfig()?.apiProxyList || [];
const proxy = assistantConfig.getCacheAssistantConfig()?.proxy || [];
const proxyApi = [...apiProxyList, ...proxy].filter((item) => item.ws);
log.debug('proxyApi ', proxyApi);
wsProxy(app.server.server, {
apiList: proxyApi,
});
};