第一次提交
This commit is contained in:
9
test/server/core/config.py
Normal file
9
test/server/core/config.py
Normal file
@@ -0,0 +1,9 @@
|
||||
# 可放项目配置,例如环境变量、数据库连接信息
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
class Settings(BaseSettings):
|
||||
app_name: str = "My FastAPI App"
|
||||
host: str = '0.0.0.0'
|
||||
debug: bool = True
|
||||
|
||||
settings = Settings()
|
19
test/server/main.py
Normal file
19
test/server/main.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from core.config import settings
|
||||
from routes import example
|
||||
|
||||
app = FastAPI(title=settings.app_name)
|
||||
|
||||
# 允许所有域名跨域访问
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # ⚠️ 生产环境建议指定域名
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# 注册路由模块
|
||||
app.include_router(example.router, prefix="/api")
|
8
test/server/routes/example.py
Normal file
8
test/server/routes/example.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from fastapi import APIRouter, Response
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/hello")
|
||||
def hello(response: Response):
|
||||
response.set_cookie(key="cookie1", value="value1")
|
||||
return {"message": "Hello, FastAPI with CORS!"}
|
Reference in New Issue
Block a user