-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.py
More file actions
78 lines (67 loc) · 2.68 KB
/
Copy pathpool.py
File metadata and controls
78 lines (67 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import asyncpg
import structlog
from pydantic import field_validator
from pydantic_settings import BaseSettings
logger = structlog.get_logger(__name__)
class Settings(BaseSettings):
db_host: str = "db"
db_port: int = 5432
db_name: str = "garmin"
db_app_user: str
db_app_password: str
session_secret: str
https_only: bool = True
trimp_lookback_days: int = 7
trimp_forecast_days: int = 7
resend_api_key: str = ""
resend_from_email: str = "onboarding@resend.dev"
app_base_url: str = ""
fernet_key: str
trusted_proxy_cidrs: list[str] = ["127.0.0.1/32"]
sentry_dsn: str = "" # SENTRY_DSN — empty = disabled
db_pool_min: int = 1 # DB_POOL_MIN — asyncpg pool min_size
db_pool_max: int = 5 # DB_POOL_MAX — asyncpg pool max_size
privacy_policy_version: str = (
"v1.0" # PRIVACY_POLICY_VERSION — bumped when policy changes
)
# KI-Wochen-Insights (ADR-0003). Default aus — Generierung liefert dann das
# deterministische Fallback. Ollama laeuft nativ am Host (nicht in Docker).
ollama_enabled: bool = False # OLLAMA_ENABLED
ollama_base_url: str = "http://host.docker.internal:11434" # OLLAMA_BASE_URL
ollama_model: str = "llama3.1:8b" # OLLAMA_MODEL
ollama_timeout_seconds: int = 30 # OLLAMA_TIMEOUT_SECONDS
@field_validator("fernet_key")
@classmethod
def fernet_key_must_not_be_empty(cls, v: str) -> str:
if not v:
raise ValueError(
"FERNET_KEY muss gesetzt sein. Generieren: "
'python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"'
)
return v
@field_validator("session_secret")
@classmethod
def session_secret_min_length(cls, v: str) -> str:
if len(v) < 32:
raise ValueError("SESSION_SECRET must be at least 32 characters")
return v
@property
def db_url(self) -> str:
return f"postgresql://{self.db_app_user}:{self.db_app_password}@{self.db_host}:{self.db_port}/{self.db_name}"
settings = Settings() # type: ignore[call-arg]
_pool: asyncpg.Pool | None = None
async def get_pool() -> asyncpg.Pool:
global _pool
if _pool is None:
try:
_pool = await asyncpg.create_pool(
settings.db_url,
min_size=settings.db_pool_min,
max_size=settings.db_pool_max,
)
except Exception as e:
# db_url embeds the DB password — log only the exception type so the
# DSN never reaches logs or a propagated trace, then re-raise.
logger.error("db.pool.create_failed", reason=type(e).__name__)
raise
return _pool