Skip to content

Repository files navigation

🏋️‍♂️ FitLog

A backend application built with FastAPI, PostgreSQL, SQLAlchemy, and Alembic. It provides user authentication, exercise and workout management, and personal record tracking.


🚀 Features

  • User Authentication
    • Registration and login using hashed passwords (bcrypt)
    • JWT-based authentication and authorization
  • Exercise Management
    • CRUD endpoints to create, read, update, and delete exercises
    • Exercises are linked to individual users
  • Workout Management
    • Create and manage workouts
    • Link workouts with multiple exercises (many-to-many relationship)
  • Personal Record (PR) Endpoint
    • Query that joins exercises, workouts, and workout-exercises tables to display (exercise_name, weight) for a specific user.

🧱 Tech Stack

  • FastAPI – Web framework for building APIs
  • Uvicorn – ASGI server to run FastAPI
  • PostgreSQL (Docker) – Database
  • SQLAlchemy – ORM for database modeling
  • Alembic – Database migrations
  • python-jose – JWT encoding and decoding
  • bcrypt – Password hashing
  • python-dotenv – Environment variable management

⚙️ Environment Setup

Prerequisites

You need Python 3.8+ and a running PostgreSQL instance.

Installation

  1. Create and Activate a Virtual Environment:

    python -m venv venv
    # For Linux/Macsource venv/bin/activate
    # For Windows
    venv\Scripts\activate
  2. Install Dependencies:

    pip install fastapi uvicorn[standard] sqlalchemy alembic psycopg2-binary python-dotenv python-jose[cryptography] bcrypt

    (You should also create a requirements.txt file by running pip freeze > requirements.txt)

Environment Variables

Create a .env file in the project root (and ensure it is added to .gitignore):


🗄️ Project Structure

fitness-tracker-api/
│
├── app/
│ ├── main.py (or app.py)
│ ├── config.py # Load environment variables
│ ├── database.py # DB engine, session, and dependency
│ ├── models/ # SQLAlchemy ORM models
│ │ ├── base.py
│ │ ├── user.py
│ │ ├── exercise.py
│ │ ├── workout.py
│ │ └── workoutexercise.py
│ ├── schemas/ # Pydantic schemas for request/response validation
│ ├── routes/ # FastAPI routers (API endpoints)
│ └── utils/ # Helper functions (e.g., password hashing, JWT)
│
├── alembic/ # Alembic migration environment
│ ├── versions/
│ └── env.py
│
├── .env
├── .gitignore
├── alembic.ini
├── requirements.txt
└── README.md

🔧 Database Connection (app/database.py)

The database.py module will handle the SQLAlchemy setup:

  • Import DATABASE_URL from app/config.py.
  • Create a SQLAlchemy Engine (connection factory).
  • Create a SessionLocal (session factory).
  • Define a get_db() dependency for FastAPI routes to manage DB sessions safely.

🧩 Migrations (Alembic)

  1. Initialize Alembic:

    alembic init alembic
  2. Configuration:

    • In alembic.ini, leave sqlalchemy.url = blank.
    • In alembic/env.py, update to load DATABASE_URL from .env, import Base and models, and set:
      target_metadata=Base.metadata
  3. Generate Migration Script:

    alembic revision --autogenerate -m "Create users table"
  4. Apply Migrations:

    alembic upgrade head

🧠 Authentication

Authentication uses secure practices for password management and token generation:

  • Password Hashing:bcrypt.hashpw()
  • Password Verification:bcrypt.checkpw()
  • JWT Encoding/Decoding: via python-jose

JWT Example

defjwt_encode(payload: dict):
# Uses SECRET_KEY and ALGORITHM from .envreturnjwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM)
defjwt_decode(token: str):
returnjwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])

🔒 Routes Overview

EndpointMethodDescriptionAuth Required
/registerPOSTRegister a new user
/loginPOSTLogin user and get JWT token
/exercises/CRUDManage exercises (Create, Read, Update, Delete)
/workouts/CRUDManage workouts (Create, Read, Update, Delete)
/pr/GETGet user’s personal records

▶️ Running the Server

Start the FastAPI application:

uvicorn app.main:app --reload

API will be available at:

  • Base URL:http://localhost:8000
  • Interactive Docs (Swagger UI):http://localhost:8000/docs

🧾 Notes

  • Alembic: Use Alembic only when modifying schema (not for runtime CRUD changes).
  • DB Sessions: Always use get_db() dependency to safely handle DB sessions.
  • Security:.env should never be committed to version control.

About

FitLog is a secure, high‑performance workout tracking backend built with FastAPI and PostgreSQL/SQLAlchemy. It features JWT authentication, bcrypt hashing, and Dockerized AWS deployment, sustaining <60 ms latency at scale with seamless schema migrations via Alembic.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages