Spaces:
Sleeping
Sleeping
Fabio Antonini Claude Sonnet 4.6 commited on
Commit ·
9331e8e
1
Parent(s): 76f414d
docs: add backend/README.md with local testing guide
Browse filesDocuments two testing modes (SQLite no-Docker vs full Docker Compose),
Swagger UI workflow, first-admin bootstrap script, env vars reference.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- backend/README.md +168 -0
backend/README.md
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# GraphoLab — Backend (FastAPI)
|
| 2 |
+
|
| 3 |
+
REST API che espone tutti i moduli AI di `core/` attraverso 35 endpoint HTTP.
|
| 4 |
+
Pensato per essere consumato dal frontend React (in sviluppo in `feature/frontend`).
|
| 5 |
+
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
## Testare in locale — senza Docker
|
| 9 |
+
|
| 10 |
+
### Prerequisiti
|
| 11 |
+
|
| 12 |
+
```bash
|
| 13 |
+
# Installa le dipendenze backend (una volta sola)
|
| 14 |
+
venv\Scripts\pip install -r requirements-backend.txt
|
| 15 |
+
```
|
| 16 |
+
|
| 17 |
+
### Opzione A — SQLite (più rapida, zero configurazione)
|
| 18 |
+
|
| 19 |
+
Ideale per sviluppo e test degli endpoint API senza installare nulla di extra.
|
| 20 |
+
|
| 21 |
+
1. Copia il file di esempio e imposta SQLite:
|
| 22 |
+
|
| 23 |
+
```bash
|
| 24 |
+
copy backend\.env.example backend\.env
|
| 25 |
+
```
|
| 26 |
+
|
| 27 |
+
2. Modifica `backend\.env` e sostituisci la riga `DATABASE_URL`:
|
| 28 |
+
|
| 29 |
+
```
|
| 30 |
+
DATABASE_URL=sqlite+aiosqlite:///./grapholab_dev.db
|
| 31 |
+
```
|
| 32 |
+
|
| 33 |
+
> Aggiungi anche `aiosqlite` al venv se non presente:
|
| 34 |
+
> `venv\Scripts\pip install aiosqlite`
|
| 35 |
+
|
| 36 |
+
3. Avvia il backend:
|
| 37 |
+
|
| 38 |
+
```bash
|
| 39 |
+
venv\Scripts\uvicorn backend.main:app --reload --port 8000
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
4. Apri la **Swagger UI** per testare tutti gli endpoint dal browser:
|
| 43 |
+
|
| 44 |
+
```
|
| 45 |
+
http://localhost:8000/docs
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
Con questa configurazione funzionano: autenticazione, gestione utenti, CRUD progetti,
|
| 49 |
+
analisi AI. **Non** funziona l'upload file su MinIO (richiede il servizio MinIO).
|
| 50 |
+
|
| 51 |
+
---
|
| 52 |
+
|
| 53 |
+
### Opzione B — PostgreSQL + MinIO locali (ambiente completo)
|
| 54 |
+
|
| 55 |
+
Se hai PostgreSQL e MinIO già installati in locale (o vuoi avviarli con Docker),
|
| 56 |
+
configura `backend\.env` con i valori reali e avvia normalmente:
|
| 57 |
+
|
| 58 |
+
```bash
|
| 59 |
+
venv\Scripts\uvicorn backend.main:app --reload --port 8000
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
---
|
| 63 |
+
|
| 64 |
+
## Testare in locale — con Docker Compose
|
| 65 |
+
|
| 66 |
+
Avvia solo i servizi necessari (senza jupyter/gradio):
|
| 67 |
+
|
| 68 |
+
```bash
|
| 69 |
+
# Solo infrastruttura (postgres + minio)
|
| 70 |
+
docker compose up postgres minio
|
| 71 |
+
|
| 72 |
+
# Infrastruttura + backend API
|
| 73 |
+
docker compose up postgres minio backend
|
| 74 |
+
|
| 75 |
+
# Stack completo (aggiunge anche ollama + gradio + jupyter)
|
| 76 |
+
docker compose up
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
| Servizio | URL locale |
|
| 80 |
+
|---|---|
|
| 81 |
+
| **Backend API** | http://localhost:8000/docs |
|
| 82 |
+
| **MinIO console** | http://localhost:9001 (user: grapholab / grapholab123) |
|
| 83 |
+
| **PostgreSQL** | localhost:5432 (db: grapholab) |
|
| 84 |
+
| **Ollama** | http://localhost:11434 |
|
| 85 |
+
| Gradio demo | http://localhost:7860 |
|
| 86 |
+
| JupyterLab | http://localhost:8888 (token: grapholab) |
|
| 87 |
+
|
| 88 |
+
---
|
| 89 |
+
|
| 90 |
+
## Workflow di test consigliato
|
| 91 |
+
|
| 92 |
+
1. Avvia il backend (opzione A o B)
|
| 93 |
+
2. Vai su `http://localhost:8000/docs`
|
| 94 |
+
3. **Crea un utente admin** — `POST /users/` (richiede token, usa prima il login di un admin già esistente nel DB, oppure crea manualmente il primo utente con uno script)
|
| 95 |
+
4. **Login** — `POST /auth/login` → ottieni `access_token`
|
| 96 |
+
5. Clicca **Authorize** in Swagger UI e incolla il token
|
| 97 |
+
6. Testa gli endpoint in ordine: crea progetto → carica documento → lancia analisi
|
| 98 |
+
|
| 99 |
+
### Script per creare il primo utente admin
|
| 100 |
+
|
| 101 |
+
```python
|
| 102 |
+
# Esegui una volta per inizializzare il DB con un admin
|
| 103 |
+
# python -m backend.scripts.create_admin (da aggiungere)
|
| 104 |
+
|
| 105 |
+
# Oppure direttamente:
|
| 106 |
+
import asyncio, sys
|
| 107 |
+
sys.path.insert(0, '.')
|
| 108 |
+
from backend.database import AsyncSessionLocal, init_db
|
| 109 |
+
from backend.models.user import User, Role
|
| 110 |
+
from backend.auth.password import hash_password
|
| 111 |
+
|
| 112 |
+
async def main():
|
| 113 |
+
await init_db()
|
| 114 |
+
async with AsyncSessionLocal() as db:
|
| 115 |
+
admin = User(
|
| 116 |
+
email="admin@grapholab.local",
|
| 117 |
+
full_name="Administrator",
|
| 118 |
+
hashed_password=hash_password("changeme"),
|
| 119 |
+
role=Role.admin,
|
| 120 |
+
)
|
| 121 |
+
db.add(admin)
|
| 122 |
+
await db.commit()
|
| 123 |
+
print("Admin creato: admin@grapholab.local / changeme")
|
| 124 |
+
|
| 125 |
+
asyncio.run(main())
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
+
---
|
| 129 |
+
|
| 130 |
+
## Struttura
|
| 131 |
+
|
| 132 |
+
```
|
| 133 |
+
backend/
|
| 134 |
+
├── main.py # FastAPI app, CORS, lifespan, health check
|
| 135 |
+
├── config.py # Settings (env vars / .env file)
|
| 136 |
+
├── database.py # SQLAlchemy async engine + get_db()
|
| 137 |
+
├── auth/
|
| 138 |
+
│ ├── jwt.py # Creazione e verifica token JWT
|
| 139 |
+
│ ├── password.py # bcrypt hash/verify
|
| 140 |
+
│ └── dependencies.py # get_current_user(), require_role()
|
| 141 |
+
├── models/
|
| 142 |
+
│ ├── user.py # User, Organization, Role
|
| 143 |
+
│ └── project.py # Project, Document, Analysis
|
| 144 |
+
├── routers/
|
| 145 |
+
│ ├── auth.py # POST /auth/login|refresh|logout
|
| 146 |
+
│ ├── users.py # CRUD /users/ + /users/me
|
| 147 |
+
│ ├── projects.py # CRUD /projects/ + upload documenti
|
| 148 |
+
│ ├── analysis.py # POST /analysis/{htr,sig,ner,writer,grapho,pipeline,dating}
|
| 149 |
+
│ └── rag.py # /rag/chat (SSE) + /rag/docs
|
| 150 |
+
└── storage/
|
| 151 |
+
└── minio_client.py # Async wrapper MinIO SDK
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
Tutto il processing AI è in `core/` — questo layer è solo HTTP + persistenza.
|
| 155 |
+
|
| 156 |
+
---
|
| 157 |
+
|
| 158 |
+
## Variabili d'ambiente
|
| 159 |
+
|
| 160 |
+
Copia `backend/.env.example` in `backend/.env` e personalizza:
|
| 161 |
+
|
| 162 |
+
| Variabile | Default | Note |
|
| 163 |
+
|---|---|---|
|
| 164 |
+
| `SECRET_KEY` | `change_me_...` | **Cambia in produzione** (`openssl rand -hex 32`) |
|
| 165 |
+
| `DATABASE_URL` | PostgreSQL asyncpg | Sostituisci con `sqlite+aiosqlite:///./dev.db` per test rapidi |
|
| 166 |
+
| `MINIO_ENDPOINT` | `localhost:9000` | |
|
| 167 |
+
| `OLLAMA_HOST` | `http://localhost:11434` | |
|
| 168 |
+
| `DEBUG` | `false` | `true` per query SQL nei log |
|