Documentation Index
Fetch the complete documentation index at: https://mintlify.com/whitiue/logiMathApp/llms.txt
Use this file to discover all available pages before exploring further.
The LogiMath backend is a FastAPI application (src/BackEnd/mainApi.py) that connects to a PostgreSQL database through SQLAlchemy. It defines Pydantic response models for serialisation, configures CORS to allow all origins, and exposes routes for users, quizzes, and questions. uvicorn serves the application on port 8000.
Application initialisation
The app is created and the database schema is auto-generated on startup. The DATABASE_URL is read from the environment via python-dotenv.
app = FastAPI()
load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL")
engine = create_engine(DATABASE_URL)
Base.metadata.create_all(bind=engine)
CORS middleware
All origins, methods, and headers are permitted. This allows the Flet frontend (running as a desktop process) to call the API without restriction.
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
Dependency injection: get_db
A get_db generator function opens a database session, yields it to the route handler, and closes it in the finally block. FastAPI’s Depends injects this session into any route that declares db: Session = Depends(get_db).
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
Pydantic response models
Four response models define the shape of JSON responses, keeping SQLAlchemy model internals out of the API surface.
class UserResponse(BaseModel):
id: int
name: str
email: str
class QuizResponse(BaseModel):
id: int
title: str
subject: str
difficulty: str
class QuestionResponse(BaseModel):
id: int
question_text: str
correct_answer: str
class ScoreResponse(BaseModel):
id: int
user_id: int
quiz_id: int
score: float
Routes
| Method | Path | Description |
|---|
| GET | / | Health check — returns API status |
| HEAD | / | Health check (HEAD variant) |
| GET | /users | Return all users |
| POST | /users | Create a new user |
| GET | /quizzes/{quiz_id}/questions | Return all questions for a quiz |
| POST | /quizzes/{quiz_id}/questions | Add a question to a quiz |
Users
@app.get("/users", response_model=list[UserResponse])
def get_users(db: Session = Depends(get_db)):
"""Obtener todos los usuarios"""
users = db.query(User).all()
return users
@app.post("/users", response_model=UserResponse)
def create_user(name: str, email: str, db: Session = Depends(get_db)):
"""Crear un nuevo usuario"""
user = User(name=name, email=email)
db.add(user)
db.commit()
db.refresh(user)
return user
Questions
@app.get("/quizzes/{quiz_id}/questions", response_model=list[QuestionResponse])
def get_questions(quiz_id: int, db: Session = Depends(get_db)):
"""Obtener preguntas de un quiz"""
questions = db.query(Question).filter(Question.quiz_id == quiz_id).all()
return questions
@app.post("/quizzes/{quiz_id}/questions", response_model=QuestionResponse)
def create_question(
quiz_id: int, question_text: str, correct_answer: str, difficulty: str, db: Session = Depends(get_db)
):
"""Crear una pregunta en un quiz"""
question = Question(
quiz_id=quiz_id,
question_text=question_text,
correct_answer=correct_answer,
difficulty=difficulty,
)
db.add(question)
db.commit()
db.refresh(question)
return question
Health check
@app.api_route("/", methods=["GET", "HEAD"])
def root():
return {"message": "LogiMath API running! 🚀"}
Interactive API documentation
FastAPI generates two documentation UIs automatically. No additional configuration is required.
| UI | URL |
|---|
| Swagger | http://localhost:8000/docs |
| ReDoc | http://localhost:8000/redoc |
Dependencies
fastapi==0.136.1
uvicorn[standard]==0.32.0
sqlalchemy==2.0.49
psycopg2-binary==2.9.12
pydantic==2.13.3
python-dotenv==1.2.2
psycopg2-binary is a self-contained build of the PostgreSQL adapter. It requires no system-level libpq installation inside the container.