mirror of
https://github.com/zvx-echo6/central.git
synced 2026-05-22 02:24:38 +02:00
refactor: remove config_source flag from bootstrap settings
Database config is now the only option, no need for feature flag. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
4376588baf
commit
a1e81bae8a
1 changed files with 46 additions and 61 deletions
|
|
@ -1,61 +1,46 @@
|
||||||
"""Bootstrap configuration from environment variables.
|
"""Bootstrap configuration from environment variables.
|
||||||
|
|
||||||
This module provides early-stage configuration loading from environment
|
This module provides early-stage configuration loading from environment
|
||||||
variables or a .env file. Used before the database-backed config store
|
variables or a .env file. Used before the database-backed config store
|
||||||
is available.
|
is available.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
|
|
||||||
from pydantic import Field, field_validator
|
from pydantic import Field
|
||||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
"""Bootstrap settings loaded from environment or .env file."""
|
"""Bootstrap settings loaded from environment or .env file."""
|
||||||
|
|
||||||
model_config = SettingsConfigDict(
|
model_config = SettingsConfigDict(
|
||||||
env_prefix="CENTRAL_",
|
env_prefix="CENTRAL_",
|
||||||
env_file=".env",
|
env_file=".env",
|
||||||
env_file_encoding="utf-8",
|
env_file_encoding="utf-8",
|
||||||
extra="ignore",
|
extra="ignore",
|
||||||
)
|
)
|
||||||
|
|
||||||
db_dsn: str = Field(description="PostgreSQL connection string")
|
db_dsn: str = Field(description="PostgreSQL connection string")
|
||||||
nats_url: str = Field(default="nats://localhost:4222", description="NATS server URL")
|
nats_url: str = Field(default="nats://localhost:4222", description="NATS server URL")
|
||||||
master_key_path: Path = Field(
|
master_key_path: Path = Field(
|
||||||
default=Path("/etc/central/master.key"),
|
default=Path("/etc/central/master.key"),
|
||||||
description="Path to AES-256 master key file",
|
description="Path to AES-256 master key file",
|
||||||
)
|
)
|
||||||
log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR"] = Field(
|
log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR"] = Field(
|
||||||
default="INFO",
|
default="INFO",
|
||||||
description="Logging level",
|
description="Logging level",
|
||||||
)
|
)
|
||||||
config_source: Literal["toml", "db"] = Field(
|
|
||||||
default="toml",
|
|
||||||
description="Configuration source: 'toml' for TOML file, 'db' for database",
|
@lru_cache
|
||||||
)
|
def get_settings(env_file: Path | None = None) -> Settings:
|
||||||
config_toml_path: Path = Field(
|
"""Load settings, optionally from a specific .env file.
|
||||||
default=Path("/etc/central/central.toml"),
|
|
||||||
description="Path to TOML config file (when config_source=toml)",
|
Results are cached. Call get_settings.cache_clear() to reload.
|
||||||
)
|
"""
|
||||||
|
if env_file is not None:
|
||||||
@field_validator("config_source")
|
return Settings(_env_file=env_file)
|
||||||
@classmethod
|
return Settings()
|
||||||
def validate_config_source(cls, v: str) -> str:
|
|
||||||
if v not in ("toml", "db"):
|
|
||||||
raise ValueError(f"config_source must be 'toml' or 'db', got {v!r}")
|
|
||||||
return v
|
|
||||||
|
|
||||||
|
|
||||||
@lru_cache
|
|
||||||
def get_settings(env_file: Path | None = None) -> Settings:
|
|
||||||
"""Load settings, optionally from a specific .env file.
|
|
||||||
|
|
||||||
Results are cached. Call get_settings.cache_clear() to reload.
|
|
||||||
"""
|
|
||||||
if env_file is not None:
|
|
||||||
return Settings(_env_file=env_file)
|
|
||||||
return Settings()
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue