feat: 跨域设置

This commit is contained in:
martsforever
2026-03-31 19:32:11 +08:00
parent 952ce5c189
commit d2ee7c1d9b
3 changed files with 12 additions and 1 deletions
+1
View File
@@ -4,3 +4,4 @@ LLM_KEY_BAILIAN=sk-248f811295914adcad837XXXXXXXXXXX # 阿里云百炼模型
LLM_KEY_DEEPSEEK=sk-a89d0ff9421a43fca5f0xxxxxxxxxxxx # Deepseek模型服务平台key LLM_KEY_DEEPSEEK=sk-a89d0ff9421a43fca5f0xxxxxxxxxxxx # Deepseek模型服务平台key
SERVER_PORT = 7004 # 服务启动端口 SERVER_PORT = 7004 # 服务启动端口
SERVER_ENABLE_CORS = False # 是否允许跨域
+1
View File
@@ -9,6 +9,7 @@ class EnvSettings(BaseSettings):
llm_key_deepseek: str = Field(..., env="LLM_KEY_DEEPSEEK") llm_key_deepseek: str = Field(..., env="LLM_KEY_DEEPSEEK")
server_port: int = Field(..., env="SERVER_PORT") server_port: int = Field(..., env="SERVER_PORT")
server_enable_cors: bool = Field(..., env="SERVER_ENABLE_CORS")
class Config: class Config:
env_file = ".env" env_file = ".env"
+10 -1
View File
@@ -6,6 +6,7 @@ from fastapi.openapi.docs import get_swagger_ui_oauth2_redirect_html, get_redoc_
from fastapi.responses import RedirectResponse from fastapi.responses import RedirectResponse
from langchain.chat_models import init_chat_model from langchain.chat_models import init_chat_model
from langserve import add_routes from langserve import add_routes
from starlette.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles from starlette.staticfiles import StaticFiles
from app.config.env import env from app.config.env import env
@@ -32,4 +33,12 @@ app = FastAPI(
for add_route_func in routes: for add_route_func in routes:
add_route_func(app) add_route_func(app)
print("/*---------------------------------------main-------------------------------------------*/") if env.server_enable_cors:
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"],
)