How tian1003/mr_backend works
Project type and purpose
• This is a Flask-based RESTful backend (“MR_backend”) for an educational/testing system. It exposes HTTP JSON APIs to manage classes, questions, practices/tests, students, etc., persists data in MySQL, and provides migration and seeding capabilities.
File structure
• Entry point: wsgi.py
• Configuration: .env.example, alembic.ini
• App package (app/):
• blueprints/ (Flask controllers – one per resource)
• validation/ (request schema definitions)
• services/ (business logic)
• daos/ (database access layer)
• models/ (SQLAlchemy ORM classes)
• dtos/ (data-transfer objects for response shaping)
• utilities/, bcrypt.py, login_manager.py, generators/
• database.py (SQLAlchemy setup)
• seeds/ (data seeders)
• Migration: Alembic (migration/ folder)
• Init scripts: initial_db.py, JSON fixtures under init_data/
• Tests: Unit & functional under tests/
• Other scripts: excel_reader.py, pdf_reader.py
Key insights from README
• Python 3.8+, MySQL 8
• initial_db.py runs Alembic migrations and seeds
• Flask-run with auto-reload via wsgi.py
• Optional API docs in a separate folder (not shown)
Main components & interactions
a. Clients (Browser, mobile app, Postman)
b. Entry layer (Flask/Werkzeug WSGI in wsgi.py)
c. Controller layer (Blueprints: route definitions)
d. Validation layer (Marshmallow schemas)
e. Service layer (business logic, orchestration)
f. DAO layer (CRUD operations via SQLAlchemy session)
g. Model layer (ORM classes mapping to MySQL tables)
i. Database (MySQL)
j. Migration tool (Alembic) & Seeder (Flask-seed)
k. Utilities (hashing, data generators, file readers)
Flow example:
HTTP Request → WSGI → Blueprint.route
→ validate input against Schema
→ Service.method(...)
→ DAO.query/insert/update
→ MySQL
← ORM model
← DTO.format(model)
← JSON Response
Architectural patterns & principles
• Layered (presentation → validation → service → DAO → persistence)
• Separation of concerns (controllers don’t know SQL, services don’t handle HTTP)
• Dependency injection via central app/__init__.py and database.py
• Migrations + seeders automate schema evolution and test data
• Test harness isolated under tests/
Elements
• Controllers (e.g., blue rectangles)
• Services (green)
• DAOs (orange)
• Models/DB (grey)
• Utilities/Generators (light)