# leoliscanoa/go-rest-api architecture

> Architecture diagram of the GitHub repository leoliscanoa/go-rest-api, 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/leoliscanoa/go-rest-api
- Repository: https://github.com/leoliscanoa/go-rest-api

## Overview

##### Main Components
- HTTP Server & Routes: Represented by the “routes” directory and main entry point (main.go), which initializes the application and binds URL patterns.
- Controllers: The “controller” directory (e.g., user.controller.go) handles HTTP requests and responses.
- Services: Contained in the “service” directory, these encapsulate the business logic. Note that there are both service implementations and test mocks.
- Repositories: Found under “repository” that abstracts data access. This includes both implementations (using Gorm for Postgres) and mocks.
- Models & DTOs: The “model” directory holds domain models (e.g., user.model.go), while “dto” provides data transfer objects.
- Middleware: Located in the “middleware” directory, these add cross-cutting concerns (e.g., basic authentication or logging).
- Configuration: The “config” directory deals with database and application configuration (including separate mock configuration).
- Documentation: The “docs” folder contains Swagger-related files (swagger.json/yaml) along with application documentation.
- Helper: Provides utility functions (like those in helper/response.helper.go) for constructing responses.

##### Relationships and Interactions
- A client request flows from the HTTP Server (as initiated in main.go, leveraging routes) to a specific controller.
- The controller validates input and then delegates to a service.
- The service processes business logic and, if needed, interacts with a repository to either query or update the database.
- Repositories use Gorm to communicate with Postgres (managed via Docker Compose) for persistent storage.
- Middleware intercepts requests for tasks like authentication or logging before they reach the controller.
- The configuration layer provides necessary runtime settings (such as database credentials) to the rest of the system.
- Swagger documentation is generated/inputted for API endpoint discovery and testing, visible at the provided Swagger UI link.

##### Architectural Patterns & Design Principles
- Layered (or N-tier) Architecture: Clearly divided into controllers, services, and repositories.
- Repository Pattern: Separating data access logic from business logic.
- Use of Mocks: Testing structure is evident from the presence of mock implementations in both repository and service layers.
- Containerization: With Docker (and docker-compose), the deployment is designed to abstract infrastructure dependencies (including Postgres).
- API Documentation Integration: Use of Swagger for API documentation.

