Help us shape the future of Go! Take the 2026 Go Developer Survey

routes

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package routes is the inventory of an application's routes (driftgate/routes.json), the routes excluded from checking (driftgate/exclusions.yaml), and coverage: which routes no catalog request reaches.

Index

Constants

View Source
const Multipart = "multipart/form-data"

Multipart is the body type of a route that takes files.

View Source
const Version = 1

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	Kind  string `json:"kind"` // route-added, route-removed, route-changed
	Route string `json:"route"`
	What  string `json:"what,omitempty"`
}

Change is a difference between two inventories.

func DiffInventories

func DiffInventories(old, cur *Inventory) []Change

DiffInventories lists routes that were added, removed or changed.

type Coverage

type Coverage struct {
	Covered   []string            // route keys reached by a request
	Excluded  map[string]string   // route key → reason
	Uncovered []string            // neither reached by a request that should succeed nor excluded
	Negative  []string            // reached only by requests that expect an error (not-found, invalid-param…); also in Uncovered
	Requests  map[string][]string // route key → request IDs
	Unknown   []string            // requests that reach no route
	Stale     []string            // exclusions that match no route
	NoBody    []string            // routes that take a body and have no sample
}

Coverage is the result of checking a catalog against the inventory.

func Check

func Check(inv *Inventory, cat *catalog.Catalog, excl *Exclusions) *Coverage

Check computes coverage.

type Exclusion

type Exclusion struct {
	Target string `yaml:"target,omitempty"` // only routes of this service; empty: every service
	Route  string `yaml:"route"`            // "METHOD /path" glob; without a method it matches every method
	Reason string `yaml:"reason"`
}

Exclusion is a route that is deliberately not checked.

type Exclusions

type Exclusions struct {
	Version int         `yaml:"version"`
	Routes  []Exclusion `yaml:"routes"`
}

Exclusions is driftgate/exclusions.yaml.

func LoadExclusions

func LoadExclusions(path string) (*Exclusions, error)

LoadExclusions reads exclusions.yaml; a missing file excludes nothing.

func (*Exclusions) Excluded

func (e *Exclusions) Excluded(r Route) *Exclusion

Excluded returns the exclusion that covers the route, if any.

func (*Exclusions) ExcludedUnit

func (e *Exclusions) ExcludedUnit(r Route, unit string) *Exclusion

ExcludedUnit returns the exclusion that covers one method of the route.

type FileField

type FileField struct {
	Field string `json:"field"`
	Kind  string `json:"kind"`
}

FileField is a field of a multipart body that takes a file, with the synthetic kind (upload.Kinds) a generated request sends in it.

type Inventory

type Inventory struct {
	Version int     `json:"version"`
	Routes  []Route `json:"routes"`
}

Inventory is driftgate/routes.json.

func LoadInventory

func LoadInventory(path string) (*Inventory, error)

LoadInventory reads routes.json; a missing file is an empty inventory.

func (*Inventory) Save

func (inv *Inventory) Save(path string) error

Save writes routes.json.

func (*Inventory) Sort

func (inv *Inventory) Sort()

Sort orders routes by path, then methods, so the file is deterministic.

type Param

type Param struct {
	Name     string
	Optional bool
	CatchAll bool
}

Param is a parameter of a route path.

type Route

type Route struct {
	Methods    []string          `json:"methods"` // "*" means any method
	Path       string            `json:"path"`
	Target     string            `json:"target,omitempty"` // the service, with several targets
	Domain     string            `json:"domain,omitempty"`
	Name       string            `json:"name,omitempty"`
	Action     string            `json:"action,omitempty"`
	File       string            `json:"file,omitempty"` // source file of the action, relative to the project root, with :line when known
	Middleware []string          `json:"middleware,omitempty"`
	Auth       bool              `json:"auth,omitempty"`  // needs an authenticated identity
	Body       json.RawMessage   `json:"body,omitempty"`  // sample request body
	Query      map[string]string `json:"query,omitempty"` // required query parameters with sample values
	BodySource string            `json:"body_source,omitempty"`
	BodyType   string            `json:"body_type,omitempty"` // media type of the body when not application/json
	Files      []FileField       `json:"files,omitempty"`     // file fields of a multipart/form-data body
	Source     string            `json:"source"`              // adapter that found it
}

Route is one route of the application. A path names its parameters in braces: {id} is required, {id?} optional, {slug*} takes the rest of the path (one or more segments) and {slug*?} the rest or nothing.

func Match

func Match(inv *Inventory, req catalog.Request) *Route

Match finds the route a request reaches: of the routes whose method and path match, the most specific one.

func (Route) HasMethod

func (r Route) HasMethod(method string) bool

HasMethod reports whether the route answers the method.

func (Route) Key

func (r Route) Key() string

Key identifies a route within its service: its methods and path.

func (Route) Label

func (r Route) Label() string

Label names a route in reports: with several services, the service first.

func (Route) Params

func (r Route) Params() []Param

Params lists the parameters of the path in order.

func (Route) Pattern

func (r Route) Pattern() *regexp.Regexp

Pattern returns a regular expression that matches the request paths of the route. A segment may hold several parameters (/files/{name}.{ext}); an optional parameter that fills a whole segment makes the segment optional, one inside a segment (user-{group?}) only its value.

func (Route) UnitOf

func (r Route) UnitOf(method string) string

UnitOf is the unit a request with this method checks.

func (Route) Units

func (r Route) Units() []string

Units lists the methods a route is checked with: each one, since they usually have separate handlers (GET and POST of a Next.js route), except that PUT and PATCH of one route are the same handler (Laravel Route::match, resource update) and count as PUT. A route that takes any method counts as GET.

Jump to

Keyboard shortcuts

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