How dowithlogic/golang-clean-architecture works
Project Identification and Main Purpose
• This project is a backend service implemented in Go, built using Clean Architecture principles and SOLID methodologies. It focuses on a layered structure separating concerns such as HTTP delivery, business logic (usecases), data models/entities (DTOs), and repository (data access layer).
• The README and file tree indicate the main sections as “Controller / Delivery”, “DTO”, “Usecase”, “Entity”, and “Repository”. There is an HTTP API built on top of the Echo framework and additional delivery mechanisms (Kafka, RPC).
Analyzing the File Structure
• Top-level directories:
• cmd/api: Contains the entry point for the API server.
• cmd/scheduler: Prepares for scheduled tasks (even if it’s a placeholder now).
• config: Contains YAML configurations and Go code for configuration management.
• database: Holds migration files (for MySQL) and likely tracks the schema.
• internal: Contains the core business logic and domain layers.
– Under internal, the “app” and “middleware” provide framework and cross-cutting concerns.
– The “users” folder is organized into further subdirectories (delivery, dtos, entities, repository, usecase, etc.) showing clear adherence to the Clean Architecture layers.
• pkg: Provides helper packages such as app_crypto, app_http, JWT, Redis, observability, etc. These illustrate infrastructure support (logging, tracing, error handling, and data source utilities).
• Other files like Dockerfile, docker-compose.yml, and Makefile indicate support for deployment, containerization, and automation.
Key Components to Capture
a. Application Layers (as defined by Clean Architecture):
• Delivery/Controller Layer: Represented by “internal/users/delivery/http/v1/handlers.go” (HTTP endpoints) and additional potential delivery methods like Kafka and RPC.
• Usecase/Application Layer: Implemented in “internal/users/usecase” and “internal/users/usecase.go”.
• Domain/Entity Layer: Found under “internal/users/entities” and “internal/users/dtos”.
• Infrastructure/Repository Layer: With “internal/users/repository” and its SQL queries and repository-test files.
b. Infrastructure and Configurations:
• Server: Use of Echo framework (observed in files such as internal/middleware/echo.go).
• Database: MySQL migrations under “database/mysql/migration”; data source utilities in pkg/datasource.
• External Services: Kafka delivery under “internal/users/delivery/kafka” suggests integration with a messaging system.
• Utility packages: JWT, Redis, observability, crypto utilities etc. are key support layers.
Establishing Relationships and Interactions
• The HTTP server (cmd/api) receives external requests and passes them to the controller layer.
• Controllers (delivery) use DTOs to capture incoming requests and delegate to the usecase layer.
• The usecase layer contains business logic which interacts with entities and uses repository abstractions to fetch/store data.
• Repositories interact with the database via SQL queries from pkg/datasource and migration scripts.
• Middleware components handle cross-cutting concerns like logging, error handling, cache management, and authentication (JWT).
• Additional service components (like scheduler and Kafka or RPC delivery options) integrate with the core business logic following similar layered interactions.
• Configuration management (via the “config” directory) and observability packages (logging, metrics, tracing) are used throughout to ensure consistency and maintainability.
Important Architectural Patterns and Design Principles
• Clean Architecture: Emphasize the separation of concerns by isolating domain logic from interface and infrastructure.
• SOLID Principles: Ensure that each component (such as usecases, entities, and repositories) has a single responsibility and depends on abstractions rather than concrete implementations.
• Layered Architecture: Clearly demarcate controllers, usecases, and repository layers, making the system modular and testable.
• Infrastructure abstraction: Using packages in pkg (for crypto, JWT, redis, datasource, etc.) to abstract external libraries.