import secrets from pathlib import Path from typing import List from pydantic import AnyHttpUrl, validator from pydantic_settings import BaseSettings class Settings(BaseSettings): # Base configuration API_V1_STR: str = "/api/v1" PROJECT_NAME: str = "User Authentication Service" PROJECT_DESCRIPTION: str = "FastAPI User Authentication Service with JWT" PROJECT_VERSION: str = "0.1.0" # Security configuration SECRET_KEY: str = secrets.token_urlsafe(32) ACCESS_TOKEN_EXPIRE_MINUTES: int = 30 REFRESH_TOKEN_EXPIRE_DAYS: int = 7 ALGORITHM: str = "HS256" # Database configuration DB_DIR: Path = Path("/app") / "storage" / "db" SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite" # CORS configuration BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = [] @validator("BACKEND_CORS_ORIGINS", pre=True) def assemble_cors_origins(cls, v: str | List[str]) -> List[AnyHttpUrl]: if isinstance(v, str) and not v.startswith("["): return [i.strip() for i in v.split(",")] elif isinstance(v, (list, str)): return v raise ValueError(v) class Config: case_sensitive = True env_file = ".env" # Create settings instance settings = Settings() # Ensure DB directory exists settings.DB_DIR.mkdir(parents=True, exist_ok=True)