Skip to content

Production-ready Go API template implementing Clean Architecture with Docker-first development and comprehensive tooling.

License

Notifications You must be signed in to change notification settings

erfanul007/go-clean-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Go Clean Architecture Template

A robust Go API template built following Clean Architecture principles and Domain-Driven Design (DDD) patterns. This project uses Docker-first development for consistency across all environments.

πŸš€ Quick Start

Prerequisites

Note: No local Go installation required! All operations run in Docker containers via Task commands.

Setup & Run

git clone <repository-url>
cd go-clean-template
task start  # One-command setup and start

Access Points:

🐳 Services

Service Port Description
API 8080 Go application with live reload
PostgreSQL 5432 Primary database
Redis 6379 Caching layer

πŸ“‹ Essential Commands

# Development (Docker-first)
task start              # Setup and start development
task dev                # Start with live reload
task check              # Run quality checks (format, lint, test)
task health             # Check service health
task clean              # Clean everything

# Code Quality (in Docker)
task fmt                # Format Go code
task lint               # Lint code with golangci-lint
task test               # Run tests
task test-coverage      # Run with coverage

# Dependencies (in Docker)
task deps               # Download and tidy dependencies
task deps-update        # Update dependencies

# Documentation
task swag-gen           # Generate Swagger docs

For advanced Docker operations, see DOCKER.md.

πŸ—οΈ Tech Stack

Current:

  • Go 1.24 with Chi Router
  • PostgreSQL 15 + Redis 7 (configured)
  • Viper (config) + Zap (logging)
  • Swagger/OpenAPI documentation
  • Docker & Docker Compose (Docker-first development)
  • Task (Taskfile.yml) for all operations
  • Air for live reload in containers
  • Health monitoring endpoints

Planned:

  • Ent ORM, JWT Authentication, Business Logic Implementation

πŸ“ Project Structure

go-clean-template/
β”œβ”€β”€ cmd/api/                    # Application entry point
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ application/            # Use cases and business logic
β”‚   β”œβ”€β”€ domain/                 # Domain entities and business rules
β”‚   β”œβ”€β”€ infrastructure/         # External concerns (database, config, logging)
β”‚   β”œβ”€β”€ presentation/           # HTTP handlers, routes, API documentation
β”‚   └── shared/                 # Shared utilities (errors, response)
β”œβ”€β”€ config/                     # Configuration files
β”œβ”€β”€ docs/                       # API documentation
β”œβ”€β”€ build/                      # Dockerfiles
β”œβ”€β”€ deployments/               # Docker Compose & scripts
└── Taskfile.yml               # Task automation

πŸ›οΈ Clean Architecture Layers

This project implements Clean Architecture with clear separation of concerns:

🎯 Domain (internal/domain/)

Core business logic - Entities, value objects, business rules. No external dependencies.

domain/
β”œβ”€β”€ example/         # Domain-specific entities
β”œβ”€β”€ shared/          # Common domain concepts
β”‚   β”œβ”€β”€ events/      # Domain events
β”‚   β”œβ”€β”€ values/      # Shared value objects
β”‚   └── interfaces/  # Domain contracts
└── services/        # Domain services

πŸ”„ Application (internal/application/)

Use cases - Orchestrates domain objects, depends only on domain layer.

application/
β”œβ”€β”€ example/         # Application services
β”‚   β”œβ”€β”€ commands/    # Command handlers
β”‚   β”œβ”€β”€ queries/     # Query handlers
β”‚   β”œβ”€β”€ dto/         # Application DTOs
β”‚   └── interfaces/  # Repository contracts
β”œβ”€β”€ common/          # Shared application logic
└── services/        # Application services

🌐 Presentation (internal/presentation/)

HTTP interface - Handlers, routes, middleware, DTOs, Swagger documentation.

presentation/http/
β”œβ”€β”€ handlers/    # HTTP request handlers
β”œβ”€β”€ middleware/  # CORS, auth, logging
β”œβ”€β”€ dto/         # Request/response structures
β”œβ”€β”€ routes.go    # API endpoints
└── server.go    # HTTP server setup

