How josetorresmx/habit-tracking-api works
Project type and purpose
• This is a backend-only RESTful web API (“web-api-habits”) built using Python and Flask.
• It exposes endpoints for habit-tracking features (CRUD operations on user habits) with JWT-based authentication and MySQL persistence.
File structure
• Top-level files:
– run.py – application entrypoint (initializes app, loads config, launches WSGI server)
– config.py – configuration (DB URIs, secret keys, JWT settings)
– requirements.txt – Python dependencies (Flask, Flask-JWT-Extended, SQLAlchemy, Flask-MySQL, etc.)
– db/htracker_backup.sql – database schema dump for MySQL
– gen_SK.py – utility to generate/rotate secret keys
• app/ directory: core Flask package
– __init__.py – creates Flask app instance, registers blueprints, sets up extensions
– auth.py – authentication blueprint: login, registration, token management (JWT, Bcrypt)
– models.py – ORM models (SQLAlchemy) for Users, Habits, etc.
– routes.py – resource-oriented API endpoints (habit CRUD, user profile)
README
a. Main components
– Clients (mobile apps or single-page web front-ends)
– API Layer (Flask application running under WSGI/Gunicorn)
– Authentication module (JWT issuance & verification, password hashing via Bcrypt)
– Business logic / Controllers (routes.py)
– Data layer (SQLAlchemy ORM + MySQL database)
– Utility services (secret key generator, database migrations via Flask-Migrate/Alembic)
– External integrations (optional e-mail service, 3rd-party analytics if used)
b. Relationships & interactions
– Clients → HTTP(S) requests → Flask API (routes)
– routes invoke auth module for protected endpoints → JWT validation → proceed
– routes interact with models via SQLAlchemy sessions → MySQL DB
– config.py injects environment-specific settings (DB URL, JWT secret, CORS policies)
– gen_SK.py is a one-off CLI utility (depicted as separate tool)
c. Architectural patterns & design principles
– MVC-like separation: models (ORM), controllers (routes), no server-side views
– RESTful resource design: clear URI patterns, HTTP verbs
– Layered architecture: Request handling → Business logic → Persistence
– Secure by design: JWT for stateless auth, Bcrypt for password hashing, CORS policy
d. Key technologies/frameworks
– Flask & Flask-Blueprints
– Flask-JWT-Extended for tokens
– Flask-Bcrypt for password hashing
– Flask-MySQL / PyMySQL for DB connectivity
– SQLAlchemy ORM
– Alembic/Flask-Migrate for migrations
– Python Decouple / python-dotenv for environment variables
– Gunicorn (or built-in Flask server in dev)