feat(backend): 更新核心模块和文件处理

- 更新配置模块 (config.py)
- 更新数据库连接 (database.py)
- 更新主应用入口 (main.py)
- 更新数据模型 (models.py)
- 更新基础 Schema (base.py)
- 更新文件处理器 (docx, excel, pdf)
- 更新 Dockerfile

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Developer
2026-03-17 17:30:11 +08:00
parent db11429290
commit 47d1da7cea
10 changed files with 393 additions and 189 deletions

View File

@@ -4,7 +4,7 @@ Application Configuration
from functools import lru_cache
from pydantic_settings import BaseSettings
from pydantic import Field
from pydantic import Field, field_validator
class Settings(BaseSettings):
@@ -15,12 +15,16 @@ class Settings(BaseSettings):
DEBUG: bool = True
HOST: str = "0.0.0.0"
PORT: int = 8000
ALLOWED_ORIGINS: str = Field(
default="*",
description="Comma-separated list of allowed CORS origins"
)
# Database - 使用 SQLite 进行开发/测试
# 生产环境可切换为 PostgreSQL
DATABASE_URL: str = Field(
default="sqlite:///./ygdataset.db",
description="Database connection URL (sqlite:// or postgresql+asyncpg://)"
default="sqlite+aiosqlite:///./ygdataset.db",
description="Database connection URL (sqlite+aiosqlite:// or postgresql+asyncpg://)"
)
DATABASE_URL_SYNC: str = Field(
default="sqlite:///./ygdataset.db",
@@ -38,8 +42,31 @@ class Settings(BaseSettings):
DEFAULT_MODEL_PROVIDER: str = "openai"
DEFAULT_MODEL_NAME: str = "gpt-4o-mini"
# Security
SECRET_KEY: str = Field(
default="your-secret-key-change-in-production",
description="Secret key for JWT and other security operations"
)
API_KEY_HEADER: str = "X-API-Key"
# Pagination
DEFAULT_PAGE_SIZE: int = 20
MAX_PAGE_SIZE: int = 100
# Logging
LOG_LEVEL: str = "INFO"
@field_validator("MAX_FILE_SIZE")
@classmethod
def validate_max_file_size(cls, v: int) -> int:
"""Validate max file size (max 500MB)"""
if v > 500 * 1024 * 1024:
return 500 * 1024 * 1024
return v
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
extra = "allow"
@@ -47,3 +74,7 @@ class Settings(BaseSettings):
def get_settings() -> Settings:
"""Get cached settings"""
return Settings()
# Create global settings instance
settings = get_settings()

View File

@@ -2,25 +2,32 @@
Database Configuration and Session Management
支持 SQLite 和 PostgreSQL
"""
import logging
from contextlib import asynccontextmanager
from typing import AsyncGenerator
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy import create_engine
from sqlalchemy import create_engine, event
from sqlalchemy.pool import NullPool
from app.core.config import get_settings
logger = logging.getLogger(__name__)
settings = get_settings()
def get_engine_config():
"""根据数据库类型返回引擎配置"""
if settings.DATABASE_URL.startswith("sqlite"):
return {"echo": settings.DEBUG}
return {"echo": settings.DEBUG, "poolclass": NullPool}
else:
return {
"echo": settings.DEBUG,
"pool_pre_ping": True,
"pool_size": 10,
"max_overflow": 20,
"pool_recycle": 3600,
"pool_timeout": 30,
}
@@ -30,14 +37,14 @@ async_engine = create_async_engine(
**get_engine_config()
)
# Sync engine for migrations
# Sync engine for migrations (use NullPool for SQLite)
sync_engine = create_engine(
settings.DATABASE_URL_SYNC,
echo=settings.DEBUG,
pool_pre_ping=True,
poolclass=NullPool if settings.DATABASE_URL_SYNC.startswith("sqlite") else None,
)
# Async session factory
AsyncSessionLocal = async_sessionmaker(
async_engine,
@@ -55,8 +62,31 @@ class Base(DeclarativeBase):
async def init_db():
"""Initialize database tables"""
logger.info("Initializing database...")
async with async_engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
logger.info("Database initialized successfully")
async def close_db():
"""Close database connections"""
logger.info("Closing database connections...")
await async_engine.dispose()
logger.info("Database connections closed")
@asynccontextmanager
async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
"""Context manager for database sessions with automatic cleanup"""
session = AsyncSessionLocal()
try:
yield session
except Exception as e:
logger.error(f"Database session error: {str(e)}")
await session.rollback()
raise
finally:
await session.close()
async def get_db() -> AsyncSession:
@@ -64,5 +94,9 @@ async def get_db() -> AsyncSession:
async with AsyncSessionLocal() as session:
try:
yield session
except Exception as e:
logger.error(f"Database error in dependency: {str(e)}")
await session.rollback()
raise
finally:
await session.close()