# ares-17/docker-awakening-gateway architecture

> Architecture diagram of the GitHub repository ares-17/docker-awakening-gateway, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ares-17/docker-awakening-gateway
- Repository: https://github.com/ares-17/docker-awakening-gateway

## Overview

1. **What this service is**
   - This repository builds a single Go binary that acts as a **reverse proxy + container wake-up controller** for Docker.
   - It sits in front of application containers, matches requests by `Host`, and starts stopped containers on demand before proxying traffic through.

2. **Top-level runtime shape**
   - `main.go` is the entrypoint.
   - The core implementation lives in the `gateway/` package, which is where routing, Docker control, discovery, auth, metrics, and scheduling are coordinated.
   - The shipped image is intentionally minimal/distroless, so the operational boundary is basically “one gateway process talking to Docker and HTTP clients.”

3. **Request path / user-facing flow**
   - Incoming HTTP requests hit the gateway first.
   - If the target container is already running, the gateway proxies directly to it.
   - If the container is stopped, the gateway returns a **loading page** (`gateway/templates/loading.html`), starts the container asynchronously, probes readiness, and then lets the browser continue to the real app.
   - The docs also show live log output on the loading page, so there is an observable bootstrapping experience, not just a blind spinner.

4. **Routing and container model**
   - `gateway/server.go` and `gateway/manager.go` appear to be the main request-routing and orchestration layers.
   - Routing is host-based: each container definition has a `host` that maps to a `Host` header.
   - The system supports both **single containers** and **groups** (`gateway/group.go`) with a strategy such as round-robin, so the routing model is not one-container-per-host only.

5. **Configuration sources**
   - The gateway accepts configuration from a static YAML file (`config.yaml`) and from **Docker labels** for auto-discovery.
   - The README and `gateway/config.go` imply these sources are merged into one runtime config model.
   - Hot reload is supported via `SIGHUP`, so config changes can be applied without restarting the gateway.

6. **Docker control plane**
   - `gateway/docker.go` is the boundary to the Docker Engine API.
   - This layer is responsible for inspecting container state, starting containers, stopping idle containers, and likely resolving container networking details.
   - `gateway/discovery.go` periodically polls Docker to find label-enabled containers, so discovery is a control-plane loop separate from request handling.

7. **Wake-up orchestration and readiness**
   - `gateway/scheduler.go` manages the timing aspects of wake and idle behavior.
   - A wake sequence starts the container, then performs readiness checks using either TCP dial or an HTTP health path.
   - There is also an **idle timeout** mechanism that stops containers after inactivity, which is part of the same orchestration boundary rather than the app containers themselves.

8. **Dependencies and grouped startup**
   - Containers can declare `depends_on`, and the gateway coordinates startup order so that prerequisite containers are running before dependents are served.
   - This creates a small internal dependency graph in the gateway, which matters for architecture diagrams because “wake request” may fan out into multiple Docker starts.

9. **Admin and observability surfaces**
   - The gateway exposes admin endpoints under `/_status`, `/_status/api`, and `/_status/wake`, plus `/_metrics` for Prometheus.
   - `gateway/admin_auth.go` adds optional protection for these endpoints using none/basic/bearer auth.
   - `gateway/metrics.go` emits request, start, latency, and idle-stop metrics, so observability is built into the gateway rather than added externally.

10. **Security and network boundary**
    - The gateway is designed to run behind a TLS-terminating proxy in production; auth itself is HTTP-level and not sufficient alone.
    - `trusted_proxies` in config suggests the server is aware of forwarded client IPs and proxy chains.
    - `gateway/security_test.go` indicates security-sensitive handling around these boundaries.

11. **External dependencies**
    - The main external runtime dependency is **Docker**, via the local Docker socket or Docker API access.
    - Optional integration points include **Prometheus** scraping and a fronting reverse proxy like Nginx/Caddy/Traefik.
    - Everything else is self-contained in the gateway process; there is no separate database or message broker in the repo.

