Songyou commited on
Commit
36f8047
·
verified ·
1 Parent(s): 8841e2c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +11 -1
main.py CHANGED
@@ -3,7 +3,7 @@ from pydantic import BaseModel
3
  from fastapi.responses import JSONResponse, HTMLResponse
4
  from fastapi.staticfiles import StaticFiles
5
  from fastapi.responses import FileResponse
6
-
7
  import logging
8
  # 创建 FastAPI 实例
9
  app = FastAPI()
@@ -16,6 +16,16 @@ class UserInput(BaseModel):
16
 
17
  app.mount("/", StaticFiles(directory="static", html=True), name="static")
18
 
 
 
 
 
 
 
 
 
 
 
19
  @app.get("/")
20
  def index() -> FileResponse:
21
  return FileResponse(path="/app/static/index.html", media_type="text/html")
 
3
  from fastapi.responses import JSONResponse, HTMLResponse
4
  from fastapi.staticfiles import StaticFiles
5
  from fastapi.responses import FileResponse
6
+ from fastapi.middleware.cors import CORSMiddleware
7
  import logging
8
  # 创建 FastAPI 实例
9
  app = FastAPI()
 
16
 
17
  app.mount("/", StaticFiles(directory="static", html=True), name="static")
18
 
19
+
20
+ # 添加 CORS 中间件
21
+ app.add_middleware(
22
+ CORSMiddleware,
23
+ allow_origins=["*"], # 允许所有来源访问,或者你可以根据需要限制为特定来源
24
+ allow_credentials=True,
25
+ allow_methods=["*"], # 允许所有 HTTP 方法
26
+ allow_headers=["*"], # 允许所有头部
27
+ )
28
+
29
  @app.get("/")
30
  def index() -> FileResponse:
31
  return FileResponse(path="/app/static/index.html", media_type="text/html")