refactor: 后端从Node.js重写为Python FastAPI

This commit is contained in:
wangwuww111
2026-03-04 18:31:48 +08:00
parent 729bb3aaeb
commit ac0accdde6
40 changed files with 1096 additions and 2035 deletions

36
server/app/database.py Normal file
View File

@@ -0,0 +1,36 @@
"""
数据库连接
"""
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker, declarative_base
from app.config import get_settings
settings = get_settings()
# 创建异步引擎
engine = create_async_engine(
settings.database_url,
echo=settings.debug,
pool_pre_ping=True,
pool_size=10,
max_overflow=20
)
# 创建异步会话
AsyncSessionLocal = sessionmaker(
bind=engine,
class_=AsyncSession,
expire_on_commit=False
)
# 基类
Base = declarative_base()
async def get_db():
"""获取数据库会话"""
async with AsyncSessionLocal() as session:
try:
yield session
finally:
await session.close()