12. **Architectural diagram hint**
    - A good diagram would show three planes:
      - **Client plane:** browser/Prometheus/admin tools
      - **Gateway control plane:** config, discovery, scheduler, auth, metrics, manager
      - **Docker/app plane:** Docker daemon plus managed containers/groups
    - The key data flow is: request arrives → gateway resolves host → if sleeping, gateway starts container and probes readiness → gateway proxies to the app → idle watcher may later stop it.

Main entry points: [`gateway/server.go`](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/server.go) (HTTP server), [`gateway/config.go`](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/config.go) (Config), [`gateway/manager.go`](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/manager.go) (Manager), [`gateway/scheduler.go`](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/scheduler.go) (Scheduler)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Client plane"]
  node_browser(("Browser<br/>client"))
  node_admin(("Admin tool<br/>client"))
  node_prometheus(("Prometheus<br/>scraper"))
end

subgraph group_gateway["Gateway control plane"]
  node_main(("Main<br/>entrypoint<br/>[main.go]"))
  node_server["HTTP server<br/>ingress/router<br/>[server.go]"]
  node_manager["Manager<br/>orchestrator<br/>[manager.go]"]
  node_scheduler["Scheduler<br/>wake/idle timing<br/>[scheduler.go]"]
  node_discovery["Discovery<br/>docker polling<br/>[discovery.go]"]
  node_config["Config<br/>[config.go]"]
  node_group{{"Groups<br/>routing group<br/>[group.go]"}}
  node_auth["Admin auth<br/>endpoint guard<br/>[admin_auth.go]"]
  node_metrics["Metrics<br/>prometheus<br/>[metrics.go]"]
  node_loading["Loading page<br/>ui template<br/>[loading.html]"]
  node_status["Status UI<br/>ui template<br/>[status.html]"]
end

subgraph group_runtime["Docker/app plane"]
  node_docker["Docker API<br/>engine adapter<br/>[docker.go]"]
  node_containers[("App containers<br/>managed workloads")]
  node_daemon[("Docker daemon<br/>external runtime")]
end

node_browser -->|"HTTP request"| node_server
node_admin -->|"admin/status"| node_server
node_prometheus -->|"scrape"| node_metrics
node_main -->|"load"| node_config
node_main -->|"start"| node_server
node_server -->|"protect"| node_auth
node_server -->|"route"| node_manager
node_server -->|"render"| node_loading
node_server -->|"render"| node_status
node_server -->|"emit"| node_metrics
node_manager -->|"orchestrate"| node_scheduler
node_manager -->|"control"| node_docker
node_manager -->|"resolve"| node_group
node_manager -->|"sync"| node_discovery
node_discovery -->|"poll"| node_docker
node_scheduler -->|"start/stop"| node_docker
node_scheduler -->|"probe"| node_containers
node_docker -->|"api"| node_daemon
node_daemon -->|"manage"| node_containers
node_config -->|"configure"| node_server
node_config -->|"configure"| node_manager
node_config -->|"configure"| node_discovery
node_config -->|"configure"| node_scheduler
node_config -->|"configure"| node_auth
node_config -->|"configure"| node_metrics

