fix bugs for launch: postgresql url for sqlalchemy; auto loading env

This commit is contained in:
2026-05-19 01:39:57 +08:00
parent a83befe109
commit 86b030930c
7 changed files with 71 additions and 7 deletions
+15
View File
@@ -0,0 +1,15 @@
from fastapi import APIRouter
from .deps import get_current_user
from .jwt import create_jwt_token, create_refresh_token, verify_access_token
# 统一创建并导出路由器,满足 main.py 的 include_router 需求
router = APIRouter(tags=["Authentication"])
# 显式声明这个包对外暴露的接口
__all__ = [
"router",
"get_current_user",
"create_jwt_token",
"create_refresh_token",
"verify_access_token",
]
+1 -1
View File
@@ -1,6 +1,6 @@
from fastapi import Request, HTTPException
import app.auth.jwt as jwt
import os
from . import jwt
JWT_SECRET = os.getenv("JWT_SECRET")
JWT_ALGORITHM = os.getenv("JWT_ALGORITHM", "HS256")
+1 -1
View File
@@ -1,6 +1,6 @@
import app.auth.jwt as jwt
from datetime import datetime, timedelta, timezone
import os
import jwt
from fastapi import HTTPException, status
JWT_SECRET = os.getenv("JWT_SECRET")
+14
View File
@@ -1,8 +1,22 @@
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from dotenv import load_dotenv
# 加载 .env 文件
load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL")
# 自动将旧版的 postgres:// 替换为 SQLAlchemy 强要求的 postgresql://
if DATABASE_URL and DATABASE_URL.startswith("postgres://"):
DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://", 1)
elif DATABASE_URL and DATABASE_URL.startswith("postgres+"):
DATABASE_URL = DATABASE_URL.replace("postgres+", "postgresql+", 1)
# 如果环境变量彻底缺失,做一个安全的报错提示
if not DATABASE_URL:
raise ValueError("DATABASE_URL environment variable is not set or empty.")
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(bind=engine)
+2 -5
View File
@@ -24,8 +24,5 @@ schema = Schema(query=Query, mutation=Mutation)
graphql_router = GraphQLRouter(schema, context_getter=get_context)
# 挂载路由
# 保留现有的 auth 路由供 Google OAuth 回调使用
app.include_router(auth_router)
# 挂载 GraphQL 终点,前端以后只需要请求 /graphql
app.include_router(graphql_router, prefix="/graphql")
app.include_router(auth_router) # 保留现有的 auth 路由供 Google OAuth 回调使用
app.include_router(graphql_router, prefix="/graphql") # 挂载 GraphQL 终点,前端以后只需要请求 /graphql