From 79e0f542be5caefc9554a254f54749698c148274 Mon Sep 17 00:00:00 2001 From: Wenyan Date: Thu, 30 Oct 2025 14:49:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E6=BB=9A=EF=BC=9A=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E8=B0=83=E7=94=A8Remix=20API=20routes=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8DCORS=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题分析: 直接让客户端调用FastAPI后端会遇到: 1. CORS跨域限制 2. Cookie无法在跨域情况下传递 3. 安全性降低 正确架构(三层代理): 浏览器 → Remix /api/* → FastAPI /dify/* → Dify 流程说明: 1. 浏览器:fetch('/api/chat-messages', { credentials: 'include' }) - 相对路径,同域请求,无CORS问题 - Cookie自动携带 2. Remix API routes (app/routes/api.chat-messages.tsx) - 从session获取JWT - 调用 difyClient.createChatMessage(..., jwt) 3. dify-client.server.ts - 使用 API_BASE_URL (根据端口配置) - 调用 FastAPI: http://baseUrl/dify/chat-messages - 携带JWT: Authorization: Bearer {jwt} 4. FastAPI /dify 路由 - 验证JWT,提取username - 调用Dify: http://nas.7bm.co:12980/v1/chat-messages baseUrl配置(服务端使用): - 端口51703 → http://172.16.0.55:8073 - 端口51704 → http://10.79.97.17:8001 - ... 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- app/config/chat.ts | 38 +++++++------------------------------- 1 file changed, 7 insertions(+), 31 deletions(-) diff --git a/app/config/chat.ts b/app/config/chat.ts index d21cb6e..e064775 100644 --- a/app/config/chat.ts +++ b/app/config/chat.ts @@ -28,42 +28,18 @@ const extractAppId = (appUrl: string): string => { return appUrl; }; -// 获取FastAPI后端的基础URL -// 客户端直接调用FastAPI后端,不再通过Remix API routes中转 +// 获取API基础URL +// 客户端调用Remix API routes(/api),由Remix服务端代理到FastAPI后端 const getApiBaseUrl = () => { - // 在客户端,从 API_BASE_URL 配置获取后端地址 - if (typeof window !== 'undefined') { - // 客户端:需要从某处获取 baseUrl 配置 - // 方案1: 从环境变量 - // 方案2: 从 window 全局变量 - // 方案3: 硬编码端口映射 - - // 获取当前端口 - const port = window.location.port; - - // 端口到后端地址的映射(与 api-config.ts 保持一致) - const portToBackend: Record = { - '5173': 'http://172.16.0.55:8000', - '51703': 'http://172.16.0.55:8073', - '51704': 'http://10.79.97.17:8001', - '51705': 'http://10.79.97.17:8002', - '51706': 'http://10.79.97.17:8003', - '51707': 'http://10.79.97.17:8004', - '51708': 'http://10.79.97.17:8005', - }; - - return portToBackend[port] || 'http://172.16.0.55:8000'; - } - - // 服务端:不应该被调用 - return ''; + // 客户端使用相对路径调用Remix API routes + return '/api'; }; // 聊天应用配置 export const CHAT_CONFIG = { - // API相关配置 - 客户端直接调用FastAPI后端的/dify路由 - // 客户端 → FastAPI /dify/* → Dify - API_URL: `${getApiBaseUrl()}/dify`, + // API相关配置 - 客户端调用Remix API routes + // 客户端 → Remix /api/* → FastAPI /dify/* → Dify + API_URL: getApiBaseUrl(), // 应用ID - 用于localStorage key(固定值) APP_ID: 'docreview-chat',