advent22/api/advent22_api/core/settings.py

82 lines
1.5 KiB
Python
Raw Normal View History

from typing import TypeVar
2023-09-03 15:55:49 +00:00
from pydantic import BaseModel
from pydantic_settings import BaseSettings, SettingsConfigDict
2022-10-10 23:46:04 +00:00
T = TypeVar("T")
2022-10-10 23:46:04 +00:00
class DavSettings(BaseModel):
"""
Connection to a DAV server.
"""
protocol: str = "https"
host: str = "example.com"
path: str = "/remote.php/webdav"
prefix: str = "/advent22"
2023-09-10 02:59:57 +00:00
username: str = "advent22_user"
password: str = "password"
cache_ttl: int = 60 * 30
config_filename: str = "config.toml"
2022-10-10 23:46:04 +00:00
@property
def url(self) -> str:
"""
Combined DAV URL.
"""
return f"{self.protocol}://{self.host}{self.path}{self.prefix}"
2022-10-08 23:36:16 +00:00
class Settings(BaseSettings):
"""
Per-run settings.
"""
2023-09-03 15:55:49 +00:00
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
env_nested_delimiter="__",
)
2022-10-08 23:36:16 +00:00
#####
# general settings
#####
production_mode: bool = False
2023-09-18 18:16:34 +00:00
ui_directory: str = "/usr/local/share/advent22_ui/html"
2022-10-08 23:36:16 +00:00
#####
# openapi settings
#####
def __dev_value(self, value: T) -> T | None:
if self.production_mode:
return None
return value
@property
def openapi_url(self) -> str | None:
return self.__dev_value("/api/openapi.json")
@property
def docs_url(self) -> str | None:
return self.__dev_value("/api/docs")
@property
def redoc_url(self) -> str | None:
return self.__dev_value("/api/redoc")
2022-10-08 23:36:16 +00:00
2022-10-10 23:46:04 +00:00
#####
# webdav settings
#####
webdav: DavSettings = DavSettings()
2022-10-08 23:36:16 +00:00
SETTINGS = Settings()