Skip to main content

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.
mainApi.py
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.
mainApi.py
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).
mainApi.py
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.
mainApi.py
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

MethodPathDescription
GET/Health check — returns API status
HEAD/Health check (HEAD variant)
GET/usersReturn all users
POST/usersCreate a new user
GET/quizzes/{quiz_id}/questionsReturn all questions for a quiz
POST/quizzes/{quiz_id}/questionsAdd a question to a quiz

Users

mainApi.py
@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

mainApi.py
@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

mainApi.py
@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.
UIURL
Swaggerhttp://localhost:8000/docs
ReDochttp://localhost:8000/redoc

Dependencies

requirements.txt
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.

Build docs developers (and LLMs) love