# amitshekhariitbhu/go-backend-clean-architecture architecture

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

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

## Overview

##### Project Type and Purpose
- This is a backend web service application following Clean Architecture. It clearly separates concerns, ensuring that routing, controllers, business logic (use cases), and data access (repositories) are decoupled, promoting testability and maintainability.
- Key features include JWT-based authentication, RESTful API endpoints (e.g., signup, login, profile, task management), and an external MongoDB database for persistence. Docker is also used for containerized deployments.

##### File Structure Insights
- Top-level directories such as "api", "bootstrap", "cmd", "domain", "repository", and "usecase" indicate a layered or hexagonal architecture.
- The "api" directory contains subdirectories for controllers (handling HTTP requests), routes (defining the API endpoints), and middleware (like the JWT authentication middleware).
- The "domain" folder contains core domain models and error handling, which represent the business entities.
- The "usecase" directory encapsulates the business logic whereas the "repository" handles data interactions with MongoDB.
- The "bootstrap" and "cmd" directories include the initialization code (setting up the application, environment, and DB connections) and the main entry point respectively.
- Testing is supported through mocks found in the "domain/mocks", "mongo/mocks", and the test files scattered in the controller and usecase directories.

a. Core Layers:
- Front Controller/Router Layer: Represent this with the "api/route" component. It receives HTTP requests and directs them to the appropriate controllers.
- Controller Layer: In "api/controller", each controller (login, profile, refresh token, signup, task) receives requests routed in and handles the immediate translation between the HTTP layer and the business logic.
- Middleware (JWT): Show a specific middleware block (from "api/middleware") for ensuring secure access.
- Usecase Layer: Capture the business logic contained in "usecase" – these components process business rules and orchestrate domain operations.
- Repository/Data Access Layer: Highlight the "repository" components that interface with MongoDB. Also include the "mongo" package which defines the connection and mocks.
- Domain Layer: Represent core business entities and data structures in the "domain" folder.
- Infrastructure/Bootstrap: Include the "bootstrap" section (app.go, database.go, env.go) that initializes the application.
- Entry Point: Mark the "cmd/main.go" as the starting point for the application.

##### Relationships and Data Flow
- HTTP Client → Router (gin framework through the "api/route") → Middleware (JWT authentication).
- Middleware → Controller – Controllers (in "api/controller") then invoke specific use case functions.
- Controller → Usecase – Use cases handle business rules and interact with repositories.
- Usecase → Repository – The repository layer interacts with the database (defined in "mongo" and "repository").
- Repositories return data/confirm transactions → Usecase sends response back to Controller → Final response returned to the Client.
- Error flow pathways (e.g., error responses from the domain layer).

##### Architectural Patterns and Key Technologies
- This design leverages Clean Architecture principles: decoupling dependencies, clear layer separation, and ensuring that business logic is independent of frameworks and delivery mechanisms.
- Gin as the HTTP web framework.
- MongoDB as the primary database (with the mongo go driver).
- JWT libraries for authentication.
- Viper for configuration management.
- Docker for containerization.
- Testing tools like Testify and Mockery that are used within the mocks directories, indicating an emphasis on test-driven development.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["HTTP Client"]:::external
    Entry["Application Entry Point (cmd/main.go)"]:::infra
    Bootstrap["Bootstrap/Initializer (bootstrap)"]:::infra
    Router["Router (api/route)"]:::api
    Middleware["JWT Middleware (api/middleware/jwt_auth_middleware.go)"]:::api
    Controllers["Controllers (api/controller)"]:::api
    Usecase["Usecase (usecase)"]:::business
    Domain["Domain Entities (domain)"]:::business
    Repository["Repositories (repository)"]:::persistence
    Mongo["MongoDB"]:::persistence

    Client -->|"sends_request"| Router
    Router -->|"passes_through"| Middleware
    Middleware -->|"forwards_to"| Controllers
    Controllers -->|"invokes"| Usecase
    Usecase -->|"uses"| Domain
    Usecase -->|"calls"| Repository
    Repository -->|"interacts_with"| Mongo

    Entry -->|"initializes"| Bootstrap
    Bootstrap -->|"sets_up_server"| Router

    Mongo -->|"returns_data"| Repository
    Repository -->|"sends_data"| Usecase
    Usecase -->|"responds"| Controllers
    Controllers -->|"sends_response"| Router
    Router -->|"delivers"| Client

    click Router "https://github.com/amitshekhariitbhu/go-backend-clean-architecture/tree/main/api/route"
    click Controllers "https://github.com/amitshekhariitbhu/go-backend-clean-architecture/tree/main/api/controller"
    click Middleware "https://github.com/amitshekhariitbhu/go-backend-clean-architecture/blob/main/api/middleware/jwt_auth_middleware.go"
    click Usecase "https://github.com/amitshekhariitbhu/go-backend-clean-architecture/tree/main/usecase"
    click Repository "https://github.com/amitshekhariitbhu/go-backend-clean-architecture/tree/main/repository"
    click Mongo "https://github.com/amitshekhariitbhu/go-backend-clean-architecture/tree/main/mongo"
    click Domain "https://github.com/amitshekhariitbhu/go-backend-clean-architecture/tree/main/domain"
    click Bootstrap "https://github.com/amitshekhariitbhu/go-backend-clean-architecture/tree/main/bootstrap"
    click Entry "https://github.com/amitshekhariitbhu/go-backend-clean-architecture/blob/main/cmd/main.go"

    classDef api fill:#F1C40F,stroke:#000,stroke-width:1px;
    classDef business fill:#2ECC71,stroke:#000,stroke-width:1px;
    classDef persistence fill:#3498DB,stroke:#000,stroke-width:1px;
    classDef infra fill:#E74C3C,stroke:#000,stroke-width:1px;
    classDef external fill:#BDC3C7,stroke:#000,stroke-width:1px;
```
