route

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownPolicy       = fmt.Errorf("unknown policy")
	ErrUnknownParam        = fmt.Errorf("unknown path param")
	ErrInvalidParamPattern = fmt.Errorf("invalid param pattern")
	ErrDuplicateRoute      = fmt.Errorf("duplicate route")
	ErrAmbiguousRoute      = fmt.Errorf("ambiguous route")
	ErrDuplicatePolicy     = fmt.Errorf("duplicate policy")
)
View Source
var (
	// UUID matches a UUID in its 8-4-4-4-12 hex form, in either letter case.
	UUID = &Pattern{expr: `[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}`}

	// Numeric matches one or more ASCII digits.
	Numeric = &Pattern{expr: `[0-9]+`}
)

Predefined patterns for Param.

Functions

This section is empty.

Types

type Exporter

type Exporter interface {
	Export(Set) error
}

type Group

type Group struct {
	// contains filtered or unexported fields
}

Group defines routes under a shared path prefix. Its middleware, policies, params and internal status apply to every route and nested group in it, whether set before or after they are defined. Paths passed to GET, POST and the other method helpers are relative to the prefix, "/" maps to the prefix itself.

Groups and routes must not be changed after DefineRoutes returns. The router keeps what was registered, while later exports would pick up the change and no longer match what the service serves.

func (*Group) DELETE

func (g *Group) DELETE(path string, handlers ...gin.HandlerFunc) *Route

DELETE defines a DELETE route at path, relative to the group prefix, served by handlers.

func (*Group) GET

func (g *Group) GET(path string, handlers ...gin.HandlerFunc) *Route

GET defines a GET route at path, relative to the group prefix, served by handlers.

func (*Group) Group

func (g *Group) Group(prefix string, mw ...gin.HandlerFunc) *Group

Group creates a nested group under prefix. mw is plain gin middleware that runs only in the service, before the param check and policies of each route.

func (*Group) HEAD

func (g *Group) HEAD(path string, handlers ...gin.HandlerFunc) *Route

HEAD defines a HEAD route at path, relative to the group prefix, served by handlers.

func (*Group) Internal

func (g *Group) Internal(prefix string) *Group

Internal creates a nested group whose routes are served but left out of route exports.

func (*Group) OPTIONS

func (g *Group) OPTIONS(path string, handlers ...gin.HandlerFunc) *Route

OPTIONS defines an OPTIONS route at path, relative to the group prefix, served by handlers.

func (*Group) PATCH

func (g *Group) PATCH(path string, handlers ...gin.HandlerFunc) *Route

PATCH defines a PATCH route at path, relative to the group prefix, served by handlers.

func (*Group) POST

func (g *Group) POST(path string, handlers ...gin.HandlerFunc) *Route

POST defines a POST route at path, relative to the group prefix, served by handlers.

func (*Group) PUT

func (g *Group) PUT(path string, handlers ...gin.HandlerFunc) *Route

PUT defines a PUT route at path, relative to the group prefix, served by handlers.

func (*Group) Param

func (g *Group) Param(name string, p *Pattern) *Group

Param constrains the named path param for the routes in the group that have it, routes without it ignore the pattern. A route's own Param for the same name takes precedence.

func (*Group) Policy

func (g *Group) Policy(names ...string) *Group

Policy applies policies registered on the module to every route in the group. Policies add up from the outermost group to the route, a repeated name keeps its first position.

type Pattern

type Pattern struct {
	// contains filtered or unexported fields
}

Pattern constrains a path param, see Param on Group and Route. It can only be built from UUID, Numeric, Regex and OneOf.

func OneOf

func OneOf(values ...string) *Pattern

OneOf is a Param pattern that matches exactly one of values. The values are matched literally, regex metacharacters in them are escaped.

func Regex

func Regex(expr string) *Pattern

Regex is a Param pattern that matches expr. expr must not be able to match '/' and must not contain '^' or '$' anchors, DefineRoutes rejects it otherwise.

func (*Pattern) Expr

func (p *Pattern) Expr() string

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

func NewRegistry

func NewRegistry() *Registry

func (*Registry) AddExporter

func (reg *Registry) AddExporter(exporter Exporter)

func (*Registry) AddPolicy

func (reg *Registry) AddPolicy(name string, mw ...gin.HandlerFunc) error

func (*Registry) DefineRoutes

func (reg *Registry) DefineRoutes(router gin.IRoutes, prefix string, fn func(*Group)) ([]Route, error)

type Route

type Route struct {
	// contains filtered or unexported fields
}

func (*Route) IsInternal

func (r *Route) IsInternal() bool

func (*Route) IsStatic

func (r *Route) IsStatic() bool

func (*Route) Method

func (r *Route) Method() string

func (*Route) Param

func (r *Route) Param(name string, p *Pattern) *Route

Param constrains the named path param of the route, overriding a group pattern for it. The param must be in the route's path.

func (*Route) Params

func (r *Route) Params() map[string]*Pattern

Params returns the patterns of the params in the route's path, resolved through its groups. Unconstrained params are not included.

func (*Route) Path

func (r *Route) Path() string

func (*Route) PathRegex

func (r *Route) PathRegex() string

PathRegex returns an anchored regex matching the route's path. A param matches its pattern, "[^/]+" when unconstrained, or ".*" for a catch-all.

func (*Route) Policies

func (r *Route) Policies() []string

Policies returns the route's policies resolved through its groups, the outermost group first and the route's own last, a repeated name keeps its first position.

func (*Route) Policy

func (r *Route) Policy(names ...string) *Route

Policy applies policies registered on the module to the route, after the policies of its groups.

type Set

type Set struct {
	Routes   []Route
	Policies []string
}

Jump to

Keyboard shortcuts

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