配置
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic import Field
|
||||
from pydantic import AliasChoices, Field, field_validator
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
@@ -25,7 +26,10 @@ class Settings(BaseSettings):
|
||||
cache_ttl_seconds: int = 900
|
||||
bundle_cache_ttl_seconds: int = 600
|
||||
max_proxy_name_length: int = 80
|
||||
default_user_agent: str = "sub-provider/0.2"
|
||||
default_user_agent: str = Field(
|
||||
default="sub-provider/0.2",
|
||||
validation_alias=AliasChoices("UPSTREAM_USER_AGENT", "DEFAULT_USER_AGENT"),
|
||||
)
|
||||
|
||||
sources_file: Path = CONFIG_DIR / "sources.yaml"
|
||||
conf_base_file: Path = CONFIG_DIR / "conf" / "base.conf"
|
||||
@@ -40,6 +44,16 @@ class Settings(BaseSettings):
|
||||
extra="ignore",
|
||||
)
|
||||
|
||||
@field_validator("default_user_agent", mode="before")
|
||||
@classmethod
|
||||
def _coalesce_default_user_agent(cls, value: str) -> str:
|
||||
if isinstance(value, str) and value.strip():
|
||||
return value
|
||||
legacy = os.getenv("DEFAULT_USER_AGENT", "").strip()
|
||||
if legacy:
|
||||
return legacy
|
||||
return "sub-provider/0.2"
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_settings() -> Settings:
|
||||
|
||||
Reference in New Issue
Block a user