first-update
This commit is contained in:
3
backend/app/core/__init__.py
Normal file
3
backend/app/core/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Core module initialization
|
||||
"""
|
||||
49
backend/app/core/config.py
Normal file
49
backend/app/core/config.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""
|
||||
Application Configuration
|
||||
"""
|
||||
|
||||
from functools import lru_cache
|
||||
from pydantic_settings import BaseSettings
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
"""Application settings"""
|
||||
|
||||
# App
|
||||
APP_NAME: str = "YG-Dataset"
|
||||
DEBUG: bool = True
|
||||
HOST: str = "0.0.0.0"
|
||||
PORT: int = 8000
|
||||
|
||||
# Database - 使用 SQLite 进行开发/测试
|
||||
# 生产环境可切换为 PostgreSQL
|
||||
DATABASE_URL: str = Field(
|
||||
default="sqlite:///./ygdataset.db",
|
||||
description="Database connection URL (sqlite:// or postgresql+asyncpg://)"
|
||||
)
|
||||
DATABASE_URL_SYNC: str = Field(
|
||||
default="sqlite:///./ygdataset.db",
|
||||
description="Synchronous database connection URL"
|
||||
)
|
||||
|
||||
# Redis
|
||||
REDIS_URL: str = "redis://localhost:6379/0"
|
||||
|
||||
# File Storage
|
||||
UPLOAD_DIR: str = "./uploads"
|
||||
MAX_FILE_SIZE: int = 100 * 1024 * 1024 # 100MB
|
||||
|
||||
# LLM Settings
|
||||
DEFAULT_MODEL_PROVIDER: str = "openai"
|
||||
DEFAULT_MODEL_NAME: str = "gpt-4o-mini"
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
extra = "allow"
|
||||
|
||||
|
||||
@lru_cache()
|
||||
def get_settings() -> Settings:
|
||||
"""Get cached settings"""
|
||||
return Settings()
|
||||
68
backend/app/core/database.py
Normal file
68
backend/app/core/database.py
Normal file
@@ -0,0 +1,68 @@
|
||||
"""
|
||||
Database Configuration and Session Management
|
||||
支持 SQLite 和 PostgreSQL
|
||||
"""
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
from sqlalchemy import create_engine
|
||||
from app.core.config import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
|
||||
def get_engine_config():
|
||||
"""根据数据库类型返回引擎配置"""
|
||||
if settings.DATABASE_URL.startswith("sqlite"):
|
||||
return {"echo": settings.DEBUG}
|
||||
else:
|
||||
return {
|
||||
"echo": settings.DEBUG,
|
||||
"pool_pre_ping": True,
|
||||
"pool_size": 10,
|
||||
"max_overflow": 20,
|
||||
}
|
||||
|
||||
|
||||
# Async engine for FastAPI
|
||||
async_engine = create_async_engine(
|
||||
settings.DATABASE_URL,
|
||||
**get_engine_config()
|
||||
)
|
||||
|
||||
# Sync engine for migrations
|
||||
sync_engine = create_engine(
|
||||
settings.DATABASE_URL_SYNC,
|
||||
echo=settings.DEBUG,
|
||||
pool_pre_ping=True,
|
||||
)
|
||||
|
||||
|
||||
# Async session factory
|
||||
AsyncSessionLocal = async_sessionmaker(
|
||||
async_engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False,
|
||||
autocommit=False,
|
||||
autoflush=False,
|
||||
)
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
"""Base class for all models"""
|
||||
pass
|
||||
|
||||
|
||||
async def init_db():
|
||||
"""Initialize database tables"""
|
||||
async with async_engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
|
||||
async def get_db() -> AsyncSession:
|
||||
"""Dependency for getting database session"""
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
await session.close()
|
||||
Reference in New Issue
Block a user