feat: bootstrap FastAPI configuration
This commit is contained in:
36
app/config.py
Normal file
36
app/config.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic import Field, PositiveInt, model_validator
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_file=".env", extra="ignore", case_sensitive=False)
|
||||
|
||||
admin_password: str = Field(alias="ADMIN_PASSWORD", min_length=1)
|
||||
secret_key: str = Field(alias="SECRET_KEY", min_length=16)
|
||||
credential_encryption_key: str = Field(alias="CREDENTIAL_ENCRYPTION_KEY", min_length=1)
|
||||
data_dir: Path = Field(default=Path("/data"), alias="DATA_DIR")
|
||||
database_url: str | None = Field(default=None, alias="DATABASE_URL")
|
||||
sync_interval_minutes: PositiveInt = Field(default=5, alias="SYNC_INTERVAL_MINUTES")
|
||||
|
||||
@model_validator(mode="after")
|
||||
def derive_paths(self) -> "Settings":
|
||||
self.data_dir = self.data_dir.expanduser().resolve()
|
||||
if self.database_url is None:
|
||||
self.database_url = f"sqlite:///{self.data_dir / 'app.db'}"
|
||||
return self
|
||||
|
||||
@property
|
||||
def tokens_dir(self) -> Path:
|
||||
return self.data_dir / "tokens"
|
||||
|
||||
@property
|
||||
def activities_dir(self) -> Path:
|
||||
return self.data_dir / "activities"
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_settings() -> Settings:
|
||||
return Settings()
|
||||
Reference in New Issue
Block a user