A robust RESTful API for managing workout data with user authentication and authorization. Built with Go, PostgreSQL, and following clean architecture principles.
- Complete CRUD Operations for workouts and workout entries
- User Registration & Authentication with JWT-like token system
- Secure Password Hashing using bcrypt
- Multi-user Support with workout ownership validation
- Database Migrations using Goose
- Comprehensive Testing with test database setup
- Clean Architecture with separated concerns (handlers, stores, middleware)
- RESTful API Design with proper HTTP status codes
- Language: Go 1.24+
- Database: PostgreSQL 12.4
- Router: Chi v5
- Password Hashing: bcrypt
- Database Driver: pgx/v4
- Migrations: Goose v3
- Testing: Testify
- Containerization: Docker & Docker Compose
- Go 1.24 or higher
- Docker & Docker Compose
- PostgreSQL (if running locally)
git clone https://github.com/OmarMWarraich/femProject.git
cd femProjectdocker compose up -dThis will start:
- Main database on port
5432 - Test database on port
5433
go mod downloadgo run main.goThe API will be available at http://localhost:8080
All workout endpoints require authentication via Bearer token.
POST /usersRequest Body:
{
"username": "john_doe",
"email": "[email protected]",
"password": "securepassword123",
"bio": "Fitness enthusiast"
}POST /tokens/authenticationRequest Body:
{
"username": "john_doe",
"password": "securepassword123"
}Response:
{
"auth_token": {
"token": "5MNNG4VRC4VUCTJ7PHRH5PLIOML6OXHKJ3OHQNGQ7D3KANGWKMAQ",
"expiry": "2024-01-02T15:04:05Z"
}
}POST /workouts
Authorization: Bearer <token>Request Body:
{
"title": "Morning Strength Training",
"description": "Full body strength training session",
"duration_minutes": 60,
"calories_burned": 500,
"entries": [
{
"exercise_name": "Squats",
"sets": 3,
"reps": 12,
"weight": 100.5,
"notes": "Felt strong today",
"order_index": 1
}
]
}GET /workouts/{id}
Authorization: Bearer <token>PUT /workouts/{id}
Authorization: Bearer <token>DELETE /workouts/{id}
Authorization: Bearer <token>GET /healthid- Primary keyusername- Unique usernameemail- Unique email addresspassword_hash- Bcrypt hashed passwordbio- User biographycreated_at,updated_at- Timestamps
id- Primary keyuser_id- Foreign key to userstitle- Workout titledescription- Workout descriptionduration_minutes- Duration in minutescalories_burned- Estimated calories burnedcreated_at,updated_at- Timestamps
id- Primary keyworkout_id- Foreign key to workoutsexercise_name- Name of exercisesets- Number of setsreps- Number of reps (optional)duration_seconds- Duration in seconds (optional)weight- Weight used (optional)notes- Additional notesorder_index- Order within workout
hash- Primary key (SHA256 hash)user_id- Foreign key to usersexpiry- Token expiration timestampscope- Token scope (authentication)
Run the test suite:
# Start test database
docker compose up test_db -d
# Run tests
go test ./...
# Run specific test
go test ./internal/store -vfemProject/
βββ internal/
β βββ api/ # HTTP handlers
β βββ app/ # Application setup
β βββ middleware/ # Authentication middleware
β βββ routes/ # Route definitions
β βββ store/ # Database layer
β βββ tokens/ # Token generation/validation
β βββ utils/ # Utility functions
βββ migrations/ # Database migrations
βββ docker-compose.yml # Database containers
βββ main.go # Application entry point
βββ README.md
The application uses environment-based configuration. Default values:
- Port: 8080
- Database Host: localhost
- Database Port: 5432 (main), 5433 (test)
- Database Name: postgres
- Database User: postgres
- Database Password: postgres
- Password Hashing: All passwords are hashed using bcrypt with cost factor 12
- Token-based Authentication: Secure token generation using crypto/rand
- Authorization: Users can only access their own workouts
- SQL Injection Protection: Parameterized queries throughout
- Input Validation: Comprehensive request validation
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with Chi Router
- Database migrations powered by Goose
- Testing utilities from Testify
Author: Omar M Warraich
Linkedin: OmarMWarraich
Twitter: OmarMWarraich