diff --git a/app/db.py b/app/db.py index a0b84b4..1218416 100644 --- a/app/db.py +++ b/app/db.py @@ -21,6 +21,19 @@ if not DATABASE_URL: engine = create_engine( DATABASE_URL, pool_recycle=3600, - pool_pre_ping=True + pool_pre_ping=True, + connect_args={ + "keepalives": 1, + "keepalives_idle": 30, + "keepalives_interval": 10, + "keepalives_count": 5 + } ) -SessionLocal = sessionmaker(bind=engine) \ No newline at end of file +SessionLocal = sessionmaker(bind=engine) + +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() \ No newline at end of file diff --git a/app/graphql/queries.py b/app/graphql/queries.py index 0b1e08b..8b7f4c3 100644 --- a/app/graphql/queries.py +++ b/app/graphql/queries.py @@ -11,7 +11,7 @@ class Query: @strawberry.field async def me(self, info: Info[CustomContext, None]) -> Optional[UserType]: # 从 context 获取当前登录用户 - user = await info.context.get_current_user() + user = await info.context. xzz() # Strawberry 会自动将 SQLAlchemy 实例映射为 UserType return user diff --git a/app/graphql/types.py b/app/graphql/types.py index c83bcab..d37ecbb 100644 --- a/app/graphql/types.py +++ b/app/graphql/types.py @@ -4,17 +4,35 @@ from datetime import datetime from strawberry.types import Info from app.utils.format import get_image_url -from app.graphql.context import CustomContext @strawberry.type class UserType: - id: strawberry.ID + id: str email: str - name: Optional[str] + name: str + avatar: str handle: Optional[str] - avatar: Optional[str] + + # 互动统计字段 following_count: int follower_count: int + + # 统计汇总字段 + owner_project_count: int + owner_likes_count: int + owner_favorites_count: int + owner_shares_count: int + + admin_project_count: int + admin_likes_count: int + admin_favorites_count: int + admin_shares_count: int + + user_project_count: int + user_likes_count: int + user_favorites_count: int + user_shares_count: int + created_at: datetime # 动态解析字段:例如自动处理头像的绝对路径 diff --git a/auth/oauth/google.py b/auth/oauth/google.py index de92dbd..e6fca5a 100644 --- a/auth/oauth/google.py +++ b/auth/oauth/google.py @@ -8,8 +8,9 @@ import secrets import json from urllib.parse import quote from dotenv import load_dotenv +from sqlalchemy.orm import Session -from app.db import SessionLocal +from app.db import SessionLocal, get_db from app.models import User from auth.tokens import create_jwt_token, create_refresh_token, decode_token from app.utils.format import get_image_url @@ -94,7 +95,13 @@ def login_google(): return response @router.get("/auth/callback") -async def auth_callback(request: Request, code: str, state: str, background_tasks: BackgroundTasks): +async def auth_callback( + request: Request, + code: str, + state: str, + background_tasks: BackgroundTasks, + db: Session = Depends(get_db) +): cookie_state = request.cookies.get("oauth_state") if not cookie_state or cookie_state != state: @@ -121,10 +128,7 @@ async def auth_callback(request: Request, code: str, state: str, background_task ) user_info = user_res.json() - - db = SessionLocal() - try: user = db.query(User).filter(User.email == user_info["email"]).first() if not user: @@ -166,8 +170,6 @@ async def auth_callback(request: Request, code: str, state: str, background_task # 这里不需要再次 refresh,除非后面还要用到更新后的 user 对象 # db.refresh(user) - finally: - db.close() jwt_token = create_jwt_token({ "sub": user.id,