Skip to content

agung-ap/go-skeleton

Repository files navigation

πŸš€ Go Skeleton

Go Version License Go Report Card Go Doc Docker

A production-ready Go skeleton with clean architecture, dependency injection, and modern development practices. Built with Gin, PostgreSQL, Redis, and comprehensive tooling for fast application development.

✨ Features

  • πŸ—οΈ Clean Architecture - Hexagonal architecture with clear separation of concerns
  • πŸ”§ Dependency Injection - Built-in DI container for easy service management
  • πŸ—„οΈ Database Support - PostgreSQL with SQLx and migration management
  • πŸš€ REST API - Gin framework with structured routing and middleware
  • πŸ“ Structured Logging - Zap logger with configurable levels and formats
  • 🐳 Docker Ready - Multi-stage Dockerfile with PostgreSQL and Redis
  • πŸ” Code Quality - GolangCI-Lint with comprehensive linting rules
  • πŸ§ͺ Testing - Built-in test utilities and coverage reporting
  • ⚑ CLI Commands - Migration and server management commands
  • πŸ” Configuration - Viper-based configuration with environment support
  • πŸ“Š Health Checks - Database and service health monitoring

πŸ—οΈ Architecture

β”œβ”€β”€ cmd/                   # Application entrypoints
β”‚   β”œβ”€β”€ app/               # Main application setup
β”‚   └── serve.go           # Server command
β”œβ”€β”€ config/                # Configuration management
β”œβ”€β”€ internal/              # Private application code
β”‚   β”œβ”€β”€ common/            # Shared utilities
β”‚   └── ping/              # Example module (Clean Architecture)
β”‚       β”œβ”€β”€ adapter/       # External adapters (REST, Repository)
β”‚       └── core/          # Business logic and ports
β”œβ”€β”€ pkg/                   # Public packages
β”‚   β”œβ”€β”€ cache/             # Caching utilities
β”‚   β”œβ”€β”€ database/          # Database management
β”‚   β”œβ”€β”€ errors/            # Error handling
β”‚   └── logger/            # Logging utilities
└── migrations/            # Database migrations

πŸš€ Quick Start

Prerequisites

  • Go 1.23.0 or higher
  • Docker and Docker Compose
  • PostgreSQL (via Docker)
  • Redis (via Docker)

Installation

  1. Clone the repository

    git clone https://github.com/your-username/go-skeleton.git
    cd go-skeleton
  2. Copy configuration

    cp application.sample.yml application.yml
  3. Start services with Docker

    make docker-up
  4. Run database migrations

    make migrate
  5. Start the application

    make run-server

The server will be available at http://localhost:8081

πŸ“– Usage

Available Commands

# Development
make build              # Build the application
make run                # Run the application
make run-server         # Start the server
make test               # Run tests
make test-coverage      # Run tests with coverage

# Database
make migrate            # Run all migrations
make migrate-create     # Create new migration
make migrate-up         # Apply migrations
make migrate-down       # Rollback migrations

# Docker
make docker-up          # Start all services
make docker-down        # Stop all services
make docker-rebuild     # Rebuild and restart

# Code Quality
make lint               # Run linter
make fmt                # Format code
make vet                # Run go vet
make quality-checks     # Run all quality checks

CLI Commands

# Start server
go run main.go server

# Database migrations
go run main.go migrate up
go run main.go migrate down
go run main.go migrate create --name=add_users_table

Configuration

The application uses application.yml for configuration. Key settings:

# Server Configuration
SERVER_PORT: 8081
READ_TIMEOUT_MS: 2000
WRITE_TIMEOUT_MS: 2000

# Database Configuration
DB_DRIVER: postgres
DB_NAME: go_skeleton
DB_HOST: postgres-db
DB_USER: postgres
DB_PASSWORD: postgres
DB_PORT: 5432

# Redis Configuration
REDIS_HOST: localhost
REDIS_PORT: 6379

# Logging Configuration
LOG_LEVEL: debug
LOG_ENCODING: json

πŸ§ͺ Testing

# Run all tests
make test

# Run tests with coverage
make test-coverage

# Run specific test
go test ./internal/ping/...

🐳 Docker

Development

# Start all services
make docker-up

# View logs
docker-compose logs -f app

# Stop services
make docker-down

Production Build

# Build production image
docker build -t go-skeleton .

# Run production container
docker run -p 8080:8080 go-skeleton

πŸ“ Project Structure

Clean Architecture Implementation

The project follows Clean Architecture principles:

  • Adapters: External interfaces (REST handlers, repositories)
  • Core: Business logic and domain models
  • Ports: Interfaces defining contracts between layers

Example Module: Ping

// Core business logic
type PingService interface {
    Ping() string
}

// REST adapter
type PingHandler struct {
    PingService PingService
}

// Repository adapter
type PingRepository struct {
    db *sqlx.DB
}

πŸ”§ Development

Adding New Modules

  1. Create module structure in internal/
  2. Implement core business logic
  3. Create adapters for external interfaces
  4. Register routes in the main router
  5. Add to dependency injection container

Code Quality

The project uses GolangCI-Lint with comprehensive rules:

  • Code formatting and imports
  • Security checks (gosec)
  • Performance optimizations
  • Error handling
  • SQL injection prevention

Environment Variables

# Development
ENVIRONMENT=development

# Database
DB_HOST=localhost
DB_PORT=5432
DB_NAME=go_skeleton
DB_USER=postgres
DB_PASSWORD=postgres

# Redis
REDIS_HOST=localhost
REDIS_PORT=6379

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow Go coding standards
  • Write tests for new features
  • Update documentation
  • Run quality checks before committing

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

πŸ“ž Support


⭐ Star this repository if you find it helpful!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •