Go Gin boilerplate with JWT authentication, environment-based configuration, structured responses, pagination, search, and sorting.
- ✅ Multiple environment support (
.envfiles) - 🔐 JWT Authentication with Refresh Token
- 📄 Structured JSON response with metadata
- 🔎 Pagination, Search, and Sorting
- 🧪 Scalable modular project structure
.
├── config/ # Application configuration
├── controllers/ # HTTP request handlers
├── database/ # DB initialization & connection
├── helpers/ # Utility functions
├── middlewares/ # HTTP middlewares
├── models/ # GORM models
├── presenters/ # API response formatting
├── requests/ # Request validation
├── routes/ # Route definitions
├── structs/ # Common shared structures
├── .env.example # Environment variables template
├── .env.development # Development environment config
├── .env.staging # Staging environment config
├── .env.production # Production environment config
├── go.mod # Go module dependencies
├── go.sum # Go module checksums
└── main.go # Application entry point
git clone https://github.com/faisallbhr/gin-boilerplate.git
cd gin-boilerplate
go mod download
cp .env.example .env.developmentEdit .env.development as needed.
# Default (.env.development)
go run main.go
# Or use other environments:
APP_ENV=staging go run main.go
APP_ENV=production go run main.go| Method | Endpoint | Description |
|---|---|---|
| POST | /api/auth/register |
Register a new user |
| POST | /api/auth/login |
Login and get tokens |
| POST | /api/auth/refresh |
Refresh JWT token |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/users |
List users (search, sort, paginate) |
| GET | /api/users/me |
Get current user profile |
| GET | /api/users/:id |
Get user by ID |
| PATCH | /api/users/:id |
Update user profile |
| PATCH | /api/users/:id/password |
Change user password |
| DELETE | /api/users/:id |
Delete user |
{
"success": true,
"message": "Success message",
"data": <object|array|null>,
"meta": {
"search": "<query>",
"sort": {
"by": "<field>",
"order": "asc|desc"
},
"pagination": {
"page": 1,
"limit": 10,
"total": 100,
"total_pages": 10
}
}
}datais flexible: object, array, ornull.metais optional and mainly used in list endpoints.
{
"success": false,
"message": "Something went wrong"
}{
"success": false,
"message": "Validation failed",
"errors": {
"field": "Field is required"
}
}errorscontains field-specific feedback (optional).
You can pass the following query parameters to list endpoints (e.g., /api/users):
| Parameter | Description | Default | Example |
|---|---|---|---|
search |
Filter data by keyword | "" |
john |
page |
Page number | 1 |
2 |
limit |
Items per page | 10 |
5 |
sort_by |
Field to sort by | id |
name, email |
order |
Sort direction | desc |
asc, desc |
If you're cloning this for a new project, update your go.mod:
module github.com/your-username/your-projectThen replace import paths:
# macOS
grep -rl 'github.com/faisallbhr/gin-boilerplate' . | xargs sed -i '' 's|github.com/faisallbhr/gin-boilerplate|github.com/your-username/your-project|g'
# Linux
grep -rl 'github.com/faisallbhr/gin-boilerplate' . | xargs sed -i 's|github.com/faisallbhr/gin-boilerplate|github.com/your-username/your-project|g'Lastly, run:
go mod tidy- Fork this repository
- Create a feature branch (
git checkout -b feature/your-feature) - Commit your changes (
git commit -m 'Add new feature') - Push to your branch (
git push origin feature/your-feature) - Create a Pull Request
This project is licensed under the MIT License. See the LICENSE file for more information.
Happy Coding! 🚀