This Go RESTful API Boilerplate is engineered to provide a robust, scalable, and production-grade foundation for your next web service. It embraces a clean, Domain-Driven Design (DDD) architecture to ensure maintainability and separation of concerns, empowering you to focus on delivering business value instead of wrestling with infrastructure setup.
- ποΈ Clean Architecture: Separates concerns into distinct layers (domain, application, infrastructure, presentation) for a more organized, testable, and maintainable codebase.
- π RESTful API: A lightweight and high-performance RESTful API built with Gin, a popular Go web framework. Includes CORS and HTTP Security middleware.
- π Live Reload: Automatically restart the application when file changes are detected.
- ποΈ Multiple Database Support: Supports PostgreSQL, MySQL, and MongoDB. Uses a repository pattern for flexible data management.
- π± Database Migration & Seeding: Manage your database schema and seed data with simple
makecommands. - β‘ Multiple Cache Support: Easily connect to Redis or an in-memory cache.
- π§© Dependency Injection: Switch between database or cache implementations without altering business logic.
- π οΈ Code Generation: Automatically generate repository, usecase, and delivery handler with a single
make generatecommand. - π Observability: Observability features include distributed tracing, metrics, and logging.
- π Health Check:
/healthendpoint for liveness and readiness probes. - β Request Validation: Validates incoming HTTP requests using struct tags to ensure data integrity.
- π§Ή Request Sanitization: Sanitizes incoming request data based on struct tags to prevent XSS and other injection attacks.
- β±οΈ Context Propagation: Manages request lifecycles with Go's
contextto handle cancellations and timeouts gracefully. - π Idempotency Handler: Prevents duplicate requests by using a distributed cache, ensuring an operation is processed only once.
- π¦ Rate Limiting: A distributed rate-limiting middleware to protect your API from excessive traffic and abuse.
- π Circuit Breaker: Enhances application stability by preventing repeated calls to failing external services.
- π¦ Standardized Response: Consistent JSON response format across all API endpoints, making it easier for clients to parse and handle responses uniformly.
- βοΈ Email Sending: Includes a mail sender service with support for HTML templates, allowing for easy and dynamic email generation.
- π Background Job Processing: Efficiently handle long-running or resource-intensive tasks asynchronously, ensuring responsive API performance and better user experience.
- π Mock Generation: Easily generate mocks for interfaces using the
make mockcommand, simplifying unit testing. - π Graceful Shutdown: Ensures that the server shuts down gracefully, finishing all in-flight requests and cleaning up resources before exiting.
- π³ Dockerized Environment: Comes with
Dockerfileanddocker-compose.ymlfor a consistent and easy-to-set-up local development environment. - π Pre-Commit Hooks: Automated git hooks that run code quality checks, including linting, formatting, and security scanning before each commit.
- π‘οΈ Quality Gate CI/CD: Automated quality checks in the CI/CD pipeline that enforce code quality standards, test coverage requirements, and security scans before deployment.
π Project Roadmap - Track our development progress, upcoming features, and planned improvements on our public roadmap.
- Go
- Make
- Docker & Docker Compose (for Docker-based setup)
Clone the repository:
git clone https://github.com/goodone-dev/go-boilerplate.git
cd go-boilerplateRun the following command to prepare your development environment. This will make all necessary shell scripts executable:
make setupTo see all available make commands and their descriptions, run:
make helpYou can run the application in two ways:
This is the easiest way to get started, as it handles all services (database, cache, etc.) for you.
-
Start the services:
make up
This command builds and starts the application, database, and other services. The API by default will be accessible at
http://localhost:8080. -
Stop the services:
make down
This command stops all services.
This method requires you to run the database and other services on your local machine.
-
Setup environment variables:
cp .env.example .env
Update
.envwith your configuration. For local development, ensure it points to your local database and other services. -
Run database migrations:
make migration_up DRIVER=postgres
-
(Optional) Seed the database:
make seeder_up DRIVER=postgres
-
Run the application:
make run
This command will start the Go application. The API will be accessible at
http://localhost:8080.
This project is structured following the principles of Clean Architecture. The code is organized into distinct layers, promoting separation of concerns, testability, and maintainability. The dependencies flow inwards, from the outer layers (Infrastructure, Presentation) to the inner layers (Application, Domain).
.
βββ .dev/ # Local development tools, scripts, and configurations.
β βββ script/ # Local development scripts.
βββ .github/ # GitHub-specific configurations including Actions workflows and issue templates.
β βββ workflow/ # GitHub Actions workflows.
βββ cmd/ # Server commands.
β βββ rest/ # REST API server.
β β βββ main.go # Entry point of the application. Initializes and starts the server.
β βββ utils/ # Utility functions shared across the server.
βββ internal/ # Internal packages.
β βββ application/ # Implements use cases by orchestrating domain logic.
β β βββ <domain_name>/ # Groups application logic for a specific domain.
β β β βββ handler/ # Adapters for handling incoming requests (e.g., HTTP, messaging).
β β β β βββ rest/ # REST API handlers for the domain.
β β β β βββ worker/ # Background worker handlers.
β β β βββ repository/ # Repository implementations for the domain.
β β β βββ usecase/ # Business logic and use cases for the domain.
β β βββ ...
β βββ config/ # Configuration loading and management.
β βββ domain/ # Contains core entities and interfaces.
β β βββ <domain_name>/ # Groups domain logic for a specific business entity.
β β β βββ mocks/ # Mocks for domain interfaces.
β β βββ ...
β βββ infrastructure/ # Provides implementations for external services.
β β βββ cache/ # Cache implementations (e.g., Redis).
β β βββ database/ # Database implementations (PostgreSQL, MySQL, MongoDB).
β β βββ integration/ # Clients for external APIs.
β β βββ logger/ # Log aggregation implementations.
β β βββ mail/ # Email sending implementation.
β β βββ message/ # Message bus/broker implementation.
β β βββ tracer/ # Distributed tracing implementation.
β β βββ ...
β βββ presentation/ # Adapters for incoming requests.
β β βββ rest/ # REST API handlers, router, and middleware.
β β β βββ middleware/ # REST API middleware.
β β β βββ router/ # REST API router setup.
β β βββ worker/ # Background worker handlers.
β β βββ consumer/ # Message consumer handlers.
β βββ utils/ # Utility functions shared across the application.
β βββ breaker/ # Circuit breaker utilities.
β βββ html/ # HTML template utilities.
β βββ http_client/ # HTTP client utilities.
β βββ http_response/ # HTTP response utilities.
β βββ sanitizer/ # Request sanitizer utilities.
β βββ validator/ # Request validation utilities.
β βββ ...
βββ migrations/ # SQL migration files for managing database schema changes.
β βββ <database_name>/ # Migration files for a specific database.
β βββ ...
βββ seeders/ # SQL seed files for populating the database with initial data.
β βββ <database_name>/ # Seeder files for a specific database.
β βββ ...
βββ templates/ # HTML templates for emails, PDFs, etc.
β βββ email/ # Email templates.
β βββ pdf/ # PDF templates.
β βββ ...
βββ .env.example # Example environment variables file.
βββ .air.toml # Air.toml for local development.
βββ .mockery.yml # Mockery configuration file.
βββ .pre-commit-config.yaml # Pre-commit configuration file.
βββ Makefile # Makefile with shortcuts for common development commands.
βββ Dockerfile # Dockerfile for building the application image.
βββ docker-compose.yml # Defines services for the local Docker environment.
| Category | Technologies |
|---|---|
| Framework | gin |
| Database | gorm (PostgreSQL, MySQL), mongo-driver (MongoDB) |
| Cache | go-redis |
| API Client | resty |
| Config | viper |
| Validation | validator |
| Migration | golang-migrate |
| Observability | opentelemetry |
| gomail | |
| Circuit Breaker | gobreaker |
| Mocking | mockery |
- Fork the repository
- Create a 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
This project is licensed under the MIT License - see the LICENSE file for details.
Bagus Abdul Kurniawan
- Email: hello@goodone.dev
- Web: goodone.dev
- LinkedIn: linkedin.com/in/bagusak95