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

jj-devhub/go-clean-template

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 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!

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 6380 (host) Caching layer

πŸ“‹ Essential Commands

# Development
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

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

# Dependencies
task deps               # Download and tidy dependencies
task deps-update        # Update dependencies

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
  • 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 - Error handling, response formatting, validation.

🎯 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:

  • Environment variables (.env file)
  • YAML configuration (config/config.yaml)
  • Docker environment (docker-compose.yml)

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.

πŸš€ 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.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 91.5%
  • Dockerfile 8.5%