Gin Boilerplate
A production-ready boilerplate for building REST APIs with Go and Gin framework. This boilerplate includes essential features like database integration, API documentation, logging, error handling, and containerization support.
π Features
- Gin Framework for routing and middleware
- PostgreSQL integration with migration support
- Swagger API documentation
- API monitoring with APIToolkit
- Custom error handling and logging
- CORS configuration
- Docker and Docker Compose support
- OTP management system
- Static file serving
- Environment configuration
- Hot reload during development
- Code security scanning with gosec
- Event streaming with Apache Kafka
- Transactional message processing
- Consumer group management
- Event broadcasting system
π Prerequisites
- Go 1.x
- Docker and Docker Compose
- PostgreSQL (if running locally)
- Make
π οΈ Installation
- Clone the repository
git clone https://github.com/CeoFred/gin-boilerplate.git
cd gin-boilerplate
- Copy the example environment file
cp .env.example .env
- Install dependencies
make requirements
π§ Configuration
Update the .env
file with your configuration:
PORT=8080
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=yourpassword
DB_NAME=yourdatabase
API_TOOLKIT_KEY=your-api-toolkit-key
π Running the Application
Local Development
# Run the application with hot reload
make run-local
Using Docker
# Build the Docker image
make build
# Start all services using Docker Compose
make service-start
π API Documentation
Swagger documentation is available at:
http://localhost:8080/swagger/index.html
To regenerate Swagger documentation:
make docs-generate
π οΈ Available Make Commands
make run-local
- Run the application locally with hot reload
make docs-generate
- Generate Swagger documentation
make requirements
- Install/update dependencies
make clean-packages
- Clean Go module cache
make build
- Build Docker image
make start-postgres
- Start PostgreSQL container
make stop-postgres
- Stop PostgreSQL container
make start
- Start application with Docker
make build-no-cache
- Build Docker image without cache
make service-stop
- Stop all Docker Compose services
make service-start
- Start all Docker Compose services
π Project Structure
.
βββ constants/ # Application constants and configuration
βββ database/ # Database connection and migrations
βββ docs/ # Swagger documentation
βββ internal/
β βββ bootstrap/ # Application bootstrapping
β βββ helpers/ # Helper functions
β βββ otp/ # OTP management
β βββ repository/ # Repository management
β βββ routes/ # API routes
β βββ streaming/ # Kafka streaming implementation
β βββ consumer.go # Kafka consumer implementation
β βββ producer.go # Kafka producer implementation
β βββ events.go # Event type definitions
βββ static/ # Static files
βββ templates/ # Template files
βββ main.go # Application entry point
βββ Dockerfile # Docker configuration
βββ docker-compose.yml # Docker Compose configuration
βββ Makefile # Build and development commands
π Security
The project includes security measures:
- Custom recovery middleware
- CORS configuration
- Request logging
- Security scanning with gosec
π€ Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
)
- Commit your changes (
git commit -m 'Add some amazing feature'
)
- Push to the branch (
git push origin feature/amazing-feature
)
- Open a Pull Request
π License
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
Johnson Awah Alfred - johnsonmessilo19@gmail.com
βοΈ Show your support
Give a βοΈ if this project helped you!