This is a template repository for building REST APIs with Go. Click the "Use this template" button above to create your own repository based on this template.
This template includes everything you need to build a REST API with Go - logging, middleware, database setup, testing, and deployment configs.
- Structured logging
- Middleware (auth, CORS, etc.)
- Config management
- API docs with Swagger
- Docker setup
- GitHub Actions CI/CD
- Automated releases with GoReleaser
- Database migrations
- Tests
- Makefile for common tasks
The main ones are:
- gorilla/mux for routing
- go-playground/validator for request validation
- go-sql-driver/mysql for MySQL database access
- jmoiron/sqlx for enhanced database access
- Masterminds/squirrel for SQL builder
- golang-migrate/migrate for database migrations
- swaggo/swag for API documentation generation
- strechr/testify for writing easier test assertions
- mockery for generating mock interfaces
- uber/zap for structured logging
- prometheus/client_golang for metrics
- otel for observability
- jaeger for distributed tracing
- Redis for cache
Click the "Use this template" button at the top of this repository, or use GitHub CLI:
gh repo create my-go-api --template MitulShah1/golang-rest-api-templategit clone https://github.com/YOUR_USERNAME/my-go-api.git
cd my-go-apiAfter creating your repository, update these files:
go.mod- Update module nameREADME.md- Update project name and description.github/workflows/go.yml- Update repository references if neededdocker-compose.yml- Update service names if needed
make help # See all available commands
make env # Create .env file
make docker_up # Start with Docker
make test # Run testsgolang-microservice-template/
βββ cmd/
β βββ server/ # Main entry point for the service
β β βββ main.go
βββ config/
β βββ config.go # Application configuration
βββ docs/ # API documentation
βββ internal/
β βββ handlers/ # HTTP handlers
β β βββ server.go # HTTP server
β βββ services/ # Business logic
β βββ repository/ # Data access layer
βββ package/ # Utility packages (database, logging, middleware, etc.)
β βββ database/
β β βββ database.go
βββ β βββmigrations/ # Database migrations
β βββ logger/
β β βββ logger.go
β βββ middleware/
β β βββ basic_auth.go # Basic authentication middleware
β β βββ cors.go # CORS middleware
β βββ βββ promotheus.go # Prometheus metrics
βββ test/
β βββ e2e/ # End-to-end tests
βββ Dockerfile # Docker build configuration
βββ docker-compose.yml # Docker Compose setup
βββ Makefile # Build automation
βββ go.mod # Go module dependencies
βββ go.sum # Dependencies lock file
βββ README.md # Project documentation- Go 1.21+
- Docker and Docker Compose
- Make
To Check All Commands:
make help1; Clone the repository
git clone https://github.com/MitulShah1/golang-rest-api-template.git2; Create .env file from .env.example add details
make env3; Start the application using Docker Compose
make docker_upBuild the application:
make buildRun tests:
make testGenerate API documentation:
make generate_docsCreate Migration:
make create_migrationRun Migrations:
make migration_upDown Migrations:
make migration_downConfiguration is managed through .env. Environment variables can override these settings.
API documentation is generated using Swagger. The documentation is available at http://localhost:8080/swagger/index.html.
The template includes a comprehensive monitoring stack with Grafana and Prometheus:
- Prometheus: Time-series database for metrics collection
- Grafana: Visualization and dashboard platform
- Jaeger: Distributed tracing
- Application Metrics: Built-in Prometheus metrics middleware
- Start the monitoring stack:
make docker_up- Access the monitoring tools:
- Grafana: http://localhost:3000 (admin/admin)
- Prometheus: http://localhost:9090
- Jaeger: http://localhost:16686
- Application Metrics: http://localhost:8080/metrics
The template includes a pre-configured Grafana dashboard with:
- Request rate and response time metrics
- Error rates by status code
- HTTP method distribution
- 95th percentile response times
For detailed monitoring setup instructions, see monitoring/README.md.
Prometheus metrics are exposed at http://localhost:8080/metrics.
- Unit tests are alongside the code
- Integration tests are in the
test/directory - Run all tests with
make test
This project uses GoReleaser for automated releases. Releases are triggered automatically when you push a version tag.
- Create and push a version tag:
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0- The GitHub Actions workflow will automatically:
- Build binaries for multiple platforms (Linux, macOS, Windows)
- Generate checksums
- Create Docker images for AMD64 and ARM64
- Publish to GitHub Container Registry
- Create a GitHub release with changelog
- Update pkg.go.dev documentation
Download the latest release from the releases page:
# Linux AMD64
wget https://github.com/MitulShah1/golang-rest-api-template/releases/download/v1.0.0/golang-rest-api-template_1.0.0_Linux_x86_64.tar.gz
tar -xzf golang-rest-api-template_1.0.0_Linux_x86_64.tar.gz
# macOS ARM64
wget https://github.com/MitulShah1/golang-rest-api-template/releases/download/v1.0.0/golang-rest-api-template_1.0.0_Darwin_arm64.tar.gz
tar -xzf golang-rest-api-template_1.0.0_Darwin_arm64.tar.gzPull and run the Docker image:
# Pull the latest version
docker pull ghcr.io/mitulshah1/golang-rest-api-template:latest
# Or pull a specific version
docker pull ghcr.io/mitulshah1/golang-rest-api-template:v1.0.0
# Run the container
docker run -p 8080:8080 ghcr.io/mitulshah1/golang-rest-api-template:latestTest the release process locally:
# Install GoReleaser
go install github.com/goreleaser/goreleaser@latest
# Test release (without publishing)
goreleaser release --snapshot --cleanThe project includes:
- Dockerfile for containerization
- docker-compose.yml for local development
- GitHub Actions for CI/CD pipeline
- GoReleaser for automated releases
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
This project is licensed under the MIT License - see the LICENSE file for details
