advent22/api/advent22_api/core/settings.py

101 lines
2 KiB
Python
Raw Normal View History

2026-02-22 11:06:16 +00:00
from pydantic import BaseModel, Field
2023-09-03 15:55:49 +00:00
from pydantic_settings import BaseSettings, SettingsConfigDict
2022-10-10 23:46:04 +00:00
class Credentials(BaseModel):
username: str = ""
password: str = ""
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"
auth: Credentials = Credentials(
username="advent22_user",
password="password",
)
2023-09-10 02:59:57 +00:00
2023-10-29 16:08:16 +00:00
cache_ttl: int = 60 * 10
2023-09-10 02:59:57 +00:00
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
2023-10-29 16:08:16 +00:00
class RedisSettings(BaseModel):
"""
Connection to a redis server.
"""
2024-03-21 22:00:45 +00:00
host: str = "localhost"
2023-10-29 16:08:16 +00:00
port: int = 6379
db: int = 0
protocol: int = 3
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_prefix="ADVENT22__",
2023-11-09 11:58:36 +00:00
env_file="api.conf",
2023-09-03 15:55:49 +00:00
env_file_encoding="utf-8",
env_nested_delimiter="__",
)
2022-10-08 23:36:16 +00:00
#####
# general settings
#####
production_mode: bool = False
2026-02-22 11:06:16 +00:00
show_api_docs: bool = Field(
default_factory=lambda data: not data["production_mode"]
)
ui_directory: str = "/opt/advent22/ui"
2022-10-08 23:36:16 +00:00
#####
# openapi settings
#####
2026-02-22 11:06:16 +00:00
def __api_docs[T](self, value: T) -> T | None:
if self.show_api_docs:
return value
2026-02-22 11:06:16 +00:00
return None
@property
def openapi_url(self) -> str | None:
2026-02-22 11:06:16 +00:00
return self.__api_docs("/api/openapi.json")
@property
def docs_url(self) -> str | None:
2026-02-22 11:06:16 +00:00
return self.__api_docs("/api/docs")
@property
def redoc_url(self) -> str | None:
2026-02-22 11:06:16 +00:00
return self.__api_docs("/api/redoc")
2022-10-08 23:36:16 +00:00
2022-10-10 23:46:04 +00:00
#####
# webdav settings
#####
webdav: DavSettings = DavSettings()
2023-10-29 16:08:16 +00:00
redis: RedisSettings = RedisSettings()
2022-10-10 23:46:04 +00:00
2022-10-08 23:36:16 +00:00
SETTINGS = Settings()