level 3 basics
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -10,6 +11,24 @@ class Settings:
|
||||
log_level: str
|
||||
shutdown_timeout: int
|
||||
session_ttl_seconds: int
|
||||
database_url: str
|
||||
|
||||
|
||||
def _database_url() -> str:
|
||||
"""DATABASE_URL_FILE wins. The value is not logged."""
|
||||
file_name = os.getenv('DATABASE_URL_FILE', '').strip()
|
||||
if file_name:
|
||||
try:
|
||||
value = Path(file_name).read_text(encoding='utf-8').strip()
|
||||
except OSError:
|
||||
raise RuntimeError('DATABASE_URL_FILE could not be read') from None
|
||||
if not value:
|
||||
raise RuntimeError('DATABASE_URL_FILE is empty')
|
||||
return value
|
||||
value = os.getenv('DATABASE_URL', '').strip()
|
||||
if not value:
|
||||
raise RuntimeError('DATABASE_URL or DATABASE_URL_FILE is required')
|
||||
return value
|
||||
|
||||
|
||||
def load_settings() -> Settings:
|
||||
@@ -20,4 +39,5 @@ def load_settings() -> Settings:
|
||||
log_level=os.getenv('LOG_LEVEL', 'info').lower(),
|
||||
shutdown_timeout=int(os.getenv('SHUTDOWN_TIMEOUT_SECONDS', '5')),
|
||||
session_ttl_seconds=int(os.getenv('SESSION_TTL_SECONDS', '3600')),
|
||||
database_url=_database_url(),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user