# CLAUDE.md This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. ## Panoramica TieMeasureFlow by Tielogic - Sistema di gestione task per misurazioni con calibro manuale. Monorepo con **server FastAPI** (backend API, porta 8000) e **client Flask** (frontend tablet, porta 5000), orchestrati con Docker Compose + Nginx reverse proxy + MySQL 8.0. ## Comandi di Sviluppo ### Avvio servizi (Docker) ```bash docker compose up -d # Avvia tutto docker compose logs -f server # Log server docker compose logs -f client # Log client docker compose ps # Stato servizi ``` ### Avvio manuale (senza Docker) ```bash # Server (terminale 1) cd server && uvicorn main:app --reload --host 0.0.0.0 --port 8000 # Client (terminale 2) cd client && flask run --host 0.0.0.0 --port 5000 # TailwindCSS watch (terminale 3) cd client && npx tailwindcss -i static/css/input.css -o static/css/tailwind.css --watch ``` ### Database & Migrations ```bash # alembic.ini è in server/migrations/, serve il flag -c cd server && alembic -c migrations/alembic.ini upgrade head # Applica migrazioni cd server && alembic -c migrations/alembic.ini revision --autogenerate -m "descrizione" # Genera migrazione cd server && alembic -c migrations/alembic.ini downgrade -1 # Rollback ultima docker compose exec server alembic -c migrations/alembic.ini upgrade head # Via Docker ``` Nota: `env.py` sovrascrive la URL di alembic.ini con quella da `.env` (`settings.database_url`). `script_location = %(here)s` usa path relativo. ### Test ```bash # Server (usa SQLite in-memory via aiosqlite, no MySQL richiesto) cd server && pytest # Tutti i test cd server && pytest tests/test_auth.py # Singolo modulo cd server && pytest tests/test_auth.py::test_login_success # Singolo test cd server && pytest --cov # Con copertura # Client cd client && pytest cd client && pytest tests/test_auth.py ``` ### i18n (Traduzioni) ```bash # Estrai stringhe cd client && pybabel extract -F babel.cfg -k _ -o translations/messages.pot . # Aggiorna catalogo cd client && pybabel update -i translations/messages.pot -d translations # Compila .po → .mo cd client && pybabel compile -d translations # oppure cd client && python compile_translations.py ``` ### Setup iniziale Dopo primo avvio, aprire `http://localhost/api/setup` (o `:8000/api/setup` senza Nginx) con la SETUP_PASSWORD dal `.env` per: inizializzare DB, creare admin, caricare dati demo. ## Architettura ### Flusso richieste ``` Browser/Tablet → Nginx (:80/443) → Flask Client (:5000) → APIClient → FastAPI Server (:8000) → MySQL ``` Il client Flask è un frontend server-side che comunica col backend via REST API. Ogni richiesta dal client al server include l'header `X-API-Key` per autenticazione. ### Server (FastAPI) — `server/` - **main.py**: entry point, lifespan async (`@asynccontextmanager`), registra middleware e 10 router. Health: `GET /api/health` - **config.py**: `Settings` (pydantic_settings.BaseSettings), legge da `../.env`. Rate limits: login 5/min, general 100/min - **database.py**: SQLAlchemy 2.0 async engine con `AsyncSession`, pool 10+20 overflow, `pool_recycle=3600`, `expire_on_commit=False` - **routers/**: auth, users, recipes, tasks, measurements, files, settings, statistics, reports, setup - **services/**: recipe_service (versioning copy-on-write), measurement_service (calcolo pass/fail), spc_service (Cp/Cpk/control chart, puro stdlib senza numpy), report_service (WeasyPrint PDF + Kaleido SVG), auth_service (bcrypt + API key) - **middleware/**: api_key.py (auth dependency), rate_limit.py (sliding window 60s per-IP, in-memory dicts), security_headers.py (CSP con `unsafe-eval` per Plotly.js, HSTS solo con SSL), logging.py (audit trail async su DB, esclude /api/health, /docs, /openapi.json) - **models/**: User, Recipe, RecipeVersion, RecipeTask, RecipeSubtask, Measurement, AccessLog, SystemSetting, RecipeVersionAudit - **schemas/**: Pydantic v2 per validazione I/O API - **migrations/**: Alembic con `alembic.ini` e `env.py` nella directory `server/migrations/` - **tests/**: pytest + pytest-asyncio, SQLite in-memory (`sqlite+aiosqlite://`, StaticPool), WeasyPrint mockato via `sys.modules`, rate limit reset tra test ### Client (Flask) — `client/` - **app.py**: factory pattern `create_app()`, CSRF (`WTF_CSRF_TIME_LIMIT=3600`), Babel i18n (`default_locale="it"`) - **blueprints/**: auth (login/logout/session), maker (editor ricette con Fabric.js), measure (esecuzione misurazioni), statistics (dashboard SPC con Plotly.js) - **services/api_client.py**: singleton `APIClient` — wrapper HTTP (get/post/put/delete) con gestione errori normalizzata, timeout 30s, header X-API-Key da session - **templates/**: Jinja2 con `{{ _('string') }}` per i18n. Context processor inietta `current_user`, `current_theme`, `current_language`, `company_logo` in tutti i template - **static/js/**: Alpine.js 3.x (CDN), Fabric.js 5.3.1 (CDN), locales JSON (it.json, en.json), alpine-init.js (theme store) - **translations/**: Flask-Babel, cataloghi .po/.mo per IT/EN. Locale selector: `session["language"]` → Accept-Language → `"it"` - **config.py**: `PERMANENT_SESSION_LIFETIME=28800` (8h), cookie secure in produzione, `BABEL_DEFAULT_TIMEZONE="Europe/Rome"` ## Template Structure (`client/templates/base.html`) Ordine blocchi in `base.html`: ``` → {% block extra_head %} (CSS/meta aggiuntivi) → {% block content %} (contenuto pagina) → {% block extra_js %} (script componenti — PRIMA di Alpine) →
``` `|tojson` dentro `