# ltphat2204/goauth architecture

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

- Interactive diagram: https://gitdiagram.com/ltphat2204/goauth
- Repository: https://github.com/ltphat2204/goauth

## Overview

##### Main Components
- HTTP/API Layer (Transport): Represent the HTTP request handlers (in the users/transport directory) handling endpoints like /token, /verify, and CRUD operations on users.
- Middlewares: Indicate components that handle tasks such as admin validation and authentication (in the middlewares directory).
- Business (Service) Layer: Show the business logic for user operations (users/business), such as creating users, deleting users, token retrieval, password update, etc.
- Data Access (Storage) Layer: Represent components that interact with the MySQL database (users/storage and common/mysql.go). This includes functions to create, retrieve, update, and delete user data.
- Domain/Entity Layer: Include the definitions for the domain models (in users/entity) which outline the structure of User, UserAccess, UserStatus, etc.
- Common Utilities: Illustrate supporting modules available in the common directory (for tokens, unified error/success responses, MySQL model enhancement).
- External Dependencies & Infrastructure: Include MySQL (highlighted in docker-compose.yml) as well as Docker containerization elements (Dockerfile, docker-compose.yml) that deploy and run the service.

##### Relationships & Data Flow
- The client (external user, admin, or any system) sends requests to the service endpoints.
- Requests first pass through the Middlewares which handle authentication (auth.go) and admin validation (admin.go).
- Once validated, the requests are routed to specific HTTP handlers (transport) that process the API calls.
- HTTP handlers then call the Business (Service) layer functions which contain the core business rules.
- The Business layer interacts with the Storage layer for operations like CRUD, token issuance, and so on, which in turn communicate with the MySQL database.
- Responses (success or error) are standardized using common response functions and then sent back to the client.
- Additionally, JWT token generation and validation occur through functions defined in the common directory to ensure secure authorization.

##### Architectural Patterns and Design Principles
- Clean Architecture: This is evident by how the project separates concerns into transport, business logic, and storage while keeping the domain (entity) pure.
- Layered Design: The strict separation helps manage dependencies – for example, UI (HTTP handlers) never access the database directly, and business logic doesn’t handle HTTP details.
- Dependency Injection (Implicitly): The architecture implies that you could swap out the MySQL implementation (by modifying common/mysql.go and users/storage) without affecting the business logic.
- Containerization: Use of Docker (Dockerfile, docker-compose.yml) shows that the service and its dependencies (MySQL) run in separate containers, illustrating modern deployment practices.

- Component has a clear label and that the dependencies (such as JWT in the common module and MySQL connectivity) are represented.

## Diagram (Mermaid)

```mermaid
graph TD
    client["Client (External Request)"]:::client

    subgraph "Middlewares"
        mw["Auth & Admin Middlewares"]:::middleware
    end

    subgraph "HTTP/API Layer (Transport)"
        transport["HTTP Handlers"]:::transport
    end

    subgraph "Business (Service) Layer"
        business["User Business Logic"]:::business
    end

    subgraph "Data Access (Storage) Layer"
        storage["Storage Operations"]:::storage
        mysqlConn["MySQL Connectivity"]:::storage
    end

    subgraph "Domain/Entity Layer"
        domain["Domain Entities"]:::entity
    end

    subgraph "Common Utilities"
        common["Common Functions (JWT, Error/Success responses)"]:::common
    end

    subgraph "External Dependencies & Infrastructure"
        docker["Docker Containerization"]:::infra
        dockerFile["Dockerfile"]:::infra_file
        dockerCompose["docker-compose.yml"]:::infra_file
        mysqlDB["MySQL Database"]:::db
    end

    client -->|"sendsRequest"| mw
    mw -->|"validates"| transport
    transport -->|"callsLogic"| business
    business -->|"invokes"| storage
    business -->|"usesDomain"| domain
    business -->|"utilizes"| common
    storage -->|"connects"| mysqlConn
    mysqlConn -->|"accesses"| mysqlDB
    docker -->|"includes"| dockerFile
    docker -->|"includes"| dockerCompose

    %% Click Events
    click transport "https://github.com/ltphat2204/goauth/tree/main/users/transport"
    click mw "https://github.com/ltphat2204/goauth/tree/main/middlewares"
    click business "https://github.com/ltphat2204/goauth/tree/main/users/business"
    click storage "https://github.com/ltphat2204/goauth/tree/main/users/storage"
    click mysqlConn "https://github.com/ltphat2204/goauth/blob/main/common/mysql.go"
    click domain "https://github.com/ltphat2204/goauth/tree/main/users/entity"
    click common "https://github.com/ltphat2204/goauth/tree/main/common"
    click dockerFile "https://github.com/ltphat2204/goauth/tree/main/Dockerfile"
    click dockerCompose "https://github.com/ltphat2204/goauth/blob/main/docker-compose.yml"

    %% Styles
    classDef client fill:#ffde7d,stroke:#333,stroke-width:2px;
    classDef middleware fill:#B3E5FC,stroke:#333,stroke-width:2px;
    classDef transport fill:#FFCCBC,stroke:#333,stroke-width:2px;
    classDef business fill:#C8E6C9,stroke:#333,stroke-width:2px;
    classDef storage fill:#B2EBF2,stroke:#333,stroke-width:2px;
    classDef entity fill:#D1C4E9,stroke:#333,stroke-width:2px;
    classDef common fill:#FFF9C4,stroke:#333,stroke-width:2px;
    classDef infra fill:#CFD8DC,stroke:#333,stroke-width:2px;
    classDef infra_file fill:#E0E0E0,stroke:#333,stroke-width:2px;
    classDef db fill:#FFCDD2,stroke:#333,stroke-width:2px;
```
