# zzhunght/go-backend-clean-architecture architecture

> Architecture diagram of the GitHub repository zzhunght/go-backend-clean-architecture, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zzhunght/go-backend-clean-architecture
- Repository: https://github.com/zzhunght/go-backend-clean-architecture

## Overview

##### 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

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Client/API Gateway"]:::client
    B["Router (api/route)"]:::api
    C["JWT Middleware"]:::middleware
    D["Controller (api/controller)"]:::api
    E["Usecase (usecase)"]:::business
    F["Repository (repository)"]:::data
    G["Domain (domain)"]:::domain
    J["MongoDB External"]:::database
    H["Bootstrap & Config"]:::config
    I["Docker & Env"]:::devops
    K["Mocks (domain/mocks, mongo/mocks)"]:::testing

    %% Main flow (vertical)
    A -->|"HTTPRequest"| B
    B -->|"RouteHandling"| C
    C -->|"AuthCheck"| D
    D -->|"BusinessLogicInvocation"| E
    E -->|"RepositoryCall"| F
    F -->|"DataPersistence"| J
    F -->|"UsesModels"| G

    %% Bootstrap influences
    H -->|"Initializes"| B
    H -->|"DBConnection"| J

    %% Docker influences
    I -->|"EnvContainerization"| H

    %% Testing support (dashed connections)
    F -.->|"UnitTestMocks"| K
    G -.->|"UnitTestMocks"| K

    %% Styles for clarity
    classDef client fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef api fill:#ABEBC6,stroke:#229954,stroke-width:2px;
    classDef middleware fill:#FAD7A0,stroke:#D68910,stroke-width:2px;
    classDef business fill:#F5CBA7,stroke:#A04000,stroke-width:2px;
    classDef data fill:#D7BDE2,stroke:#8E44AD,stroke-width:2px;
    classDef domain fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef config fill:#AED6F1,stroke:#1ABC9C,stroke-width:2px;
    classDef devops fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
    classDef database fill:#D6EAF8,stroke:#2471A3,stroke-width:2px;
    classDef testing fill:#FADBD8,stroke:#C0392B,stroke-width:2px;

    %% Click Events
    click B "https://github.com/zzhunght/go-backend-clean-architecture/tree/main/api/route"
    click D "https://github.com/zzhunght/go-backend-clean-architecture/tree/main/api/controller"
    click E "https://github.com/zzhunght/go-backend-clean-architecture/tree/main/usecase"
    click F "https://github.com/zzhunght/go-backend-clean-architecture/tree/main/repository"
    click G "https://github.com/zzhunght/go-backend-clean-architecture/tree/main/domain"
    click C "https://github.com/zzhunght/go-backend-clean-architecture/blob/main/api/middleware/jwt_auth_middleware.go"
    click H "https://github.com/zzhunght/go-backend-clean-architecture/tree/main/bootstrap"
    click I "https://github.com/zzhunght/go-backend-clean-architecture/tree/main/Dockerfile"
```
