From 01503954ade8efecb0193a22ad9aa6d405150579 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Mon, 10 Mar 2025 14:30:40 +0800 Subject: [PATCH] fix: fix proxy and api error --- assistant-module/package.json | 2 +- assistant-module/src/file/index.ts | 6 ++++++ assistant-module/src/proxy/api-proxy.ts | 2 +- assistant-module/src/proxy/file-proxy.ts | 22 +++++++++++---------- package.json | 3 ++- src/modules/config/index.ts | 7 ++++--- src/proxy-route/index.ts | 25 ++++++++++++++++++------ 7 files changed, 45 insertions(+), 22 deletions(-) diff --git a/assistant-module/package.json b/assistant-module/package.json index 9ac26c4..0044553 100644 --- a/assistant-module/package.json +++ b/assistant-module/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/assistant-module", - "version": "0.0.4-beta.2", + "version": "0.0.4-beta.3", "description": "assistant module", "main": "dist/assistant-module.mjs", "types": "dist/assistant-module.d.ts", diff --git a/assistant-module/src/file/index.ts b/assistant-module/src/file/index.ts index f1f45bb..1ba8778 100644 --- a/assistant-module/src/file/index.ts +++ b/assistant-module/src/file/index.ts @@ -1,5 +1,11 @@ import fs from 'fs'; +/** + * 检查文件是否存在 + * @param filePath 文件路径 + * @param checkIsFile 是否检查文件类型 default: false + * @returns + */ export const checkFileExists = (filePath: string, checkIsFile = false) => { try { fs.accessSync(filePath); diff --git a/assistant-module/src/proxy/api-proxy.ts b/assistant-module/src/proxy/api-proxy.ts index a43af38..5a5c5e2 100644 --- a/assistant-module/src/proxy/api-proxy.ts +++ b/assistant-module/src/proxy/api-proxy.ts @@ -18,7 +18,7 @@ export const defaultApiProxy = [ * @param paths ['/api/router', '/v1' ] * @returns */ -export const createApiProxy = (api: string, paths: string[] = ['/api/router', '/v1', '/resources']) => { +export const createApiProxy = (api: string, paths: string[] = ['/api', '/v1', '/resources']) => { const pathList = paths.map((item) => { return { path: item, diff --git a/assistant-module/src/proxy/file-proxy.ts b/assistant-module/src/proxy/file-proxy.ts index 57ea1f4..c96af8c 100644 --- a/assistant-module/src/proxy/file-proxy.ts +++ b/assistant-module/src/proxy/file-proxy.ts @@ -1,28 +1,30 @@ import http from 'http'; import send from 'send'; import fs from 'fs'; -import { fileIsExist } from '@kevisual/use-config'; import path from 'path'; import { ProxyInfo } from './proxy.ts'; +import { checkFileExists } from '@/file/index.ts'; export const fileProxy = (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => { // url开头的文件 const url = new URL(req.url, 'http://localhost'); - let pathname = url.pathname.slice(1); + const [user, key, _info] = url.pathname.split('/'); + const pathname = url.pathname.slice(1); const { indexPath = '', target = '', rootPath = process.cwd() } = proxyApi; try { - if (pathname.endsWith('/')) { - pathname = pathname + 'index.html'; - } // 检测文件是否存在,如果文件不存在,则返回404 - let filePath = path.join(rootPath, target, pathname); - let exist = fileIsExist(filePath); + let filePath = ''; + let exist = false; + if (_info) { + filePath = path.join(rootPath, target, pathname); + exist = checkFileExists(filePath, true); + } if (!exist) { - filePath = path.join(rootPath, target, '/' + indexPath); - exist = fileIsExist(filePath); + filePath = path.join(rootPath, target, indexPath); + exist = checkFileExists(filePath, true); } console.log('filePath', filePath, exist); - + if (!exist) { res.statusCode = 404; res.end('Not Found File'); diff --git a/package.json b/package.json index ac62294..9bf460d 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "dev:watch": "cross-env NODE_ENV=development concurrently -n \"Watch,Dev\" -c \"green,blue\" \"npm run watch\" \"sleep 1 && npm run dev\" ", "clean": "rm -rf dist", "prepub": "envision switch root", - "pub": "npm run build && envision pack -p -u" + "pub": "npm run build && envision pack -p -u", + "download": "ev app download -i root/assistant-base-app" }, "keywords": [], "author": "abearxiong ", diff --git a/src/modules/config/index.ts b/src/modules/config/index.ts index c64d304..06673a7 100644 --- a/src/modules/config/index.ts +++ b/src/modules/config/index.ts @@ -48,8 +48,8 @@ export const setConfig = (config?: AssistantConfig) => { if (!config) { return assistantConfig; } - assistantConfig = config; - fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); + assistantConfig = { ...assistantConfig, ...config }; + fs.writeFileSync(configPath, JSON.stringify(assistantConfig, null, 2)); return assistantConfig; }; type AppConfig = { @@ -69,7 +69,8 @@ export const getAppConfig = (): AppConfig => { }; export const setAppConfig = (config: AppConfig) => { - fs.writeFileSync(appConfigPath, JSON.stringify(config, null, 2)); + const _config = getAppConfig(); + fs.writeFileSync(appConfigPath, JSON.stringify({ ..._config, ...config }, null, 2)); return config; }; diff --git a/src/proxy-route/index.ts b/src/proxy-route/index.ts index e7420c9..90a2ec4 100644 --- a/src/proxy-route/index.ts +++ b/src/proxy-route/index.ts @@ -17,37 +17,50 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp console.log('handle by router'); return; } + // client, api, v1, serve 开头的拦截 const apiProxyList = assistantConfig?.apiProxyList || []; const defaultApiProxy = createApiProxy(assistantConfig?.pageApi || 'https://kevisual.xiongxiao.me'); const apiBackendProxy = [...apiProxyList, ...defaultApiProxy].find((item) => pathname.startsWith(item.path)); if (apiBackendProxy) { - console.log('apiBackendProxy', apiBackendProxy); + console.log('apiBackendProxy', apiBackendProxy, req.url); return apiProxy(req, res, { path: apiBackendProxy.path, target: apiBackendProxy.target, }); } - // client, api, v1, serve 开头的拦截 + 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) { console.log('proxyApi', proxyApi, pathname); const { user, key } = proxyApi; return fileProxy(req, res, { - path: proxyApi.path, - rootPath: appDir, - indexPath: `${user}/${key}/index.html`, + path: proxyApi.path, // 代理路径, 比如/root/center + rootPath: appDir, // 根路径 + indexPath: `${user}/${key}/index.html`, // 首页路径 }); } const localProxyProxy = localProxyProxyList.find((item) => pathname.startsWith(item.path)); if (localProxyProxy) { + console.log('localProxyProxy', localProxyProxy, req.url); return fileProxy(req, res, { path: localProxyProxy.path, rootPath: process.cwd(), indexPath: localProxyProxy.indexPath, }); } - console.log('handle by router 404'); + console.log('handle by router 404', req.url); res.statusCode = 404; res.end('Not Found Proxy'); };