27 lines
820 B
Python
27 lines
820 B
Python
from pathlib import Path
|
|
from typing import Any, Dict, Optional
|
|
|
|
from pydantic import BaseSettings, validator
|
|
|
|
class Settings(BaseSettings):
|
|
"""Application settings."""
|
|
API_V1_STR: str = "/api/v1"
|
|
PROJECT_NAME: str = "Generic REST API Service"
|
|
|
|
# Configure database
|
|
DB_DIR: Path = Path("/app") / "storage" / "db"
|
|
SQLALCHEMY_DATABASE_URL: str = ""
|
|
|
|
@validator("SQLALCHEMY_DATABASE_URL", pre=True)
|
|
def assemble_db_url(cls, v: Optional[str], values: Dict[str, Any]) -> str:
|
|
if v and len(v) > 0:
|
|
return v
|
|
|
|
# Ensure DB directory exists
|
|
db_dir = values.get("DB_DIR")
|
|
if db_dir:
|
|
Path(db_dir).mkdir(parents=True, exist_ok=True)
|
|
|
|
return f"sqlite:///{values.get('DB_DIR')}/db.sqlite"
|
|
|
|
settings = Settings() |