# go-chi/chi architecture

> Architecture diagram of the GitHub repository go-chi/chi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/go-chi/chi
- Repository: https://github.com/go-chi/chi

## Overview

chi is a lightweight Go HTTP router for composing and dispatching service routes, middleware, and subrouters. In a typical request, an HTTP client reaches a service’s registered routes; the router matches the method and path using its radix tree, builds request-scoped route context, and invokes the handler through middleware. The REST, static-file, versioning, resource, and request-limit examples illustrate distinct ways to use it. The core and middleware internals are represented separately. Some example implementations are unsampled; their documented or indexed connections are shown only where evidence supports them.

Main entry points: [`chi.go`](https://github.com/go-chi/chi/blob/master/chi.go) (Router interface), [`_examples/versions/main.go`](https://github.com/go-chi/chi/blob/master/_examples/versions/main.go) (Versioned API setup), [`mux.go`](https://github.com/go-chi/chi/blob/master/mux.go) (Mux dispatch), [`middleware`](https://github.com/go-chi/chi/tree/master/middleware) (Middleware package)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Router core"]
  node_chiapi["Router interface<br/>[chi.go]"]
  node_mux["Mux dispatch<br/>[mux.go]"]
  node_radix["Radix route tree<br/>[tree.go]"]
  node_routecontext["Request context<br/>[context.go]"]
  node_handlerchain["Handler chain<br/>[chain.go]"]
end

subgraph group_middleware["Middleware"]
  node_middlewarelib["Middleware package"]
end

subgraph group_examples["Service examples"]
  node_restapp["REST service setup<br/>[main.go]"]
  node_resthandlers["Article handlers<br/>[main.go]"]
  node_restdata[("Mock article data<br/>[main.go]")]
  node_staticfiles["Static file example<br/>[main.go]"]
  node_todos["Resource subrouters<br/>[main.go]"]
  node_todoimpl["Todo handlers<br/>[todos.go]"]
  node_userimpl["User handlers<br/>[users.go]"]
  node_limits["Request limits example<br/>[main.go]"]
  node_graceful["Graceful shutdown example<br/>[main.go]"]
  node_custommethods["Custom methods example<br/>[main.go]"]
end

subgraph group_versioning["API versioning"]
  node_versionapp["Versioned API setup<br/>[main.go]"]
  node_versiondata["Version data model"]
  node_presenters["Version presenters"]
end

node_client(("HTTP client"))
node_httpserver(("HTTP server"))

node_client -->|"sends requests"| node_httpserver
node_restapp -->|"creates router"| node_chiapi
node_restapp -->|"registers middleware"| node_middlewarelib
node_restapp -->|"registers routes"| node_resthandlers
node_resthandlers -->|"reads and writes"| node_restdata
node_httpserver -->|"serves handler"| node_chiapi
node_chiapi -->|"provides router"| node_mux
node_mux -->|"matches routes"| node_radix
node_mux -.->|"sets route context"| node_routecontext
node_mux -.->|"runs handlers"| node_handlerchain
node_handlerchain -.->|"composes middleware"| node_middlewarelib
node_staticfiles -->|"registers file routes"| node_chiapi
node_todos -->|"mounts subrouters"| node_chiapi
node_todos -.->|"uses routes"| node_todoimpl
node_todos -.->|"uses routes"| node_userimpl
node_limits -->|"registers routes"| node_chiapi
node_limits -->|"uses limits"| node_middlewarelib
node_graceful -->|"creates router"| node_chiapi
node_custommethods -->|"registers methods"| node_chiapi
node_versionapp -->|"creates routers"| node_chiapi
node_versionapp -->|"registers middleware"| node_middlewarelib
node_versionapp -->|"loads article data"| node_versiondata
node_versionapp -->|"selects presenters"| node_presenters
node_presenters -->|"formats model"| node_versiondata

click node_restapp "https://github.com/go-chi/chi/blob/master/_examples/rest/main.go"
click node_resthandlers "https://github.com/go-chi/chi/blob/master/_examples/rest/main.go"
click node_restdata "https://github.com/go-chi/chi/blob/master/_examples/rest/main.go"
click node_staticfiles "https://github.com/go-chi/chi/blob/master/_examples/fileserver/main.go"
click node_todos "https://github.com/go-chi/chi/blob/master/_examples/todos-resource/main.go"
click node_todoimpl "https://github.com/go-chi/chi/blob/master/_examples/todos-resource/todos.go"
click node_userimpl "https://github.com/go-chi/chi/blob/master/_examples/todos-resource/users.go"
click node_limits "https://github.com/go-chi/chi/blob/master/_examples/limits/main.go"
click node_graceful "https://github.com/go-chi/chi/blob/master/_examples/graceful/main.go"
click node_custommethods "https://github.com/go-chi/chi/blob/master/_examples/custom-method/main.go"
click node_versionapp "https://github.com/go-chi/chi/blob/master/_examples/versions/main.go"
click node_versiondata "https://github.com/go-chi/chi/tree/master/_examples/versions/data"
click node_presenters "https://github.com/go-chi/chi/tree/master/_examples/versions/presenter"
click node_chiapi "https://github.com/go-chi/chi/blob/master/chi.go"
click node_mux "https://github.com/go-chi/chi/blob/master/mux.go"
click node_radix "https://github.com/go-chi/chi/blob/master/tree.go"
click node_routecontext "https://github.com/go-chi/chi/blob/master/context.go"
click node_handlerchain "https://github.com/go-chi/chi/blob/master/chain.go"
click node_middlewarelib "https://github.com/go-chi/chi/tree/master/middleware"

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_chiapi,node_mux,node_radix,node_routecontext,node_handlerchain,node_client toneBlue
class node_middlewarelib toneAmber
class node_restapp,node_resthandlers,node_restdata,node_staticfiles,node_todos,node_todoimpl,node_userimpl,node_limits,node_graceful,node_custommethods,node_httpserver toneMint
class node_versionapp,node_versiondata,node_presenters toneRose
```

## Components

### Router core

- [Router interface](https://github.com/go-chi/chi/blob/master/chi.go): `chi.go`
- [Mux dispatch](https://github.com/go-chi/chi/blob/master/mux.go): `mux.go`
- [Radix route tree](https://github.com/go-chi/chi/blob/master/tree.go): `tree.go`
- [Request context](https://github.com/go-chi/chi/blob/master/context.go): `context.go`
- [Handler chain](https://github.com/go-chi/chi/blob/master/chain.go): `chain.go`

### Middleware

- [Middleware package](https://github.com/go-chi/chi/tree/master/middleware): `middleware`

### Service examples

- [REST service setup](https://github.com/go-chi/chi/blob/master/_examples/rest/main.go): `_examples/rest/main.go`
- [Article handlers](https://github.com/go-chi/chi/blob/master/_examples/rest/main.go): `_examples/rest/main.go`
- [Mock article data](https://github.com/go-chi/chi/blob/master/_examples/rest/main.go): `_examples/rest/main.go`
- [Static file example](https://github.com/go-chi/chi/blob/master/_examples/fileserver/main.go): `_examples/fileserver/main.go`
- [Resource subrouters](https://github.com/go-chi/chi/blob/master/_examples/todos-resource/main.go): `_examples/todos-resource/main.go`
- [Todo handlers](https://github.com/go-chi/chi/blob/master/_examples/todos-resource/todos.go): `_examples/todos-resource/todos.go`
- [User handlers](https://github.com/go-chi/chi/blob/master/_examples/todos-resource/users.go): `_examples/todos-resource/users.go`
- [Request limits example](https://github.com/go-chi/chi/blob/master/_examples/limits/main.go): `_examples/limits/main.go`
- [Graceful shutdown example](https://github.com/go-chi/chi/blob/master/_examples/graceful/main.go): `_examples/graceful/main.go`
- [Custom methods example](https://github.com/go-chi/chi/blob/master/_examples/custom-method/main.go): `_examples/custom-method/main.go`

### API versioning

- [Versioned API setup](https://github.com/go-chi/chi/blob/master/_examples/versions/main.go): `_examples/versions/main.go`
- [Version data model](https://github.com/go-chi/chi/tree/master/_examples/versions/data): `_examples/versions/data`
- [Version presenters](https://github.com/go-chi/chi/tree/master/_examples/versions/presenter): `_examples/versions/presenter`

### Other components

- **HTTP client**
- **HTTP server**

## Connections

- HTTP client → HTTP server: sends requests
- REST service setup → Router interface: creates router (evidence: `_examples/rest/main.go`)
- REST service setup → Middleware package: registers middleware (evidence: `_examples/rest/main.go`)
- REST service setup → Article handlers: registers routes (evidence: `_examples/rest/main.go`)
- Article handlers → Mock article data: reads and writes (evidence: `_examples/rest/main.go`)
- HTTP server → Router interface: serves handler
- Router interface → Mux dispatch: provides router
- Mux dispatch → Radix route tree: matches routes
- Mux dispatch → Request context: sets route context
- Mux dispatch → Handler chain: runs handlers
- Handler chain → Middleware package: composes middleware
- Static file example → Router interface: registers file routes (evidence: `_examples/fileserver/main.go`)
- Resource subrouters → Router interface: mounts subrouters (evidence: `_examples/todos-resource/main.go`)
- Resource subrouters → Todo handlers: uses routes
- Resource subrouters → User handlers: uses routes
- Request limits example → Router interface: registers routes (evidence: `_examples/limits/main.go`)
- Request limits example → Middleware package: uses limits (evidence: `_examples/limits/main.go`)
- Graceful shutdown example → Router interface: creates router (evidence: `_examples/graceful/main.go`)
- Custom methods example → Router interface: registers methods (evidence: `_examples/custom-method/main.go`)
- Versioned API setup → Router interface: creates routers (evidence: `_examples/versions/main.go`)
- Versioned API setup → Middleware package: registers middleware (evidence: `_examples/versions/main.go`)
- Versioned API setup → Version data model: loads article data (evidence: `_examples/versions/main.go`)
- Versioned API setup → Version presenters: selects presenters (evidence: `_examples/versions/main.go`)
- Version presenters → Version data model: formats model (evidence: `_examples/versions/presenter/v3/article.go`)