##### Relevant Technologies & Frameworks
- Go (version 1.21.7)
- Mux for HTTP routing
- Docker & Docker Compose for container orchestration
- Postgres (accessed via Gorm ORM, version 1.25.7)
- Sqlmock and Testify for testing
- Swaggo for generating Swagger documentation

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "HTTP Layer"
        A1["main.go"]:::api
        A2["routes/routes.go"]:::api
        A1 --> A2
    end

    subgraph "Middleware Layer"
        B1["basic.middleware.go"]:::middleware
    end

    subgraph "Controller Layer"
        C1["user.controller.go"]:::controller
    end

    subgraph "Service Layer"
        D1["user.service.go"]:::service
        D2["user.service.implement.go"]:::service
        D3["Service Mocks"]:::service_mock
        D1 --> D2
    end

    subgraph "Repository Layer"
        E1["user.repository.go"]:::repository
        E2["user.repository.implement.go"]:::repository
        E3["Repository Mocks"]:::repository_mock
        E1 --> E2
    end

    subgraph "Database"
        F1["Postgres (Gorm)"]:::database
    end

    subgraph "Configuration"
        G1["database.config.go"]:::config
        G2["database.mock.go"]:::config
    end

    subgraph "Documentation"
        H1["swagger.json"]:::docs
        H2["swagger.yaml"]:::docs
        H3["docs.go"]:::docs
    end

    subgraph "Models & DTOs"
        I1["user.model.go"]:::models
        I2["generic.dto.go"]:::models
    end

    subgraph "Helper"
        J1["response.helper.go"]:::helper
    end

    K1("Client Request"):::client

    %% Data Flow Connections
    K1 -->|"request"| A1
    A2 -->|"passes_request"| B1
    B1 -->|"forwards_to"| C1
    C1 -->|"calls"| D1
    D2 -->|"executes_query"| E1
    E2 -->|"uses_ORM"| F1

    %% Configuration feeds layers
    G1 ---|"config_data"| A1
    G1 ---|"config_data"| E1

    %% Documentation access point
    H1 ---|"Swagger_API"| K1

    %% Models and DTOs usage
    I1 ---|"domain_model"| C1
    I2 ---|"data_transfer"| D1

    %% Helper usage
    J1 ---|"utility"| C1

    %% Mocks for testing (non-production)
    D3 ---|"test_mock"| D1
    E3 ---|"test_mock"| E1

    %% Styles for clarity
    classDef client fill:#f2f2f2,stroke:#333,stroke-width:2px,color:#000;
    classDef api fill:#bbdefb,stroke:#0d47a1,stroke-width:2px,color:#0d47a1;
    classDef middleware fill:#ffe082,stroke:#ff6f00,stroke-width:2px,color:#ff6f00;
    classDef controller fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px,color:#2e7d32;
    classDef service fill:#d1c4e9,stroke:#512da8,stroke-width:2px,color:#512da8;
    classDef service_mock fill:#e1bee7,stroke:#6a1b9a,stroke-width:2px,color:#6a1b9a;
    classDef repository fill:#ffe0b2,stroke:#ef6c00,stroke-width:2px,color:#ef6c00;
    classDef repository_mock fill:#ffccbc,stroke:#d84315,stroke-width:2px,color:#d84315;
    classDef database fill:#b2dfdb,stroke:#00796b,stroke-width:2px,color:#00796b;
    classDef config fill:#f0f4c3,stroke:#afb42b,stroke-width:2px,color:#afb42b;
    classDef docs fill:#dcedc8,stroke:#558b2f,stroke-width:2px,color:#558b2f;
    classDef models fill:#ffcdd2,stroke:#c62828,stroke-width:2px,color:#c62828;
    classDef helper fill:#e0f7fa,stroke:#00796b,stroke-width:2px,color:#00796b;

    %% Click Events
    click A1 "https://github.com/leoliscanoa/go-rest-api/blob/main/main.go"
    click A2 "https://github.com/leoliscanoa/go-rest-api/blob/main/routes/routes.go"
    click B1 "https://github.com/leoliscanoa/go-rest-api/blob/main/middleware/basic.middleware.go"
    click C1 "https://github.com/leoliscanoa/go-rest-api/blob/main/controller/user.controller.go"
    click D1 "https://github.com/leoliscanoa/go-rest-api/blob/main/service/user.service.go"
    click D2 "https://github.com/leoliscanoa/go-rest-api/blob/main/service/user.service.implement.go"
    click E1 "https://github.com/leoliscanoa/go-rest-api/blob/main/repository/user.repository.go"
    click E2 "https://github.com/leoliscanoa/go-rest-api/blob/main/repository/user.repository.implement.go"
    click I1 "https://github.com/leoliscanoa/go-rest-api/blob/main/model/user.model.go"
    click I2 "https://github.com/leoliscanoa/go-rest-api/blob/main/dto/generic.dto.go"
    click G1 "https://github.com/leoliscanoa/go-rest-api/blob/main/config/database.config.go"
    click G2 "https://github.com/leoliscanoa/go-rest-api/blob/main/config/mock/database.mock.go"
    click H1 "https://github.com/leoliscanoa/go-rest-api/blob/main/docs/swagger.json"
    click H2 "https://github.com/leoliscanoa/go-rest-api/blob/main/docs/swagger.yaml"
    click H3 "https://github.com/leoliscanoa/go-rest-api/blob/main/docs/docs.go"
    click J1 "https://github.com/leoliscanoa/go-rest-api/blob/main/helper/response.helper.go"
```
