feat:初版
This commit is contained in:
6
apps/api/app/core/__init__.py
Normal file
6
apps/api/app/core/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
# Core module exports
|
||||
from .config import get_settings, Settings
|
||||
from .logging import logger
|
||||
from .database import get_db, AsyncSessionLocal, engine
|
||||
|
||||
__all__ = ["get_settings", "Settings", "logger", "get_db", "AsyncSessionLocal", "engine"]
|
||||
42
apps/api/app/core/config.py
Normal file
42
apps/api/app/core/config.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from pydantic_settings import BaseSettings
|
||||
from functools import lru_cache
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
# App
|
||||
app_env: str = "dev"
|
||||
api_host: str = "0.0.0.0"
|
||||
api_port: int = 8000
|
||||
debug: bool = True
|
||||
|
||||
# Database
|
||||
database_url: str = "postgresql+asyncpg://user:pass@db:5432/app"
|
||||
|
||||
# Redis
|
||||
redis_url: str = "redis://redis:6379/0"
|
||||
|
||||
# LLM
|
||||
llm_provider: str = "openai"
|
||||
llm_api_key: str = ""
|
||||
llm_model: str = "gpt-4o-mini"
|
||||
llm_base_url: str | None = None
|
||||
default_temperature: float = 0.0
|
||||
|
||||
# Cache
|
||||
cache_ttl_seconds: int = 604800 # 7 days
|
||||
|
||||
# Rate Limit
|
||||
rate_limit_per_minute: int = 60
|
||||
|
||||
# Security
|
||||
secret_key: str = "change-me-in-production"
|
||||
access_token_expire_minutes: int = 30
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
extra = "ignore"
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_settings() -> Settings:
|
||||
return Settings()
|
||||
16
apps/api/app/core/database.py
Normal file
16
apps/api/app/core/database.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from ..core import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
engine = create_async_engine(settings.database_url, echo=settings.debug)
|
||||
|
||||
AsyncSessionLocal = sessionmaker(
|
||||
engine, class_=AsyncSession, expire_on_commit=False
|
||||
)
|
||||
|
||||
|
||||
async def get_db():
|
||||
async with AsyncSessionLocal() as session:
|
||||
yield session
|
||||
24
apps/api/app/core/logging.py
Normal file
24
apps/api/app/core/logging.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import logging
|
||||
import sys
|
||||
from .config import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
|
||||
def setup_logging() -> logging.Logger:
|
||||
logger = logging.getLogger("ai_translator")
|
||||
logger.setLevel(logging.DEBUG if settings.debug else logging.INFO)
|
||||
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
handler.setLevel(logging.DEBUG)
|
||||
|
||||
formatter = logging.Formatter(
|
||||
'{"time":"%(asctime)s","level":"%(levelname)s","message":"%(message)s"}'
|
||||
)
|
||||
handler.setFormatter(formatter)
|
||||
logger.addHandler(handler)
|
||||
|
||||
return logger
|
||||
|
||||
|
||||
logger = setup_logging()
|
||||
Reference in New Issue
Block a user