# elrojo/go-vault architecture

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

## Overview

##### Main Components
- API Service: The core service entry point is the Go application (main.go) which initializes the API server. The “api” folder contains handler logic (api.go, vault_handlers.go) that sets up endpoints (such as /vault/init and /vault/secret).
- Controllers: Organized under the “controllers/vault” directory, these files (vault.go, vault_config.go, vault_types.go) encapsulate core business logic for handling vault operations like initialization, configuration, and secret mapping.
- Utility Layer: Located in “internal/utility”, this layer contains helper functions (logger, pwgen, validate_request_fields, etc.) for tasks such as logging, secure password generation, and request validation.
- External Vault Service: The system interacts with HashiCorp Vault (not part of this project’s codebase but a key external dependency). The README describes how to authenticate and communicate with Vault via its HTTP API.
- Infrastructure & CI/CD: The Dockerfile and docker-compose.yml outline containerization and orchestration for both the API and Vault. GitHub workflows (in .github/workflows) define CI/CD pipelines for PR and push events.

##### Relationships and Interactions
- The API Layer (main.go → api handlers) receives HTTP requests.
- Controllers process business logic and delegate calls to the Vault client (via the vault-client-go package) and manage the vault’s “flat-file” configuration.
- Utility functions support both controllers and API by providing logging, request validation, and secure password generation.
- The Docker configuration ensures the API server runs in a containerized environment and communicates with a separate Vault container instance.
- GitHub workflows trigger builds, tests, and deployments as code is pushed or pull requests are made.

##### Architectural Patterns and Technologies
- Separation of Concerns: Clear division between API handlers, business logic (controllers), and supporting utilities.
- Dependency Injection / Interface Abstraction: As shown in the vault_test.go, mocking and dependency injection are used to facilitate testing.
- Containerization: Docker and docker-compose facilitate consistent deployment environments.
- External Service Integration: Use of the vault-client-go package ensures smooth CRUD operations with HashiCorp Vault.
- RESTful API Design: The API documentation shows well-structured endpoints and standardized request/response formats.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Request
    Client["HTTP Request"]:::client

    %% API Service Layer
    subgraph "API Service"
        A1["main.go"]:::api
        A2["api/api.go"]:::api
        A3["api/vault_handlers.go"]:::api
        A4["api/types.go"]:::api
    end

    %% Controllers Layer
    subgraph "Vault Controllers"
        B1["vault.go"]:::controller
        B2["vault_config.go"]:::controller
        B3["vault_types.go"]:::controller
    end

    %% Utility Layer
    subgraph "Utility Functions"
        C1["Logger, PWGen, Validator"]:::utility
    end

    %% External Vault Service
    External["HashiCorp Vault (Port 8200)"]:::vault

    %% Containerization Boundary
    subgraph "Containerization (Docker)"
        E1["API Server Container"]:::docker
        E2["Vault Container"]:::docker
    end

    %% CI/CD Pipeline
    subgraph "CI/CD Pipeline"
        D1["Dockerfile"]:::ci
        D2["docker-compose.yml"]:::ci
        D3["GitHub Workflows"]:::ci
    end

    %% Connections and Data Flow
    Client -->|"HTTP_Request"| A1
    A1 -->|"initializes"| A2
    A2 -->|"sets_up_endpoints"| A3
    A3 -->|"defines_API_types"| A4
    A3 -->|"delegates_vault_operations"| B1
    B1 -->|"configures_vault"| B2
    B1 -->|"maps_vault_types"| B3
    A2 -->|"logs_validates"| C1
    B1 -->|"calls_Vault_API"| External
    B1 -->|"uses"| C1

    %% Containerization relations
    A1 ---|"runs_in"| E1
    A2 --- E1
    A3 --- E1
    A4 --- E1
    External ---|"deployed_in"| E2

    %% CI/CD triggering container deployment
    D1 -->|"builds_image"| E1
    D2 -->|"orchestrates"| E1
    D3 -->|"deploys_API"| E1
    D3 -->|"deploys_Vault"| E2

    %% Click Events for API Service
    click A1 "https://github.com/elrojo/go-vault/blob/main/main.go"
    click A2 "https://github.com/elrojo/go-vault/blob/main/api/api.go"
    click A3 "https://github.com/elrojo/go-vault/blob/main/api/vault_handlers.go"
    click A4 "https://github.com/elrojo/go-vault/blob/main/api/types.go"

    %% Click Events for Controllers
    click B1 "https://github.com/elrojo/go-vault/blob/main/controllers/vault/vault.go"
    click B2 "https://github.com/elrojo/go-vault/blob/main/controllers/vault/vault_config.go"
    click B3 "https://github.com/elrojo/go-vault/blob/main/controllers/vault/vault_types.go"

    %% Click Event for Utility Layer
    click C1 "https://github.com/elrojo/go-vault/tree/main/internal/utility/"

    %% Click Events for CI/CD
    click D1 "https://github.com/elrojo/go-vault/tree/main/Dockerfile"
    click D2 "https://github.com/elrojo/go-vault/blob/main/docker-compose.yml"
    click D3 "https://github.com/elrojo/go-vault/tree/main/.github/workflows/"

    %% Styles for Components
    classDef api fill:#bbf,stroke:#333,stroke-width:1px;
    classDef controller fill:#f9c,stroke:#333,stroke-width:1px;
    classDef utility fill:#cfc,stroke:#333,stroke-width:1px;
    classDef vault fill:#fc9,stroke:#333,stroke-width:1px;
    classDef ci fill:#cff,stroke:#333,stroke-width:1px;
    classDef docker fill:#fdd,stroke:#333,stroke-width:1px;
    classDef client fill:#dfd,stroke:#333,stroke-width:1px;
```
