2026-08-03 13:47:24 +08:00
|
|
|
"""Environment-backed application configuration."""
|
|
|
|
|
|
2026-08-05 17:57:32 +08:00
|
|
|
from decimal import Decimal
|
2026-08-03 13:47:24 +08:00
|
|
|
from functools import lru_cache
|
2026-08-05 17:57:32 +08:00
|
|
|
from pathlib import Path
|
2026-08-03 13:47:24 +08:00
|
|
|
from typing import Literal
|
|
|
|
|
|
2026-08-11 11:20:13 +08:00
|
|
|
from pydantic import Field
|
2026-08-03 13:47:24 +08:00
|
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
|
|
|
"""Validated process configuration at the application boundary."""
|
|
|
|
|
|
|
|
|
|
app_name: str = "Zhixing Server"
|
|
|
|
|
app_env: Literal["development", "test", "production"] = "development"
|
|
|
|
|
log_level: str = "INFO"
|
2026-08-05 17:57:32 +08:00
|
|
|
database_url: str = "postgresql://zhixing:zhixing@localhost:5432/zhixing"
|
|
|
|
|
tushare_token: str = ""
|
|
|
|
|
market_data_csv_root: Path = Path("./data/market-data")
|
|
|
|
|
market_data_coverage_threshold: Decimal = Decimal("0.99")
|
2026-08-11 11:20:13 +08:00
|
|
|
market_data_max_workers: int = Field(default=8, ge=1)
|
2026-08-05 17:57:32 +08:00
|
|
|
market_data_request_interval_seconds: float = 0.2
|
|
|
|
|
market_data_max_retries: int = 3
|
|
|
|
|
market_data_retry_backoff_seconds: float = 1.0
|
|
|
|
|
market_data_advisory_lock_key: int = 7_380_521
|
2026-08-12 09:45:16 +08:00
|
|
|
selection_max_workers: int = Field(default=4, ge=1)
|
|
|
|
|
selection_batch_size: int = Field(default=200, ge=1)
|
2026-08-03 13:47:24 +08:00
|
|
|
|
|
|
|
|
model_config = SettingsConfigDict(
|
|
|
|
|
env_file=".env",
|
|
|
|
|
env_prefix="ZHIXING_",
|
|
|
|
|
extra="ignore",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-08-05 22:45:57 +08:00
|
|
|
def sqlalchemy_database_url(database_url: str) -> str:
|
|
|
|
|
"""Add SQLAlchemy's Psycopg 3 driver to a PostgreSQL URL.
|
|
|
|
|
|
|
|
|
|
``Settings.database_url`` remains a plain ``postgresql://`` URL because
|
|
|
|
|
Psycopg 3 accepts that form directly. Alembic and SQLAlchemy need the
|
|
|
|
|
explicit ``postgresql+psycopg://`` dialect, while the rest of the URL
|
|
|
|
|
(including query parameters such as ``sslmode``) must remain unchanged.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
database_url: PostgreSQL connection URL from the process environment.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The URL with SQLAlchemy's Psycopg 3 dialect prefix.
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
ValueError: If the URL does not use a supported PostgreSQL scheme.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
if database_url.startswith("postgresql+psycopg://"):
|
|
|
|
|
return database_url
|
|
|
|
|
if database_url.startswith("postgresql://"):
|
|
|
|
|
return "postgresql+psycopg://" + database_url.removeprefix("postgresql://")
|
|
|
|
|
raise ValueError("database_url must use postgresql:// or postgresql+psycopg://")
|
|
|
|
|
|
|
|
|
|
|
2026-08-03 13:47:24 +08:00
|
|
|
@lru_cache
|
|
|
|
|
def get_settings() -> Settings:
|
|
|
|
|
"""Return one immutable-by-convention configuration object per process.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Settings populated from defaults, `.env`, and process environment variables.
|
|
|
|
|
"""
|
|
|
|
|
return Settings()
|