diff --git a/SKIRRING serveur AI b/SKIRRING serveur AI new file mode 100644 index 0000000..046d8f2 --- /dev/null +++ b/SKIRRING serveur AI @@ -0,0 +1,318 @@ +Pour réviser, corriger et déboguer les erreurs dans le projet, je vais examiner le code fourni pour détecter les erreurs potentielles et proposer des corrections. Nous allons suivre les étapes suivantes pour chaque composant du projet : + +1. **Backend avec FastAPI** : + - `main.py` + - `models.py` + - `schemas.py` + - `crud.py` + - `database.py` + - `Dockerfile` + - `requirements.txt` + - `tests/test_main.py` + +2. **Frontend avec React** : + - `App.js` + - `index.js` + - `services/api.js` + - `Dockerfile` + +3. **Configuration de Docker** : + - `docker-compose.yml` + +4. **Déploiement avec GitHub Actions** : + - `.github/workflows/main.yml` + +### 1. Backend avec FastAPI + +#### `backend/app/main.py` +```python +from fastapi import FastAPI, Depends, HTTPException +from sqlalchemy.orm import Session +from . import models, schemas, crud +from .database import engine, SessionLocal + +models.Base.metadata.create_all(bind=engine) + +app = FastAPI() + +# Dependency +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() + +@app.post("/items/", response_model=schemas.Item) +def create_item(item: schemas.ItemCreate, db: Session = Depends(get_db)): + return crud.create_item(db=db, item=item) + +@app.get("/items/{item_id}", response_model=schemas.Item) +def read_item(item_id: int, db: Session = Depends(get_db)): + db_item = crud.get_item(db, item_id=item_id) + if db_item is None: + raise HTTPException(status_code=404, detail="Item not found") + return db_item +``` + +#### `backend/app/models.py` +```python +from sqlalchemy import Column, Integer, String +from .database import Base + +class Item(Base): + __tablename__ = "items" + + id = Column(Integer, primary_key=True, index=True) + name = Column(String, index=True) + description = Column(String, index=True) +``` + +#### `backend/app/schemas.py` +```python +from pydantic import BaseModel + +class ItemBase(BaseModel): + name: str + description: str + +class ItemCreate(ItemBase): + pass + +class Item(ItemBase): + id: int + + class Config: + orm_mode = True +``` + +#### `backend/app/crud.py` +```python +from sqlalchemy.orm import Session +from . import models, schemas + +def get_item(db: Session, item_id: int): + return db.query(models.Item).filter(models.Item.id == item_id).first() + +def create_item(db: Session, item: schemas.ItemCreate): + db_item = models.Item(**item.dict()) + db.add(db_item) + db.commit() + db.refresh(db_item) + return db_item +``` + +#### `backend/app/database.py` +```python +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" + +engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}) + +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +Base = declarative_base() +``` + +#### `backend/Dockerfile` +```Dockerfile +FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8 + +COPY ./app /app/app + +WORKDIR /app + +RUN pip install --no-cache-dir -r /app/app/requirements.txt + +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] +``` + +#### `backend/requirements.txt` +``` +fastapi +uvicorn +sqlalchemy +pydantic +psycopg2-binary +``` + +#### `backend/tests/test_main.py` +```python +from fastapi.testclient import TestClient +from app.main import app + +client = TestClient(app) + +def test_create_item(): + response = client.post( + "/items/", + json={"name": "Test Item", "description": "This is a test item."}, + ) + assert response.status_code == 200 + assert response.json()["name"] == "Test Item" + assert response.json()["description"] == "This is a test item." + +def test_read_item(): + response = client.post( + "/items/", + json={"name": "Test Item", "description": "This is a test item."}, + ) + item_id = response.json()["id"] + response = client.get(f"/items/{item_id}") + assert response.status_code == 200 + assert response.json()["name"] == "Test Item" + assert response.json()["description"] == "This is a test item." +``` + +### 2. Frontend avec React + +#### `frontend/src/App.js` +```javascript +import React, { useState, useEffect } from 'react'; +import axios from 'axios'; + +function App() { + const [items, setItems] = useState([]); + + useEffect(() => { + axios.get('/api/items') + .then(response => { + setItems(response.data); + }); + }, []); + + return ( +