temp
This commit is contained in:
85
assistant/src/services/proxy/proxy-page-index.ts
Normal file
85
assistant/src/services/proxy/proxy-page-index.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { fileProxy, apiProxy, createApiProxy } from '@/module/assistant/index.ts';
|
||||
import http from 'http';
|
||||
import { LocalProxy } from './local-proxy.ts';
|
||||
import { assistantConfig } 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 url = new URL(req.url, 'http://localhost');
|
||||
const pathname = url.pathname;
|
||||
if (pathname.startsWith('/favicon.ico')) {
|
||||
res.statusCode = 404;
|
||||
res.end('Not Found Favicon');
|
||||
return;
|
||||
}
|
||||
if (pathname.startsWith('/client')) {
|
||||
console.log('handle by router');
|
||||
return;
|
||||
}
|
||||
// client, api, v1, serve 开头的拦截
|
||||
const apiProxyList = _assistantConfig?.apiProxyList || [];
|
||||
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, {
|
||||
path: apiBackendProxy.path,
|
||||
target: apiBackendProxy.target,
|
||||
});
|
||||
}
|
||||
const urls = pathname.split('/');
|
||||
const [_, _user, _app] = urls;
|
||||
if (!_app) {
|
||||
res.statusCode = 404;
|
||||
res.end('Not Found Proxy');
|
||||
return;
|
||||
}
|
||||
if (_app && urls.length === 3) {
|
||||
// 重定向到
|
||||
res.writeHead(302, { Location: `${req.url}/` });
|
||||
return res.end();
|
||||
}
|
||||
const proxyApiList = _assistantConfig?.proxy || [];
|
||||
const proxyApi = proxyApiList.find((item) => pathname.startsWith(item.path));
|
||||
if (proxyApi) {
|
||||
log.log('proxyApi', { proxyApi, pathname });
|
||||
const { user, key } = proxyApi;
|
||||
return fileProxy(req, res, {
|
||||
path: proxyApi.path, // 代理路径, 比如/root/center
|
||||
rootPath: appDir, // 根路径
|
||||
indexPath: `${user}/${key}/index.html`, // 首页路径
|
||||
});
|
||||
}
|
||||
const localProxyProxyList = localProxy.getLocalProxyList();
|
||||
const localProxyProxy = localProxyProxyList.find((item) => pathname.startsWith(item.path));
|
||||
if (localProxyProxy) {
|
||||
log.log('localProxyProxy', { localProxyProxy, url: req.url });
|
||||
return fileProxy(req, res, {
|
||||
path: localProxyProxy.path,
|
||||
rootPath: assistantConfig.configPath?.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, {
|
||||
path: centerProxy.path,
|
||||
target: centerProxy.target,
|
||||
type: 'static',
|
||||
});
|
||||
}
|
||||
res.statusCode = 404;
|
||||
res.end('Not Found Proxy');
|
||||
// console.log('getCacheAssistantConfig().pageApi', getCacheAssistantConfig().pageApi);
|
||||
// return apiProxy(req, res, {
|
||||
// path: url.pathname,
|
||||
// target: getCacheAssistantConfig().pageApi,
|
||||
// });
|
||||
};
|
||||
Reference in New Issue
Block a user