# arthurdotwork/heimdall architecture

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

## Overview

##### Project Overview
- Heimdall is a lightweight and extensible API Gateway written in Go. Its primary purpose is to handle HTTP routing, proxying to backend services, and managing middleware (such as logging and CORS).
- It is designed to be easily configured via a YAML file and is deployable either as a standalone binary or within a Docker container.

##### Analyzing the File Structure
- Top-Level Directories/Files:
- "cmd/heimdall": Contains the main entry point (main.go) for the application.
- "internal": Contains the core logic separated into sub-packages:
▸ config – Handles configuration parsing from YAML files.
▸ middleware – Manages the middleware chain, including registration and test files.
▸ proxy – Implements the HTTP proxy handler that forwards requests.
▸ router – Contains routing logic to match endpoints and apply middleware.
▸ server – Sets up the HTTP server and handles graceful shutdown.
- "middleware" (at root): Houses built-in middleware functions (e.g., logger and CORS).
- Configuration examples and deployment files (Dockerfile, Taskfile.yaml, GitHub workflows).

##### Key System Components & Interactions
a. Main Application Initialization:
- The main.go in cmd/heimdall bootstraps the application by loading configuration (via the internal/config package) and then initializing the server.

b. Configuration Component:
- Uses YAML-based configuration (e.g., config.example.yaml) to define gateway settings, endpoints, and middleware.
- Responsible for reading network timeouts, port settings, and middleware mappings.

c. Middleware Management:
- Built-in middleware components (e.g., logger, CORS) exist in both “middleware” (global resources) and “internal/middleware”.
- The middleware registry and chaining are handled in internal/middleware/chain.go and registry.go.
- Middleware executes in a defined order: global middlewares are applied first, then endpoint-specific ones.

d. Router and Proxy Handling:
- The router (internal/router/router.go) determines which endpoint configuration matches an incoming request, and selects the appropriate middleware chain.
- The proxy handler (internal/proxy/handler.go) forwards requests to the designated backend service.

e. Server:
- The internal/server/server.go sets up the HTTP server, wires in the middleware chain and router, and ensures graceful shutdown based on termination signals.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A("Entry Point: main.go"):::entry
    B("Configuration Loader"):::config
    C("HTTP Server"):::server
    D("Global Middleware Manager"):::globalMiddleware
    F("Router"):::router
    E1("Middleware Chain"):::middleware
    E2("Middleware Registry"):::middleware
    G("Proxy Handler"):::proxy
    H("External Backend Service"):::external
    I("Docker Deployment"):::docker

    A -->|"loadsConfig"| B
    B -->|"initializesServer"| C
    C -->|"applies"| D
    D -->|"routesRequestTo"| F
    F -->|"uses"| E1
    E1 -->|"forwardsRequestTo"| G
    G -->|"calls"| H
    E2 -.->|"registersMiddleware"| E1
    C -.->|"containerized"| I

    click A "https://github.com/arthurdotwork/heimdall/blob/master/cmd/heimdall/main.go"
    click B "https://github.com/arthurdotwork/heimdall/blob/master/internal/config/config.go"
    click C "https://github.com/arthurdotwork/heimdall/blob/master/internal/server/server.go"
    click D "https://github.com/arthurdotwork/heimdall/tree/master/middleware/"
    click E1 "https://github.com/arthurdotwork/heimdall/blob/master/internal/middleware/chain.go"
    click E2 "https://github.com/arthurdotwork/heimdall/blob/master/internal/middleware/registry.go"
    click F "https://github.com/arthurdotwork/heimdall/blob/master/internal/router/router.go"
    click G "https://github.com/arthurdotwork/heimdall/blob/master/internal/proxy/handler.go"

    classDef entry fill:#ffe066,stroke:#333,stroke-width:2px
    classDef config fill:#9fdf9f,stroke:#333,stroke-width:2px
    classDef server fill:#dfe6e9,stroke:#333,stroke-width:2px
    classDef globalMiddleware fill:#fab1a0,stroke:#333,stroke-width:2px
    classDef middleware fill:#ffdead,stroke:#333,stroke-width:2px
    classDef router fill:#74b9ff,stroke:#333,stroke-width:2px
    classDef proxy fill:#a29bfe,stroke:#333,stroke-width:2px
    classDef external fill:#ff7675,stroke:#333,stroke-width:2px
    classDef docker fill:#b2bec3,stroke:#333,stroke-width:2px
```
