# palantir/witchcraft-go-server architecture

> Architecture diagram of the GitHub repository palantir/witchcraft-go-server, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/palantir/witchcraft-go-server
- Repository: https://github.com/palantir/witchcraft-go-server

## Overview

##### Main Components

a. Core Server Components:
- HTTP/HTTPS Server (with TLS support)
- Management Server (optional, on separate port)
- Router System (wrouter)
- Middleware Chain
- Configuration System (Install & Runtime)

b. Supporting Systems:
- Logging System
- Metrics System
- Tracing System
- Health Check System
- Diagnostic System

##### Component Relationships

**Client Requests**
- ↓
**TLS Termination**
- ↓
**Request Middleware Chain**
- ↓
**Router (wrouter)**
- ↓
**Route Middleware Chain**
- ↓
**Application Handlers**

**Supporting Systems (Connected to Main Flow):**
- Logging System → Log Files/stdout
- Metrics System → Metrics Collection
- Tracing System → Trace Collection
- Health Check System → Status Endpoints
- Configuration System → Install & Runtime Config

##### Specific Implementation Details to Highlight

a. Configuration System:
- Separation between Install Config (static) and Runtime Config (refreshable)
- File-based config sources (var/conf/install.yml, var/conf/runtime.yml)
- Encrypted config value support

b. Middleware Architecture:
- Two types of middleware (Request and Route)
- Built-in middleware components (panic recovery, logging, metrics)
- Where custom middleware can be inserted

c. Observability Stack:
- Various log types (service, request, metric, trace)
- Metrics collection and emission flow
- Trace ID propagation through the system

d. Health & Diagnostics:
- Health check endpoints (/status/*)
- Diagnostic endpoints (/debug/diagnostic/*)
- Management server separation (if enabled)

##### Key Interfaces to Document

- Server initialization interface
- Router registration interface
- Middleware interfaces
- Configuration interfaces (Install & Runtime)
- Refreshable interface for runtime config

##### Additional Elements

- Annotations for key configuration points
- Signal handling (SIGQUIT, SIGTERM, SIGINT)
- Where custom application code integrates

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        C[Client Request]
    end

    subgraph "Server Layer"
        TLS[TLS Termination]:::security
        HTTP[HTTP/HTTPS Server]:::server
        MGMT[Management Server]:::server
    end

    subgraph "Middleware Layer"
        MW[Request Middleware Chain]:::middleware
        RMW[Route Middleware Chain]:::middleware
        RL[Rate Limiter]:::middleware
    end

    subgraph "Routing Layer"
        R[Router System]:::routing
        SR[Server Routes]:::routing
    end

    subgraph "Core Components"
        AH[Application Handlers]:::core
        REST[REST Handlers]:::core
        SD[Service Discovery]:::core
    end

    subgraph "Supporting Systems"
        direction TB
        LS[Logging System]:::monitoring
        MS[Metrics System]:::monitoring
        TS[Tracing System]:::monitoring
        HC[Health Check System]:::monitoring
        DS[Diagnostic System]:::monitoring
        DH[Dependency Health]:::monitoring
    end

    subgraph "Configuration"
        direction TB
        CS[Configuration System]:::config
        IC[Install Config]:::config
        RC[Runtime Config]:::config
        RFC[Refreshable Config]:::config
    end

    C --> TLS
    TLS --> HTTP
    HTTP --> MW
    MW --> R
    R --> RMW
    RMW --> AH
    AH --> REST

    R --> SR
    HTTP --> MGMT
    MGMT --> HC
    MGMT --> DS

    MW --> RL
    AH --> SD

    CS --> IC
    CS --> RC
    RC --> RFC

    %% Component connections to supporting systems
    MW --> LS
    MW --> MS
    MW --> TS
    AH --> DH

    %% Click events for component mapping
    click R "https://github.com/palantir/witchcraft-go-server/tree/develop/wrouter"
    click CS "https://github.com/palantir/witchcraft-go-server/tree/develop/config"
    click HTTP "https://github.com/palantir/witchcraft-go-server/blob/develop/witchcraft/witchcraft.go"
    click MW "https://github.com/palantir/witchcraft-go-server/tree/develop/witchcraft/internal/middleware"
    click REST "https://github.com/palantir/witchcraft-go-server/tree/develop/rest"
    click HC "https://github.com/palantir/witchcraft-go-server/tree/develop/status"
    click DS "https://github.com/palantir/witchcraft-go-server/tree/develop/witchcraft/internal/wdebug"
    click MS "https://github.com/palantir/witchcraft-go-server/tree/develop/witchcraft/internal/metricloggers"
    click RL "https://github.com/palantir/witchcraft-go-server/tree/develop/witchcraft/ratelimit"
    click IC "https://github.com/palantir/witchcraft-go-server/tree/develop/app_example/var/conf"
    click DH "https://github.com/palantir/witchcraft-go-server/tree/develop/witchcraft/internal/dependencyhealth"
    click SR "https://github.com/palantir/witchcraft-go-server/blob/develop/witchcraft/server_routes.go"
    click MS "https://github.com/palantir/witchcraft-go-server/blob/develop/witchcraft/server_metrics.go"
    click SD "https://github.com/palantir/witchcraft-go-server/blob/develop/witchcraft/service_discovery.go"
    click RFC "https://github.com/palantir/witchcraft-go-server/tree/develop/witchcraft/refreshable"

    %% Styling
    classDef security fill:#ff6b6b
    classDef server fill:#4ecdc4
    classDef middleware fill:#45b7d1
    classDef routing fill:#96ceb4
    classDef core fill:#88d8b0
    classDef monitoring fill:#ffeead
    classDef config fill:#ff9999

    %% Legend
    subgraph Legend
        L1[Security Component]:::security
        L2[Server Component]:::server
        L3[Middleware Component]:::middleware
        L4[Routing Component]:::routing
        L5[Core Component]:::core
        L6[Monitoring Component]:::monitoring
        L7[Config Component]:::config
    end
```
