chore: 初始化项目与后端基础工程
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
from functools import lru_cache
|
||||
from typing import Any
|
||||
|
||||
from pydantic import field_validator
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=".env",
|
||||
env_file_encoding="utf-8",
|
||||
case_sensitive=False,
|
||||
extra="ignore",
|
||||
)
|
||||
|
||||
app_name: str = "财务 AI 助手 API"
|
||||
app_version: str = "1.0.0"
|
||||
debug: bool = True
|
||||
secret_key: str = "change-me-in-production"
|
||||
allowed_origins: list[str] = ["http://localhost:3000"]
|
||||
|
||||
database_url: str = "postgresql+asyncpg://s2f_user:s2f_password@localhost:5432/s2f_db"
|
||||
|
||||
jwt_secret_key: str = "change-me-in-production"
|
||||
jwt_algorithm: str = "HS256"
|
||||
jwt_access_token_expire_minutes: int = 60
|
||||
|
||||
openai_api_key: str = ""
|
||||
openai_model: str = "gpt-4-turbo-preview"
|
||||
zhipu_api_key: str = ""
|
||||
ai_provider: str = "zhipu"
|
||||
ai_api_key: str = ""
|
||||
|
||||
upload_dir: str = "./uploads"
|
||||
max_upload_size: int = 10_485_760
|
||||
|
||||
log_level: str = "INFO"
|
||||
|
||||
@field_validator("allowed_origins", mode="before")
|
||||
@classmethod
|
||||
def parse_allowed_origins(cls, value: Any) -> list[str]:
|
||||
if isinstance(value, str):
|
||||
return [origin.strip() for origin in value.split(",") if origin.strip()]
|
||||
if isinstance(value, list):
|
||||
return value
|
||||
return ["http://localhost:3000"]
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_settings() -> Settings:
|
||||
return Settings()
|
||||
@@ -0,0 +1,17 @@
|
||||
import logging
|
||||
|
||||
import structlog
|
||||
|
||||
|
||||
def configure_logging(log_level: str = "INFO") -> None:
|
||||
level = getattr(logging, log_level.upper(), logging.INFO)
|
||||
|
||||
structlog.configure(
|
||||
processors=[
|
||||
structlog.processors.TimeStamper(fmt="iso"),
|
||||
structlog.processors.add_log_level,
|
||||
structlog.processors.JSONRenderer(ensure_ascii=False),
|
||||
],
|
||||
wrapper_class=structlog.make_filtering_bound_logger(level),
|
||||
cache_logger_on_first_use=True,
|
||||
)
|
||||
Reference in New Issue
Block a user