51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
from functools import lru_cache
|
|
|
|
from sqlalchemy.engine import URL
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore")
|
|
|
|
app_name: str = "Plast Track MVP"
|
|
database_url: str | None = None
|
|
postgres_host: str = "database"
|
|
postgres_port: int = 5432
|
|
postgres_db: str = "plasttrack"
|
|
postgres_user: str = "plasttrack"
|
|
postgres_password: str = "plasttrack"
|
|
mqtt_host: str = "mqtt"
|
|
mqtt_port: int = 1883
|
|
mqtt_topic: str = "plasttrack/machines/+/events"
|
|
mqtt_client_id: str = "plast-track-backend"
|
|
frontend_origin: str | None = None
|
|
frontend_origins: str = "http://localhost:5173,http://127.0.0.1:5173,http://localhost:5174,http://127.0.0.1:5174"
|
|
downtime_check_interval_sec: int = 5
|
|
database_startup_retries: int = 30
|
|
database_startup_retry_delay_sec: float = 2.0
|
|
|
|
@property
|
|
def sqlalchemy_database_url(self) -> str:
|
|
if self.database_url:
|
|
return self.database_url
|
|
return URL.create(
|
|
"postgresql+psycopg",
|
|
username=self.postgres_user,
|
|
password=self.postgres_password,
|
|
host=self.postgres_host,
|
|
port=self.postgres_port,
|
|
database=self.postgres_db,
|
|
).render_as_string(hide_password=False)
|
|
|
|
@property
|
|
def cors_allowed_origins(self) -> list[str]:
|
|
configured_origins = [origin.strip() for origin in self.frontend_origins.split(",") if origin.strip()]
|
|
if self.frontend_origin:
|
|
configured_origins.append(self.frontend_origin.strip())
|
|
return list(dict.fromkeys(configured_origins))
|
|
|
|
|
|
@lru_cache
|
|
def get_settings() -> Settings:
|
|
return Settings()
|