A comprehensive REST API for managing gym operations, including user authentication, membership packages, and member management.
- Framework: FastAPI 0.115+
- Server: Uvicorn
- Database: PostgreSQL 16 with AsyncPG
- Authentication: JWT with python-jose and Passlib
- Configuration: Pydantic Settings
- Python: 3.10 or higher
- PostgreSQL: 16.x
- Docker & Docker Compose: (Optional, for containerized setup)
# Navigate to project directory
cd python-engineer26_gym-management
# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Windows:
.\venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activatepip install -r requirements.txtKey packages installed:
fastapi>=0.115.0- Web frameworkuvicorn[standard]>=0.30.0- ASGI serversqlalchemy>=2.0.0- ORMasyncpg>=0.30.0- PostgreSQL async driverpsycopg[binary]>=3.1.0- PostgreSQL sync driver for Alembic migrationsalembic>=1.13.0- Database migrationspydantic>=2.7.0- Data validationpython-jose[cryptography]>=3.3.0- JWT tokenspasslib[argon2]>=1.7.4- Password hashing
Create a .env file in the project root:
# Application
APP_NAME=Gym Management API
APP_VERSION=0.1.0
DEBUG=true
API_V1_PREFIX=/api/v1
# Database
DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:15433/gymdb
# JWT
JWT_SECRET_KEY=your-secret-key-change-in-production
JWT_ALGORITHM=HS256
JWT_ACCESS_TOKEN_EXPIRE_MINUTES=30
JWT_REFRESH_TOKEN_EXPIRE_DAYS=7Note: Update JWT_SECRET_KEY and database credentials for production.
# Start PostgreSQL container
docker-compose up -d
# Verify the container is running
docker-compose psThe database will be available at localhost:15433 with:
- User: postgres
- Password: postgres
- Database: gymdb
If you have PostgreSQL installed locally:
# Create database
createdb -U postgres -p 5432 gymdb
# Update DATABASE_URL in .env if using different port/credentialsAlembic has been configured to work with async SQLAlchemy and PostgreSQL.
Configured Models:
User- Gym staff/admin usersPackage- Membership packagesMember- Gym members
# Apply all pending migrations
python -m alembic upgrade head
# Check current database version
python -m alembic current
# View migration history
python -m alembic historyAfter modifying models in app/models/:
# Generate migration automatically
python -m alembic revision --autogenerate -m "Description of changes"
# Review the generated migration file in alembic/versions/
# Then apply it
python -m alembic upgrade head# Downgrade to previous migration
python -m alembic downgrade -1
# Downgrade to specific revision
python -m alembic downgrade <revision_id># Make sure virtual environment is activated
# and database is running
python -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000The API will be available at: http://localhost:8000
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
# Example: Get API info
curl http://localhost:8000/api/v1/health
# Check Swagger docs for all available endpoints# Windows (recommended, use project venv)
.\venv\Scripts\python -m pytest -q
# macOS/Linux
./venv/bin/python -m pytest -q# Unit + E2E tests for package module
.\venv\Scripts\python -m pytest app/test/package -q# Check typing for package tests
.\venv\Scripts\python -m mypy app/test/package- Prefer running test commands through the virtual environment Python to avoid PATH issues.
- If TestClient reports missing dependency, install/update from requirements:
pip install -r requirements.txtpython -m alembic revision --autogenerate -m "Add new table"# WARNING: This deletes all data
python -m alembic downgrade base
python -m alembic upgrade head# Press Ctrl+C in the terminaldocker-compose downIf port 8000 is already in use:
python -m uvicorn app.main:app --reload --port 8001- Ensure PostgreSQL container is running:
docker-compose ps - Check DATABASE_URL in
.envmatches your setup - Verify credentials are correct
- Check Alembic configuration:
alembic current - Review migration files in
alembic/versions/ - Ensure database is running before running migrations
This project is part of Python Engineer training program.