How thnkrn/go-gin-clean-arch works
Project Type and Purpose
• This is a backend RESTful API service built in Go, following the “Clean Architecture” (hexagonal/corporate) pattern.
• It uses Gin as the HTTP framework, GORM with PostgreSQL for persistence, JWT for authentication, Wire for dependency injection, Viper for configuration, and Swag for auto-generated Swagger docs.
File Structure Analysis
• cmd/api/main.go: entry point that initializes and starts the server.
• pkg/config: application configuration loader (Viper).
• pkg/db: database connection setup (GORM + PostgreSQL).
• pkg/di: Wire DI definitions.
• pkg/domain: core business entities and interfaces.
• pkg/repository: repository implementations and interfaces (data access layer).
• pkg/usecase: business logic orchestrators.
• pkg/api: HTTP layer, including router/server.go, handlers (user.go), middleware (auth.go), and docs.
Key README Insights
• Lists major technologies and their roles.
• Describes available REST endpoints (/, /login, /api/users, etc.).
• Shows Makefile targets for building, testing, DI generation, and Swagger generation.
Main Components
a. External Clients
• Web/mobile clients or curl/Postman invoking REST endpoints.
b. API Layer
• Gin HTTP router (pkg/api/server.go)
• Swagger docs endpoint (cmd/api/docs/*.yaml)
• HTTP middleware (JWT auth in pkg/api/middleware/auth.go)
• Handlers (pkg/api/handler/user.go)
c. Application Layer (Use Cases)
• Use case interface (pkg/usecase/interface/user.go)
• Use case implementation (pkg/usecase/user.go)
d. Domain Layer
• Domain entity definitions (pkg/domain/user.go)
e. Infrastructure Layer
• Repository interface (pkg/repository/interface/user.go)
• GORM-based repository implementation (pkg/repository/user.go)
• Database connection (pkg/db/connection.go)
f. Cross-cutting Services
• Configuration (Viper in pkg/config/config.go)
• Dependency Injection container (Wire in pkg/di/wire.go + wire_gen.go)
• Authentication (JWT library usage in middleware and handlers)
Relationships and Interactions
• Client → Gin Router → Middleware (auth) → Handlers
• Handlers → Use Case methods → Domain models
• Use Cases → Repository interfaces → GORM implementation → PostgreSQL
• At startup: main.go uses DI (Wire) to assemble Config → DB → Repositories → Use Cases → HTTP Server.
• Swagger docs are served via a Gin endpoint generated by swag.
Architectural Patterns and Principles
• Clean Architecture / Hexagonal: strict layer separation inward (depends on abstractions).
• Dependency Inversion: higher layers define interfaces, lower layers implement them.
• Single Responsibility: each package has a distinct concern.
• Convention over configuration: Makefile, Go modules, standard folder layout.
• Layers clearly separated and labeled (Framework / Adapters / Application / Domain).
• Notes on configuration loading and DI graph generation at startup.