Structura repo conform plan.md sect. 4, booteaza cu /healthz verde:
- app/main.py: FastAPI (lifespan init_db), /healthz (worker viu + last login + queue), /metrics
- app/api/v1: POST /v1/prezentari (enqueue + dedup idempotency UNIQUE), GET prezentari/{id}, nomenclator, mapari
- app/rar_client.py: client RAR real (login/JWT, nomenclator, postPrezentare, getFinalizate) cu User-Agent obligatoriu (fix WAF 403)
- app/worker: proces separat, claim atomic BEGIN IMMEDIATE, heartbeat, login+send (send dezactivat by default)
- app/web: dashboard Jinja2+HTMX (coada, banner alerta blocate, worker viu/mort, stari empty)
- app/db.py + schema.sql: SQLite WAL, tabele accounts/api_keys/operations_mapping/nomenclator_rar/submissions/worker_heartbeat
- app/idempotency.py + payload.py: hash continut canonic + builder payload (status FINALIZATA, fara tipPrestatie)
- Dockerfile + docker-compose.yml (api+worker, volum SQLite persistent, restart:always)
- tools/import_dbf.py: stub T5
Verificat live: login prin rar_client OK (token 259), nomenclator 18 coduri, worker heartbeat -> /healthz worker_alive=True.
Ramas: T3 validare Pydantic, T4 snapshot payload, T2 reconciliere/retry worker, T5 import DBF, auth API-key, middleware redactare creds, criptare PII.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
76 lines
2.7 KiB
Python
76 lines
2.7 KiB
Python
"""Configurare gateway. Env vars (prefix AUTOPASS_) + valori implicite.
|
|
|
|
NU stocheaza parole RAR. Credentialele RAR vin per-cerere de la ROAAUTO
|
|
(vezi plan.md sect. 5). Helper-ul `load_test_credentials` citeste blocul
|
|
<test> din settings.xml DOAR pentru dev local / probe pe mediul de test.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import xml.etree.ElementTree as ET
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
model_config = SettingsConfigDict(env_prefix="AUTOPASS_", env_file=".env", extra="ignore")
|
|
|
|
# --- Bază de date ---
|
|
db_path: Path = ROOT / "data" / "autopass.db"
|
|
|
|
# --- RAR ---
|
|
rar_env: str = "test" # "test" | "prod"
|
|
rar_base_url_test: str = "https://apps.rarom.ro/test-rar-autopass"
|
|
rar_base_url_prod: str = "https://apps.rarom.ro/rar-autopass"
|
|
|
|
# WAF-ul RAR da 403 fara User-Agent de browser (confirmat live, vezi
|
|
# docs/api-rar-contract.md). Toate apelurile httpx il trimit.
|
|
http_user_agent: str = "Mozilla/5.0"
|
|
http_timeout_s: float = 30.0
|
|
|
|
# --- Worker ---
|
|
worker_poll_interval_s: float = 5.0
|
|
worker_heartbeat_stale_s: int = 30 # /healthz considera worker-ul mort peste atat
|
|
# In schelet send-ul e DEZACTIVAT (nu trimite la RAR). Activeaza-l explicit
|
|
# pentru proba end-to-end. Reconcilierea/retry-ul complet = T2.
|
|
worker_send_enabled: bool = False
|
|
# Dev: foloseste creds <test> din settings.xml pt login worker. In productie
|
|
# creds vin per-cerere de la ROAAUTO (T2) — lasa False.
|
|
worker_use_test_creds: bool = False
|
|
|
|
@property
|
|
def rar_base_url(self) -> str:
|
|
return self.rar_base_url_prod if self.rar_env == "prod" else self.rar_base_url_test
|
|
|
|
|
|
@lru_cache
|
|
def get_settings() -> Settings:
|
|
return Settings()
|
|
|
|
|
|
def load_test_credentials(settings_xml: Path | None = None) -> dict | None:
|
|
"""Citeste credentialele <test> din settings.xml (dev local / probe test).
|
|
|
|
Intoarce {"email", "password"} sau None daca fisierul lipseste / e template.
|
|
NU se foloseste in productie — acolo creds vin per-cerere de la ROAAUTO.
|
|
"""
|
|
path = settings_xml or (ROOT / "settings.xml")
|
|
if not path.exists():
|
|
return None
|
|
try:
|
|
root = ET.parse(path).getroot()
|
|
node = root.find("./test/credentials")
|
|
if node is None:
|
|
return None
|
|
email = (node.findtext("email") or "").strip()
|
|
password = (node.findtext("password") or "").strip()
|
|
if not email or not password or email.startswith("EMAIL_"):
|
|
return None
|
|
return {"email": email, "password": password}
|
|
except ET.ParseError:
|
|
return None
|