chi

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package chi is a router plugin for github.com/go-chi/chi/v5. It statically recognizes:

r.Get("/users/{id}", GetUser)                    // method-specific: Get/Post/Put/Patch/Delete/Head/Options
r.Handle("/health", http.HandlerFunc(HealthCheck)) // all methods, unless the pattern itself has a "METHOD " prefix
r.Method("GET", "/users/{id}", handler)            // explicit method as a compile-time-constant string
r.Route("/users", func(r chi.Router) {             // nested prefix routing, arbitrarily deep
	r.Get("/{id}", GetUser)                        // -> "/users/{id}"
})
r.Group(func(r chi.Router) { r.Get("/admin", Admin) }) // no path prefix, middleware scoping only
r.Mount("/products", productsRouter())             // see mountedRoutes: same-package, zero-arg constructor
v1 := chi.NewRouter()                              // a local router variable, configured via a same-
registerUserRoutes(v1)                             // package helper function taking it as a parameter,
r.Mount("/v1", v1)                                 // then mounted at a real prefix -- see extractRouterVarBlocks

It does not import the real chi: recognition is pure go/types path/name matching against the analyzed target's own type-checked packages, the same mechanism internal/router/nethttp uses for *http.ServeMux — gota's own build never depends on chi. Both chi's current module path ("github.com/go-chi/chi/v5") and its legacy, unversioned one ("github.com/go-chi/chi", chi v1–v4 — still common in the wild) are recognized; see chiPkgPaths.

Chi handlers are plain func(w http.ResponseWriter, r *http.Request) using ordinary encoding/json — chi introduces no response-writing idiom of its own — so this plugin pairs with the existing inference.NetHTTP() dialect unchanged.

Path parameters use the same "{name}" syntax as OpenAPI path templates; a chi-specific regex constraint ("{id:[0-9]+}") degrades to the bare "{id}" (no direct OpenAPI equivalent). A pattern containing a bare wildcard segment ("/admin/*") has no OpenAPI path-template equivalent at all and is declined entirely, not approximated.

A function taking a chi.Router/*chi.Mux parameter and registering routes on it — the canonical domain-driven Chi shape, e.g. "func RegisterHTTPEndPoints(router *chi.Mux) { router.Route("/api/v1/x", ...) }" called (often cross-package) as x.RegisterHTTPEndPoints(s.router) — is walked at the empty top-level prefix, the parameter itself acting as a chi receiver. This is correct for the overwhelmingly common real case: absolute paths on a top-level router (confirmed running this plugin against gmhafiz/go8, a large real Chi API, where every domain's register function has exactly this shape). Two same-package cases where an empty prefix would be wrong are filtered out (see routerParamSets): a function already emitted at its real prefix by extractRouterVarBlocks (a local router var configured then mounted, "v1 := chi.NewRouter(); registerUserRoutes(v1); r.Mount("/v1", v1)") is not walked again here; and a function that receives a router var mounted at a prefix through a multi-argument call the prefix can't be wired through is declined entirely rather than emitted at a silently-wrong empty prefix.

Known, deliberate v1 gaps (declined, not guessed) — found running this plugin against real, public Chi projects on GitHub, not speculated:

  • A router-parameter register function that uses RELATIVE paths and is mounted at a prefix in a DIFFERENT package — the mount site is invisible to a single-package Plugin.Extract call, so its routes surface at the empty prefix (unprefixed) rather than the real one. The same-package version of this is detected and declined (above); this cross-package sliver is the exact boundary a future all-packages pass would move.
  • Mount beyond a same-package, zero-argument constructor function or a same-block tracked variable — a cross-package constructor, or a variable that escapes the block it was declared in, falls outside what a single Plugin.Extract call over one package/block can resolve.
  • A chi.Router reached through struct embedding rather than chi's own named types directly.
  • A Route/Group callback not written as an inline closure argument (assigned to a variable first, then passed) — extracting it at the wrong prefix would be silently wrong, not just incomplete, so it's declined instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Plugin

type Plugin struct{}

func New

func New() *Plugin

func (*Plugin) Extract

func (p *Plugin) Extract(pkg *packages.Package) ([]router.Route, error)

func (*Plugin) Name

func (p *Plugin) Name() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL