41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
"""Runtime configuration, read from environment variables."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Config:
|
|
data_dir: Path
|
|
plugins_dir: Path
|
|
database_url: str
|
|
bootstrap_admin_token: str | None
|
|
invite_code: str | None
|
|
admin_username: str
|
|
max_plugin_bytes: int
|
|
|
|
@classmethod
|
|
def from_env(cls) -> "Config":
|
|
data_dir = Path(os.environ.get("DSH_DATA_DIR", "data")).resolve()
|
|
database_url = os.environ.get("DSH_DATABASE_URL", "").strip()
|
|
if not database_url:
|
|
raise RuntimeError(
|
|
"DSH_DATABASE_URL is required, e.g. "
|
|
"postgresql://dsh:secret@localhost:5432/dsh"
|
|
)
|
|
return cls(
|
|
data_dir=data_dir,
|
|
plugins_dir=data_dir / "plugins",
|
|
database_url=database_url,
|
|
bootstrap_admin_token=os.environ.get("DSH_BOOTSTRAP_ADMIN_TOKEN") or None,
|
|
invite_code=os.environ.get("DSH_INVITE_CODE") or None,
|
|
admin_username=os.environ.get("DSH_ADMIN_USERNAME", "admin"),
|
|
max_plugin_bytes=int(os.environ.get("DSH_MAX_PLUGIN_MB", "20")) * 1024 * 1024,
|
|
)
|
|
|
|
def ensure_dirs(self) -> None:
|
|
self.data_dir.mkdir(parents=True, exist_ok=True)
|
|
self.plugins_dir.mkdir(parents=True, exist_ok=True)
|