# Fake Backend Tool > **Strumento di sviluppo per simulare risposte API AI** > > Scopo: Permettere lo sviluppo e il testing del frontend senza dipendere dal backend reale o dalle API di OpenRouter. --- ## πŸ“‹ Descrizione Il `fake-backend` Γ¨ un server Node.js/Express che simula le risposte dell'API di LogWhispererAI. È progettato per: - **Sviluppo UI**: Testare l'interfaccia utente con risposte realistiche - **Demo offline**: Mostrare il prodotto senza connessione internet - **Testing**: Validare il flusso frontend senza costi API - **Onboarding**: Permettere ai nuovi sviluppatori di lavorare subito --- ## πŸš€ Endpoint API ### POST /api/analyze Analizza un log e restituisce una risposta AI simulata. **Request:** ```http POST http://localhost:3000/api/analyze Content-Type: application/json { "log": "FATAL: database system is out of memory" } ``` **Response (dopo 1.5s delay):** ```json { "success": true, "analysis": { "title": "PostgreSQL Out of Memory", "description": "Il database ha esaurito la memoria disponibile...", "command": "ps aux | grep postgres | head -5 && free -h", "isSafe": true, "note": "Verifica processi Postgres e memoria disponibile." }, "timestamp": "2026-04-03T10:30:00.000Z" } ``` **Errori gestiti:** - `400 Bad Request`: Payload vuoto o malformato - `500 Internal Error`: Errore server generico --- ## πŸ› οΈ Setup ### Prerequisiti - Node.js 18+ - npm 9+ ### Installazione ```bash # Entra nella directory cd tools/fake-backend # Installa dipendenze npm install # Avvia il server node server.js ``` Il server sarΓ  disponibile su `http://localhost:3000` --- ## 🐳 Docker Per avviare il fake backend con Docker: ```bash # Dalla root del progetto docker compose up fake-backend -d ``` Il servizio sarΓ  esposto sulla porta 3000. --- ## πŸ”§ Configurazione Variabili ambiente (opzionali): ```bash PORT=3000 # Porta server (default: 3000) DELAY_MS=1500 # Delay simulazione AI (default: 1500ms) ``` --- ## πŸ“ Struttura ``` tools/fake-backend/ β”œβ”€β”€ server.js # Server Express principale β”œβ”€β”€ package.json # Dipendenze npm β”œβ”€β”€ Dockerfile # Containerizzazione └── README.md # Questo file ``` --- ## πŸ”’ Sicurezza ⚠️ **ATTENZIONE**: Questo Γ¨ uno strumento di sviluppo. NON usarlo in produzione: - Nessuna autenticazione implementata - Risposte statiche predefinite - Nessuna validazione input avanzata - CORS abilitato per tutte le origini --- ## πŸ§ͺ Test Manuale ```bash # Test endpoint analyze curl -X POST http://localhost:3000/api/analyze \ -H "Content-Type: application/json" \ -d '{"log": "Error: Connection refused"}' ``` --- ## πŸ“ Note per Sviluppatori ### Aggiungere nuove risposte mock Modifica l'oggetto `MOCK_RESPONSES` in `server.js`: ```javascript const MOCK_RESPONSES = { 'errore-specifico': { title: 'Titolo Problema', description: 'Descrizione...', command: 'comando-da-eseguire', isSafe: true, note: 'Nota aggiuntiva' } }; ``` ### Pattern matching Il server cerca keyword nel log e restituisce la risposta appropriata: - "memory" / "oom" β†’ PostgreSQL OOM - "connection refused" / "502" β†’ Nginx/Connection error - Default β†’ Risposta generica --- ## 🀝 Integrazione Frontend Per usare il fake backend dal frontend React: ```typescript const analyzeLog = async (log: string) => { const response = await fetch('http://localhost:3000/api/analyze', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ log }) }); return response.json(); }; ``` --- ## πŸ”„ Passaggio a Backend Reale Quando il backend reale Γ¨ pronto: 1. Aggiorna l'URL in `InteractiveDemo.tsx` 2. Aggiungi autenticazione (JWT/API Key) 3. Rimuovi CORS permissivo 4. Implementa rate limiting --- *Strumento creato seguendo il principio "Safety First" del Metodo Sacchi*