A robust RESTful API built with Go for tracking and managing workout sessions. This application allows users to create, read, update, and delete workout records with detailed exercise entries.
This project is a Go-based backend service that provides a complete workout tracking solution with user authentication. The application follows clean architecture principles with clear separation of concerns between handlers, business logic, and data storage.
- User Authentication: Secure registration and token-based authentication
- Workout Management: Create, read, update, and delete workout sessions
- Exercise Tracking: Add detailed exercise entries to workouts including sets, reps, duration, and weight
- Authorization: Users can only modify their own workouts
- Language: Go 1.24.1
- Web Framework: Chi Router
- Database: PostgreSQL
- Authentication: JWT-based token authentication
- Containerization: Docker and Docker Compose for development environment
go-workout-tracker-api/
├── database/ # Database data directories
├── docs/ # Detailed documentation
├── docker-compose.yml # Docker configuration for PostgreSQL
├── go.mod # Go module dependencies
├── go.sum # Go module checksums
├── internal/ # Application internal packages
│ ├── api/ # API handlers
│ │ ├── tokens_handler.go
│ │ ├── user_handler.go
│ │ └── workout_handler.go
│ ├── app/ # Application setup and configuration
│ │ └── app.go
│ ├── middleware/ # HTTP middleware
│ ├── routes/ # Route definitions
│ │ └── routes.go
│ ├── store/ # Data access layer
│ │ ├── database.go
│ │ ├── tokens.go
│ │ ├── user_store.go
│ │ └── workout_store.go
│ ├── tokens/ # Token management
│ └── utils/ # Utility functions
├── main.go # Application entry point
└── migrations/ # Database migrations
└── .env # Environment variables
POST /users
- Register a new userPOST /tokens/authentication
- Create authentication token (login)
GET /workouts/{id}
- Get a specific workout by IDPOST /workouts
- Create a new workoutPUT /workouts/{id}
- Update an existing workoutDELETE /workouts/{id}
- Delete a workout
GET /health
- Check API health status
type Workout struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Title string `json:"title"`
Description string `json:"description"`
DurationMinutes int `json:"duration_minutes"`
CaloriesBurned int `json:"calories_burned"`
Entries []WorkoutEntry `json:"entries"`
}
type WorkoutEntry struct {
ID int `json:"id"`
ExerciseName string `json:"exercise_name"`
Sets int `json:"sets"`
Reps *int `json:"reps"`
DurationSeconds *int `json:"duration_seconds"`
Weight *float64 `json:"weight"`
Notes string `json:"notes"`
OrderIndex int `json:"order_index"`
}
type User struct {
ID int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
PasswordHash password `json:"-"` // Not exposed in JSON
Bio string `json:"bio"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type Token struct {
Plaintext string `json:"token"`
Hash []byte `json:"-"` // Not exposed in JSON
UserID int `json:"-"` // Not exposed in JSON
Expiry time.Time `json:"expiry"`
Scope string `json:"-"` // Not exposed in JSON
}
- Go 1.24.1 or higher
- Docker and Docker Compose (for local development)
-
Clone the repository:
git clone https://github.com/vinayakvispute/go-lang.git cd go-lang
-
Create a
.env
file in the root directory with this env variable and add your own database credentials:DB_HOST=localhost DB_PORT=5432 DB_USER=your_db_user DB_PASSWORD=your_db_password DB_NAME=your_db_name DB_SSLMODE=your_jwt_secret
-
Start the PostgreSQL database:
docker-compose up
-
Run the application:
go run main.go
By default, the server runs on port 8080. You can specify a different port using the
-port
flag:go run main.go -port 3000
The application uses the goose
migration tool to manage database schema changes. Migrations are automatically applied when the application starts.
To run tests:
go test ./...
A separate test database is configured in the Docker Compose file to ensure tests don't interfere with development data.
- Authentication is implemented using JWT tokens
- Passwords are securely hashed before storage
- API endpoints are protected with middleware to ensure proper authorization
- Users can only access and modify their own workout data
- Add support for workout categories and tags
- Implement workout statistics and reporting
- Add social features like sharing workouts
- Create mobile app integration
Detailed documentation for the API, including endpoints, data models, and implementation details, can be found in the docs/ directory.
- Create detailed documentation in docs/ directory covering API endpoints, data models, and implementation details
- Implement query timeouts for database operations
- Add proper database locking mechanisms
- Improve concurrency handling
- Add more comprehensive error handling
pm2 start /root/go-workout-tracker-api/mainFile \
--name workout-api \
--cwd /root/go-workout-tracker-api \
--time # timestamps in logs
pm2 save # remember the process list for reboot
This project is licensed under the MIT License - see the LICENSE file for details.