click node_main "https://github.com/ares-17/docker-awakening-gateway/blob/main/main.go"
click node_server "https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/server.go"
click node_manager "https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/manager.go"
click node_scheduler "https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/scheduler.go"
click node_discovery "https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/discovery.go"
click node_docker "https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/docker.go"
click node_config "https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/config.go"
click node_group "https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/group.go"
click node_auth "https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/admin_auth.go"
click node_metrics "https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/metrics.go"
click node_loading "https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/templates/loading.html"
click node_status "https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/templates/status.html"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_browser,node_admin,node_prometheus toneBlue
class node_main,node_server,node_manager,node_scheduler,node_discovery,node_config,node_group,node_auth,node_metrics,node_loading,node_status toneAmber
class node_docker,node_containers,node_daemon toneMint
```

## Components

### Client plane

Browsers, operators, and scrapers that talk to the gateway

- **Browser**: client · End-user HTTP client reaching host-based routes
- **Admin tool**: client · Operator using status and wake endpoints
- **Prometheus**: scraper · Scrapes gateway metrics

### Gateway control plane

Single Go process that resolves hosts, wakes containers, and proxies traffic

- [Main](https://github.com/ares-17/docker-awakening-gateway/blob/main/main.go): entrypoint · Bootstraps the gateway process · `main.go`
- [HTTP server](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/server.go): ingress/router · Handles incoming requests, host routing, proxying, and admin endpoints · `gateway/server.go`
- [Manager](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/manager.go): orchestrator · Coordinates request handling, wake-up flow, and container lifecycle decisions · `gateway/manager.go`
- [Scheduler](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/scheduler.go): wake/idle timing · Runs wake sequencing, readiness probing, and idle-stop timing · `gateway/scheduler.go`
- [Discovery](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/discovery.go): docker polling · Polls Docker for label-based container discovery · `gateway/discovery.go`
- [Config](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/config.go): runtime config · Merges YAML and Docker-label configuration into one model · `gateway/config.go`
- [Groups](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/group.go): routing group · Models grouped containers and strategies such as round-robin · `gateway/group.go`
- [Admin auth](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/admin_auth.go): endpoint guard · Protects status and wake admin endpoints with optional auth · `gateway/admin_auth.go`
- [Metrics](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/metrics.go): prometheus · Emits request, wake, latency, and idle-stop metrics · `gateway/metrics.go`
- [Loading page](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/templates/loading.html): ui template · Renders the wait experience while containers wake up · `gateway/templates/loading.html`
- [Status UI](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/templates/status.html): ui template · Renders admin/status views for operational introspection · `gateway/templates/status.html`

### Docker/app plane

Docker Engine and managed application containers

- [Docker API](https://github.com/ares-17/docker-awakening-gateway/blob/main/gateway/docker.go): engine adapter · Boundary to the Docker Engine for inspect/start/stop and networking details · `gateway/docker.go`
- **App containers**: managed workloads · Application containers and groups started, probed, and stopped on demand
- **Docker daemon**: external runtime · Underlying Docker Engine accessed by the gateway

## Connections

- Browser → HTTP server: HTTP request · User traffic reaches the gateway
- Admin tool → HTTP server: admin/status · Operator calls status and wake endpoints
- Prometheus → Metrics: scrape · Metrics are scraped over HTTP
- Main → Config: load · Process startup reads runtime configuration
- Main → HTTP server: start · Entrypoint launches the HTTP gateway
- HTTP server → Admin auth: protect · Admin endpoints may require authentication
- HTTP server → Manager: route · Request handling delegates orchestration decisions
- HTTP server → Loading page: render · Sleeping targets return the loading experience
- HTTP server → Status UI: render · Status endpoints serve operational views
- HTTP server → Metrics: emit · HTTP activity contributes to metrics
- Manager → Scheduler: orchestrate · Manager hands wake and idle timing to the scheduler
- Manager → Docker API: control · Manager uses Docker operations to inspect and change state
- Manager → Groups: resolve · Grouped backends are resolved for routing and wake order
- Manager → Discovery: sync · Configured and discovered containers are reconciled
- Discovery → Docker API: poll · Discovery reads container metadata from Docker
- Scheduler → Docker API: start/stop · Wake sequences and idle stops act on Docker containers
- Scheduler → App containers: probe · Readiness checks validate application availability
- Docker API → Docker daemon: api · Adapter talks to the Docker Engine
- Docker daemon → App containers: manage · Docker runtime hosts the application workloads
- Config → HTTP server: configure · Routing, proxy, and proxy-chain behavior are configured here
- Config → Manager: configure · Wake, dependency, and grouping settings feed orchestration
- Config → Discovery: configure · Discovery relies on label and polling settings
- Config → Scheduler: configure · Idle and readiness timing come from config
- Config → Admin auth: configure · Admin authentication mode is selected from config
- Config → Metrics: configure · Metrics exposure and labels are configured
