# gbrayhan/microservices-go architecture

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

- Interactive diagram: https://gitdiagram.com/gbrayhan/microservices-go
- Repository: https://github.com/gbrayhan/microservices-go

## Overview

##### Main Components
- Core components:
- Domain Layer – Found under src/domain, this contains core business models (e.g., “Medicine”, “User”) and error handling.
- Application Layer – Located under src/application/usecases, which includes the use-cases (e.g., auth, medicine, user) that orchestrate business logic and application rules.
- Infrastructure Layer – Includes several sub-components:
- Repository: Implements data persistence (src/infrastructure/repository) and contains database initialization and mappings.
- REST Interface: Found in src/infrastructure/rest where adapters, controllers, routes, and middlewares (including logging, error handling, and JWT-based security) are defined.
- Security: The JWT implementation within src/infrastructure/security.
- External Components:
- Database – The stack uses a MySQL/MariaDB instance as indicated by the README and the SQL scripts in docker/scripts/schema.sql.
- API Documentation – Provided via Swagger (docs/swagger.json and docs/swagger.yaml) and supported by swaggo in the routes.
- Docker & Deployment – Dockerfile, docker-compose file, and docker scripts indicate container-based deployment.

##### Relationships and Interactions
- The main entry point (main.go) initializes and wires up the components. It likely sets up the REST server, routes, middlewares, and configures dependency injection between layers.
- The Infrastructure layer provides external adapters to call the underlying Application usecases. For example, incoming HTTP requests hit the REST controllers, which then call the corresponding usecases defined in the application layer.
- The Application layer encapsulates business logic and makes calls to the Infrastructure repository layer to interact with the database. The Domain layer remains at the center, holding the business rules and types.
- Security (JWT) and middlewares ensure that REST endpoints are protected and that errors and validations are managed properly across the stack.

##### Architectural Patterns and Design Principles
- Clean/Hexagonal Architecture: The system is divided into layers (Domain, Application, and Infrastructure). This separation ensures that business logic is decoupled from external concerns.
- Separation of Concerns: Each directory (usecases, domain, repository, adapter) plays a distinct role.
- Dependency Inversion: Higher-level modules (Application, Domain) are not directly dependent on the lower-level modules (Infrastructure). Adapters and controllers mediate the interactions.

##### Incorporate Technologies and Frameworks
- Golang (v1.21)
- Gin framework for building REST APIs
- JWT for token-based security
- MySQL/MariaDB for data persistence
- Swagger/Swaggo for API documentation
- Viper for managing environment configs
- Docker for containerization and deployment
- Dependency management with Go modules

##### Final Checklist
- Clear component labels (Domain, Application Usecases, REST Controllers, Repositories, Database, External Services)
- Indications of middleware/security layers (JWT, interceptors, etc.).
- Placement of initialization point (main.go) and configuration elements such as Docker and environment variables.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry Point
    subgraph "Initialization"
        Main["main.go"]:::entry
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        REST["REST API (Controllers/Adapters/Middlewares)"]:::infrastructure
        JWT["JWT Security"]:::infrastructure
        Repo["Repository (Data Persistence)"]:::infrastructure
    end

    %% Application Layer
    subgraph "Application Layer"
        UseCase["Application Use Cases"]:::application
    end

    %% Domain Layer
    subgraph "Domain Layer"
        Domain["Domain Models"]:::domain
    end

    %% External Components
    subgraph "External Components"
        DB["MySQL/MariaDB Database"]:::external
        SwaggerJSON["Swagger API (JSON)"]:::external
        SwaggerYAML["Swagger API (YAML)"]:::external
        subgraph "Deployment & Docker"
            DF["Dockerfile"]:::external
            DC["docker-compose.yml"]:::external
            DD["docker/ Directory"]:::external
        end
    end

    %% Connections
    Main -->|"initializes"| REST
    REST -->|"routes to"| UseCase
    UseCase -->|"orchestrates"| Domain
    UseCase -->|"invokes"| Repo
    Repo -->|"persists to"| DB
    REST -->|"security"| JWT
    REST -->|"documents"| SwaggerJSON
    REST -->|"documents"| SwaggerYAML
    %% Deployment interaction (dashed arrow)
    DF ---|"deploys"| Main
    DC ---|"deploys"| Main
    DD ---|"deploys"| Main

    %% Styles
    classDef entry fill:#ffebcd,stroke:#d2691e,stroke-width:2px;
    classDef application fill:#add8e6,stroke:#00008b,stroke-width:1px;
    classDef domain fill:#90ee90,stroke:#006400,stroke-width:1px;
    classDef infrastructure fill:#ffa500,stroke:#ff8c00,stroke-width:1px;
    classDef external fill:#d3d3d3,stroke:#696969,stroke-width:1px;

    %% Click Events
    click Main "https://github.com/gbrayhan/microservices-go/blob/main/main.go"
    click Domain "https://github.com/gbrayhan/microservices-go/tree/main/src/domain"
    click UseCase "https://github.com/gbrayhan/microservices-go/tree/main/src/application/usecases"
    click Repo "https://github.com/gbrayhan/microservices-go/tree/main/src/infrastructure/repository"
    click REST "https://github.com/gbrayhan/microservices-go/tree/main/src/infrastructure/rest"
    click JWT "https://github.com/gbrayhan/microservices-go/blob/main/src/infrastructure/security/jwt/jwt.go"
    click DB "https://github.com/gbrayhan/microservices-go/blob/main/docker/scripts/schema.sql"
    click SwaggerJSON "https://github.com/gbrayhan/microservices-go/blob/main/docs/swagger.json"
    click SwaggerYAML "https://github.com/gbrayhan/microservices-go/blob/main/docs/swagger.yaml"
    click DF "https://github.com/gbrayhan/microservices-go/tree/main/Dockerfile"
    click DC "https://github.com/gbrayhan/microservices-go/blob/main/docker-compose.yml"
    click DD "https://github.com/gbrayhan/microservices-go/tree/main/docker/"
```
