33 lines
799 B
Python
33 lines
799 B
Python
from pathlib import Path
|
|
from typing import Any, Dict, Optional
|
|
|
|
from pydantic import BaseSettings, validator
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
PROJECT_NAME: str = "Todo App API"
|
|
API_V1_STR: str = "/api/v1"
|
|
|
|
# Database
|
|
DB_DIR: Path = Path("/app") / "storage" / "db"
|
|
SQLALCHEMY_DATABASE_URL: Optional[str] = None
|
|
|
|
@validator("SQLALCHEMY_DATABASE_URL", pre=True)
|
|
def assemble_db_url(cls, v: Optional[str], values: Dict[str, Any]) -> str:
|
|
if v:
|
|
return v
|
|
|
|
db_dir = values.get("DB_DIR")
|
|
if db_dir:
|
|
db_dir.mkdir(parents=True, exist_ok=True)
|
|
return f"sqlite:///{db_dir}/db.sqlite"
|
|
|
|
return "sqlite:///./db.sqlite"
|
|
|
|
class Config:
|
|
env_file = ".env"
|
|
case_sensitive = True
|
|
|
|
|
|
settings = Settings()
|