feat: 重构代理功能,添加文件代理支持并优化相关逻辑
This commit is contained in:
@@ -27,13 +27,16 @@ export class AssistantInit extends AssistantConfig {
|
||||
}
|
||||
}
|
||||
|
||||
async init() {
|
||||
async init(configDir?: string) {
|
||||
if (configDir) {
|
||||
this.configDir = configDir;
|
||||
}
|
||||
// 1. 检查助手路径是否存在
|
||||
if (!this.checkConfigPath()) {
|
||||
console.log(chalk.blue('助手路径不存在,正在创建...'));
|
||||
super.init();
|
||||
super.init(configDir);
|
||||
} else {
|
||||
super.init();
|
||||
super.init(configDir);
|
||||
const assistantConfig = this;
|
||||
console.log(chalk.yellow('助手路径已存在'), chalk.green(assistantConfig.configDir));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { fileProxy, httpProxy, createApiProxy, ProxyInfo, proxy } from '@/module/assistant/index.ts';
|
||||
import { fileProxy, httpProxy, createApiProxy, ProxyInfo, proxy, fileProxy2 } from '@/module/assistant/index.ts';
|
||||
import http from 'node:http';
|
||||
import { LocalProxy } from './local-proxy.ts';
|
||||
import { assistantConfig, app, simpleRouter } from '@/app.ts';
|
||||
@@ -116,27 +116,37 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp
|
||||
const defaultApiProxy = createApiProxy(_assistantConfig?.app?.url || 'https://kevisual.cn');
|
||||
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 });
|
||||
const proxyFn = async (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => {
|
||||
log.debug('proxyApi', { proxyApi, url: req.url });
|
||||
// 设置 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');
|
||||
if (apiBackendProxy.s3?.id) {
|
||||
if (proxyApi.s3?.id) {
|
||||
const storage = _assistantConfig?.storage || []
|
||||
const storageConfig = storage.find((item) => item.id === apiBackendProxy.s3?.id);
|
||||
apiBackendProxy.s3 = {
|
||||
const storageConfig = storage.find((item) => item.id === proxyApi.s3?.id);
|
||||
proxyApi.s3 = {
|
||||
...storageConfig,
|
||||
...apiBackendProxy.s3,
|
||||
...proxyApi.s3,
|
||||
}
|
||||
}
|
||||
if (proxyApi.file?.id) {
|
||||
const storage = _assistantConfig?.storage || []
|
||||
const storageConfig = storage.find((item) => item.id === proxyApi.file?.id);
|
||||
proxyApi.file = {
|
||||
...storageConfig,
|
||||
...proxyApi.file,
|
||||
}
|
||||
}
|
||||
return proxy(req, res, {
|
||||
path: apiBackendProxy.path,
|
||||
target: apiBackendProxy.target,
|
||||
...apiBackendProxy,
|
||||
path: proxyApi.path,
|
||||
target: proxyApi.target,
|
||||
...proxyApi,
|
||||
});
|
||||
}
|
||||
if (apiBackendProxy) {
|
||||
return proxyFn(req, res, apiBackendProxy);
|
||||
}
|
||||
logger.debug('proxyRoute handle by router', { url: req.url }, noAdmin);
|
||||
const urls = pathname.split('/');
|
||||
const [_, _user, _app] = urls;
|
||||
@@ -157,27 +167,9 @@ 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 && 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/home
|
||||
rootPath: proxyApi.rootPath,
|
||||
...proxyApi,
|
||||
indexPath: _indexPath, // 首页路径
|
||||
});
|
||||
} else if (proxyApi && proxyApi.type === 'http') {
|
||||
log.debug('proxyApi http', { proxyApi, pathname });
|
||||
return httpProxy(req, res, {
|
||||
path: proxyApi.path,
|
||||
target: proxyApi.target,
|
||||
type: 'http',
|
||||
});
|
||||
if (proxyApi) {
|
||||
log.debug('proxyPage', { proxyApi, pathname });
|
||||
return proxyFn(req, res, proxyApi);
|
||||
}
|
||||
const filter = await authFilter(req, res);
|
||||
if (filter.code !== 200) {
|
||||
@@ -189,16 +181,19 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp
|
||||
const localProxyProxy = localProxyProxyList.find((item) => pathname.startsWith(item.path));
|
||||
if (localProxyProxy) {
|
||||
log.log('localProxyProxy', { localProxyProxy, url: req.url });
|
||||
return fileProxy(req, res, {
|
||||
return proxyFn(req, res, {
|
||||
path: localProxyProxy.path,
|
||||
rootPath: localProxy.pagesDir,
|
||||
indexPath: localProxyProxy.indexPath,
|
||||
"type": 'file',
|
||||
file: {
|
||||
rootPath: localProxy.pagesDir,
|
||||
indexPath: localProxyProxy.file.indexPath,
|
||||
}
|
||||
});
|
||||
}
|
||||
const creatCenterProxy = createApiProxy(_assistantConfig?.app?.url || 'https://kevisual.cn', ['/root', '/' + _user]);
|
||||
const centerProxy = creatCenterProxy.find((item) => pathname.startsWith(item.path));
|
||||
if (centerProxy) {
|
||||
return httpProxy(req, res, {
|
||||
return proxyFn(req, res, {
|
||||
path: centerProxy.path,
|
||||
target: centerProxy.target,
|
||||
type: 'http',
|
||||
|
||||
Reference in New Issue
Block a user