πŸ”§ Infrastructure (internal/infrastructure/)

External concerns - Database, config, logging, authentication implementations.

infrastructure/
β”œβ”€β”€ auth/        # JWT, password hashing
β”œβ”€β”€ config/      # Environment, YAML config
β”œβ”€β”€ logger/      # Structured logging
└── persistence/ # Database, repositories

🀝 Shared (internal/shared/)

Common utilities - Enhanced error handling with chaining, response formatting, validation.

shared/
β”œβ”€β”€ errors/          # Enhanced error handling with cause chaining
β”œβ”€β”€ response/        # HTTP response utilities with error chain support
└── validation/      # Input validation helpers

🎯 Key Principles

  • Dependency Rule: Outer layers depend on inner layers
  • Framework Independence: Business logic isolated from frameworks
  • Testability: Each layer independently testable
  • Single Responsibility: Each layer has one clear purpose

πŸ”„ Request Flow

HTTP β†’ Presentation β†’ Application β†’ Domain
  ↑         ↓            ↓         ↓
Response ← Infrastructure ← Infrastructure

πŸ” Health Endpoints

Endpoint Purpose
/health Detailed health with dependencies
/heartbeat Simple heartbeat
/live Container liveness probe
/ready Container readiness probe
/system System information

πŸ“š API Documentation

Swagger UI: http://localhost:8080/swagger/index.html

Currently Available Endpoints:

  • Health monitoring (/health, /live, /ready)
  • Heartbeat (/heartbeat)

Planned: Full RESTful API implementation following clean architecture patterns.

πŸ”§ Configuration

Uses hybrid configuration system with clear separation of concerns:

Configuration Layers (Priority: High β†’ Low)

  1. Environment Variables - Runtime, sensitive, environment-specific
  2. YAML Configuration (config/config.yaml) - Static application behavior
  3. Code Defaults - Essential fallbacks for critical services

Configuration Files

  • .env.example β†’ .env - Environment-specific and sensitive data
  • config/config.yaml - Static configurations (CORS, rate limiting, Swagger, metrics)
  • docker-compose.yml - Development environment setup

Key Features

  • Security: Sensitive data only in environment variables
  • Flexibility: Easy environment-specific overrides
  • Maintainability: Static configs in version control
  • Deployment: Simple .env file changes for different environments

task setup automatically copies .env.example to .env and downloads dependencies.

πŸ§ͺ Testing

task test               # Run tests
task test-coverage      # Run with coverage

All tests run in Docker for consistency across environments.

⚑ Live Reload Development

The project uses Air for live reload during development, automatically rebuilding and restarting the application when code changes are detected.

Configuration (.air.toml)

  • Watches: cmd/, internal/, config/, docs/ directories
  • File Types: .go, .yaml, .yml, .json files
  • Excludes: Test files, temporary files, build artifacts
  • Build Target: ./tmp/main (excluded from Docker context)

Usage

task dev    # Start with live reload (Docker-first approach)

Benefits: Instant feedback during development, no manual restarts needed, fully integrated with Docker development workflow. Air runs inside the development container, ensuring consistency across all environments.

πŸš€ Deployment

task setup              # Copy .env.example to .env
# Edit .env with production values
task start              # Start all services
task health             # Verify services

For production, update .env with secure values (JWT_SECRET, passwords, etc.).

🀝 Contributing

  1. Install Docker and Task
  2. Run task setup and task dev
  3. Make changes (live reload enabled)
  4. Run task check before committing
  5. Submit pull request

πŸ“š Additional Documentation

  • DOCKER.md - Advanced Docker operations, troubleshooting, and deployment

πŸ‘¨β€πŸ’» Author

Md. Erfanul Islam Bhuiyan - Software Engineer

GitHub LinkedIn

About

Production-ready Go API template implementing Clean Architecture with Docker-first development and comprehensive tooling.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •