How zzhunght/go-backend-clean-architecture works
Main Components
• Client/API Gateway: Represent the consumers (e.g., browser or mobile app) sending HTTP requests.
• Router Layer: The API routers (located in “api/route”) that capture incoming HTTP requests and direct them to their corresponding controllers.
• Controller Layer: The controllers (in “api/controller”) handling the HTTP request–response cycle. They invoke specific use cases based on routes.
• Usecase Layer: Business logic encapsulated in usecase files (in “usecase”) that manage application-specific operations (e.g., login, profile, tasks).
• Repository Layer: Interfaces and implementations (in “repository”) responsible for data persistence, in this case, interfacing with MongoDB (see “mongo” package for DB connections).
• Domain Layer: Core domain models and entities (in “domain”), including definitions for user, task, authentication tokens, and responses.
• Middleware: The JWT authentication middleware (in “api/middleware”) that secures private endpoints.
• Bootstrap and Configuration: The “bootstrap” directory contains application initialization code (app setup, database, environment loading).
• Docker & Environment: Dockerfile, docker-compose.yaml, and .env support containerization and environment configuration.
Relationships and Interactions
• From the Router, show a flow to the corresponding Controller. Emphasize that for authenticated routes, the request passes through the JWT Authentication Middleware before reaching the Controller.
• The Usecase layer then communicates with the Repository layer for data persistence and retrieval.
• The Repository interacts with the external MongoDB (illustrate this as an external component) via the official MongoDB Go driver.
• Additionally, represent interactions with configuration and initialization performed in the Bootstrap layer which sets up the Gin engine, database connections, and environment variables.
Architectural Patterns and Design Principles
• Clean Architecture Layers: Clearly delineate the separation of concerns among the Router, Controller, Usecase, Repository, and Domain.
• Dependency Inversion: Demonstrate how high-level policies (Usecase) depend on abstractions (Repository interfaces in Domain) rather than low-level implementations.
• Testability: Note the presence of “mocks” in the Domain and Mongo folders, which support unit testing. This decoupling is a key principle here.
• Middleware Integration: Highlight that the JWT authentication serves as a cross-cutting concern applied across selected routes.
Relevant Technologies
• Gin (HTTP Framework)
• MongoDB (NoSQL Database) accessed via the mongo go driver.
• JWT for creating and validating access and refresh tokens.
• Viper for configuration management.
• Additional packages like bcrypt, testify, and mockery used in testing.
Suggested Elements
• Client → Router → Middleware (if applicable) → Controller → Usecase → Repository → MongoDB