feat(backend): setup FastAPI + SQLite + config
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
5
backend/app/config.py
Normal file
5
backend/app/config.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./jardin.db")
|
||||||
|
UPLOAD_DIR = os.getenv("UPLOAD_DIR", "./data/uploads")
|
||||||
|
CORS_ORIGINS = os.getenv("CORS_ORIGINS", "http://localhost:5173").split(",")
|
||||||
13
backend/app/database.py
Normal file
13
backend/app/database.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from sqlmodel import SQLModel, create_engine, Session
|
||||||
|
from app.config import DATABASE_URL
|
||||||
|
|
||||||
|
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
||||||
|
|
||||||
|
|
||||||
|
def get_session():
|
||||||
|
with Session(engine) as session:
|
||||||
|
yield session
|
||||||
|
|
||||||
|
|
||||||
|
def create_db_and_tables():
|
||||||
|
SQLModel.metadata.create_all(engine)
|
||||||
33
backend/app/main.py
Normal file
33
backend/app/main.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import os
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
|
from app.config import CORS_ORIGINS, UPLOAD_DIR
|
||||||
|
from app.database import create_db_and_tables
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
||||||
|
import app.models # noqa — enregistre tous les modèles avant create_all
|
||||||
|
create_db_and_tables()
|
||||||
|
from app.seed import run_seed
|
||||||
|
run_seed()
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(title="Jardin API", lifespan=lifespan)
|
||||||
|
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=CORS_ORIGINS,
|
||||||
|
allow_methods=["*"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/health")
|
||||||
|
def health():
|
||||||
|
return {"status": "ok"}
|
||||||
2
backend/app/seed.py
Normal file
2
backend/app/seed.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
def run_seed():
|
||||||
|
pass
|
||||||
7
backend/requirements.txt
Normal file
7
backend/requirements.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
fastapi==0.115.5
|
||||||
|
uvicorn[standard]==0.32.1
|
||||||
|
sqlmodel==0.0.22
|
||||||
|
python-multipart==0.0.12
|
||||||
|
aiofiles==24.1.0
|
||||||
|
pytest==8.3.3
|
||||||
|
httpx==0.28.0
|
||||||
Reference in New Issue
Block a user