Skip to content

aupv9/clean-templates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Go Service Template

πŸ‡»πŸ‡³ TiαΊΏng Việt | πŸ‡¬πŸ‡§ English


English

A template for quickly scaffolding Go microservices following Clean Architecture + golang-standards/project-layout.

Usage

Windows (PowerShell)

.\init-service.ps1 -ServiceName point-service

Linux/macOS

chmod +x init-service.sh
./init-service.sh point-service

Custom target directory

./init-service.sh reward-service ./my-services/reward-service

Template Structure

go-service-template/
β”œβ”€β”€ cmd/api/main.go                           # Entry point
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ app/app.go                            # DI container
β”‚   β”œβ”€β”€ config/config.go                      # Configuration
β”‚   β”œβ”€β”€ entity/example.go                     # Domain entities
β”‚   β”œβ”€β”€ usecase/
β”‚   β”‚   β”œβ”€β”€ interfaces.go                     # Repository interfaces
β”‚   β”‚   └── example.go                        # Business logic
β”‚   β”œβ”€β”€ controller/http/
β”‚   β”‚   β”œβ”€β”€ server.go                         # HTTP server
β”‚   β”‚   β”œβ”€β”€ middleware.go                     # Middlewares
β”‚   β”‚   └── v1/                               # API v1 handlers
β”‚   └── infrastructure/
β”‚       β”œβ”€β”€ repository/postgres/              # PostgreSQL repos
β”‚       β”œβ”€β”€ cache/redis/                      # Redis cache
β”‚       └── messaging/kafka/                  # Kafka producer
β”œβ”€β”€ migrations/                               # SQL migrations
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ Makefile
β”œβ”€β”€ .env.example
└── README.md

After Initializing a New Service

  1. Update entity - internal/entity/example.go

    • Rename Example to your domain entity
    • Add required fields
  2. Update usecase - internal/usecase/

    • Rename ExampleUseCase
    • Implement business logic
  3. Update repository - internal/infrastructure/repository/postgres/

    • Rename ExampleRepository
    • Implement data access
  4. Update handlers - internal/controller/http/v1/

    • Rename handlers
    • Add API endpoints
  5. Update migrations - migrations/

    • Rename table examples
    • Add schema for your domain

Example: Creating a Point Service

./init-service.sh point-service

# Then:
# - Rename Example -> PointAccount, PointTransaction
# - Implement EarnPoints, RedeemPoints use cases
# - Add APIs: POST /points/earn, POST /points/redeem

Clean Architecture Layers

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           Frameworks & Drivers          β”‚  ← infrastructure/
β”‚         (DB, Cache, Messaging)          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚           Interface Adapters            β”‚  ← controller/
β”‚         (HTTP, gRPC handlers)           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚         Application Business Rules      β”‚  ← usecase/
β”‚              (Use Cases)                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚        Enterprise Business Rules        β”‚  ← entity/
β”‚              (Entities)                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Dependency Rule

  • entity/ β†’ No dependencies
  • usecase/ β†’ Depends only on entity/, defines interfaces
  • controller/ β†’ Depends on usecase/
  • infrastructure/ β†’ Implements interfaces from usecase/

TiαΊΏng Việt

Template để tαΊ‘o nhanh microservice Go theo Clean Architecture + golang-standards/project-layout.

Sα»­ dα»₯ng

Windows (PowerShell)

.\init-service.ps1 -ServiceName point-service

Linux/macOS

chmod +x init-service.sh
./init-service.sh point-service

Custom target directory

./init-service.sh reward-service ./my-services/reward-service

CαΊ₯u trΓΊc Template

go-service-template/
β”œβ”€β”€ cmd/api/main.go                           # Entry point
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ app/app.go                            # DI container
β”‚   β”œβ”€β”€ config/config.go                      # Configuration
β”‚   β”œβ”€β”€ entity/example.go                     # Domain entities
β”‚   β”œβ”€β”€ usecase/
β”‚   β”‚   β”œβ”€β”€ interfaces.go                     # Repository interfaces
β”‚   β”‚   └── example.go                        # Business logic
β”‚   β”œβ”€β”€ controller/http/
β”‚   β”‚   β”œβ”€β”€ server.go                         # HTTP server
β”‚   β”‚   β”œβ”€β”€ middleware.go                     # Middlewares
β”‚   β”‚   └── v1/                               # API v1 handlers
β”‚   └── infrastructure/
β”‚       β”œβ”€β”€ repository/postgres/              # PostgreSQL repos
β”‚       β”œβ”€β”€ cache/redis/                      # Redis cache
β”‚       └── messaging/kafka/                  # Kafka producer
β”œβ”€β”€ migrations/                               # SQL migrations
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ Makefile
β”œβ”€β”€ .env.example
└── README.md

Sau khi init service mα»›i

  1. CαΊ­p nhαΊ­t entity - internal/entity/example.go

    • Đổi tΓͺn Example thΓ nh domain entity cα»§a bαΊ‘n
    • ThΓͺm cΓ‘c fields cαΊ§n thiαΊΏt
  2. CαΊ­p nhαΊ­t usecase - internal/usecase/

    • Đổi tΓͺn ExampleUseCase
    • Implement business logic
  3. CαΊ­p nhαΊ­t repository - internal/infrastructure/repository/postgres/

    • Đổi tΓͺn ExampleRepository
    • Implement data access
  4. CαΊ­p nhαΊ­t handlers - internal/controller/http/v1/

    • Đổi tΓͺn handlers
    • ThΓͺm API endpoints
  5. CαΊ­p nhαΊ­t migrations - migrations/

    • Đổi tΓͺn table examples
    • ThΓͺm schema cho domain

VΓ­ dα»₯: TαΊ‘o Point Service

./init-service.sh point-service

# Sau Δ‘Γ³:
# - Đổi Example -> PointAccount, PointTransaction
# - Implement EarnPoints, RedeemPoints use cases
# - ThΓͺm API: POST /points/earn, POST /points/redeem

Clean Architecture Layers

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           Frameworks & Drivers          β”‚  ← infrastructure/
β”‚         (DB, Cache, Messaging)          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚           Interface Adapters            β”‚  ← controller/
β”‚         (HTTP, gRPC handlers)           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚         Application Business Rules      β”‚  ← usecase/
β”‚              (Use Cases)                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚        Enterprise Business Rules        β”‚  ← entity/
β”‚              (Entities)                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Dependency Rule

  • entity/ β†’ KhΓ΄ng phα»₯ thuα»™c gΓ¬
  • usecase/ β†’ Chỉ phα»₯ thuα»™c entity/, Δ‘α»‹nh nghΔ©a interfaces
  • controller/ β†’ Phα»₯ thuα»™c usecase/
  • infrastructure/ β†’ Implement interfaces tα»« usecase/

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published