9dfe000f2d
- Rename get_current_user to get_current_user_id in auth/deps.py to clarify intent - Remove legacy /me, /logout routes, and placeholder function from auth/__init__.py - Remove dependency_overrides from app/main.py - Update CustomContext to resolve and cache full User objects via db query
52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
import os
|
|
from fastapi import FastAPI, Depends, HTTPException
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from strawberry import Schema
|
|
from strawberry.fastapi import GraphQLRouter
|
|
from dotenv import load_dotenv
|
|
|
|
from auth import router as auth_router
|
|
|
|
from app.db import engine, SessionLocal
|
|
from app.models import Base, User
|
|
from app.utils.format import get_image_url
|
|
|
|
# 导入 GraphQL 组件
|
|
from app.graphql.queries import Query
|
|
from app.graphql.mutations import Mutation
|
|
from app.graphql.context import get_context
|
|
|
|
# 自动建表
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
# 创建 FastAPI 实例
|
|
app = FastAPI()
|
|
|
|
# 获取环境变量字符串,并转成列表
|
|
load_dotenv()
|
|
origins_str = os.getenv("CORS_ORIGINS", "http://localhost:3000")
|
|
origins = [origin.strip() for origin in origins_str.split(",")]
|
|
|
|
# 配置 CORS 中间件,允许前端访问
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 组装 GraphQL Schema
|
|
schema = Schema(query=Query, mutation=Mutation)
|
|
|
|
# 创建 GraphQL 路由并注入上下文
|
|
graphql_router = GraphQLRouter(schema, context_getter=get_context)
|
|
|
|
# 挂载路由
|
|
app.include_router(auth_router) # 保留现有的 auth 路由供 Google OAuth 回调使用
|
|
app.include_router(graphql_router, prefix="/graphql") # 挂载 GraphQL 终点,前端以后只需要请求 /graphql
|
|
|
|
# 打印路由测试
|
|
for route in app.routes:
|
|
methods = getattr(route, "methods", "N/A")
|
|
print(f"Path: {route.path} | Name: {route.name} | Methods: {methods}") |