routing

package
v0.0.0-...-b970022 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mapper

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

Mapper is the pure-Go analogue of ActionDispatch::Routing::Mapper: the receiver of the routing DSL evaluated inside RouteSet.Draw. It threads a lexical scope (path/module/name prefixes, constraints, defaults, and the enclosing resource) through nested blocks.

func (*Mapper) Collection

func (m *Mapper) Collection(block func(*Mapper))

Collection evaluates block adding routes at the :collection level of the enclosing resource (/resource/action).

func (*Mapper) ConstraintsBlock

func (m *Mapper) ConstraintsBlock(reqs map[string]string, block func(*Mapper))

ConstraintsBlock evaluates block with added segment constraints.

func (*Mapper) Delete

func (m *Mapper) Delete(path string, opts ...Option)

Delete adds a DELETE route.

func (*Mapper) Get

func (m *Mapper) Get(path string, opts ...Option)

Get adds a GET route. See Match for option handling.

func (*Mapper) Match

func (m *Mapper) Match(path string, opts ...Option)

Match adds a route responding to the methods given by Via (or any method when none are supplied).

func (*Mapper) Member

func (m *Mapper) Member(block func(*Mapper))

Member evaluates block adding routes at the :member level of the enclosing resource (/resource/:id/action).

func (*Mapper) Namespace

func (m *Mapper) Namespace(name string, block func(*Mapper))

Namespace evaluates block under a path, module, and name prefix all equal to name (namespace :admin).

func (*Mapper) Patch

func (m *Mapper) Patch(path string, opts ...Option)

Patch adds a PATCH route.

func (*Mapper) Post

func (m *Mapper) Post(path string, opts ...Option)

Post adds a POST route.

func (*Mapper) Put

func (m *Mapper) Put(path string, opts ...Option)

Put adds a PUT route.

func (*Mapper) Resource

func (m *Mapper) Resource(name string, block func(*Mapper), opts ...ResOption)

Resource declares a singular RESTful resource (6 routes, no index, no :id).

func (*Mapper) Resources

func (m *Mapper) Resources(name string, block func(*Mapper), opts ...ResOption)

Resources declares a plural RESTful resource (7 routes). block, when non-nil, receives a mapper scoped to the resource for member/collection/nested routes.

func (*Mapper) Root

func (m *Mapper) Root(target string, opts ...Option)

Root adds the root route (GET "/") named "root".

func (*Mapper) Scope

func (m *Mapper) Scope(opts ScopeOpts, block func(*Mapper))

Scope evaluates block within an added path/module/name scope.

type MissingRouteKeys

type MissingRouteKeys struct {
	Name string
	Spec string
}

MissingRouteKeys is returned by generation when required dynamic segments have no value.

func (*MissingRouteKeys) Error

func (e *MissingRouteKeys) Error() string

type Option

type Option func(*routeOpts)

Option customises a plain route (get/post/match).

func Action

func Action(name string) Option

Action sets the action explicitly (used with Controller).

func As

func As(name string) Option

As sets the path-helper name.

func Constraints

func Constraints(reqs map[string]string) Option

Constraints sets per-segment regexp requirements (raw, unanchored sources).

func Controller

func Controller(name string) Option

Controller sets the controller explicitly (used with Action).

func Defaults

func Defaults(defs map[string]string) Option

Defaults sets default parameters merged into recognitions of the route.

func On

func On(where string) Option

On places a resource route at the :member or :collection level.

func To

func To(target string) Option

To sets the "controller#action" dispatch target.

func Via

func Via(methods ...string) Option

Via sets the HTTP methods a match route responds to.

type Recognition

type Recognition struct {
	Route      *Route
	Controller string
	Action     string
	// Params holds the merged defaults and captured path segments (including
	// "controller", "action", and, when present, "format").
	Params map[string]any
}

Recognition is the outcome of recognizing a request: the matched route and the resolved controller, action, and path parameters.

type ResOption

type ResOption func(*resConfig)

ResOption customises a resources/resource declaration.

func Except

func Except(actions ...string) ResOption

Except omits the listed RESTful actions.

func Only

func Only(actions ...string) ResOption

Only restricts the generated RESTful actions to those listed.

func PathName

func PathName(name string) ResOption

PathName overrides the URL path segment (resources :posts, path: "articles").

func ResController

func ResController(name string) ResOption

ResController overrides the controller for the resource.

func ResParam

func ResParam(name string) ResOption

ResParam overrides the member parameter name (default "id").

type Route

type Route struct {
	// Name is the path-helper base, or "" when the route adds no new helper.
	Name string
	// Verb is the upper-case HTTP method, or "" to match any method.
	Verb string
	// Spec is the raw path specification (e.g. "/posts/:id(.:format)").
	Spec string
	// Controller and Action are the dispatch target.
	Controller string
	Action     string
	// Defaults are parameters merged into every recognition of this route
	// (they include "controller" and "action").
	Defaults map[string]string
	// contains filtered or unexported fields
}

Route is one compiled routing rule: an HTTP verb, a path pattern, and the controller/action it dispatches to, plus any default parameters and a reverse-routing name (the base of its path helper, e.g. "post" for post_path).

type RouteNotFound

type RouteNotFound struct{ Name string }

RouteNotFound is returned by Path/PathArgs when no route carries the requested helper name.

func (*RouteNotFound) Error

func (e *RouteNotFound) Error() string

type RouteSet

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

RouteSet is the pure-Go analogue of ActionDispatch::Routing::RouteSet: an ordered collection of routes with reverse-routing helpers. Recognition scans routes in definition order and returns the first match, exactly like Rails.

func NewRouteSet

func NewRouteSet() *RouteSet

NewRouteSet returns an empty route set.

func (*RouteSet) Draw

func (rs *RouteSet) Draw(fn func(*Mapper)) error

Draw evaluates the routing DSL in fn against a fresh Mapper and returns the first error encountered while compiling any path pattern.

func (*RouteSet) Path

func (rs *RouteSet) Path(name string, params map[string]any) (string, error)

Path generates the path for the named route with the given parameters. Extra parameters that are not path segments become a sorted query string.

func (*RouteSet) PathArgs

func (rs *RouteSet) PathArgs(name string, args ...any) (string, error)

PathArgs generates the path for the named route from positional arguments, mapped to the route's required dynamic segments in order (post_path(1)). A trailing map[string]any argument supplies extra options (e.g. format or query parameters).

func (*RouteSet) Recognize

func (rs *RouteSet) Recognize(method, path string) (*Recognition, bool)

Recognize maps an HTTP method and path to a controller, action, and parameters, returning ok=false when nothing matches.

func (*RouteSet) Routes

func (rs *RouteSet) Routes() []*Route

Routes returns the routes in definition order. The slice is a copy.

func (*RouteSet) UrlFor

func (rs *RouteSet) UrlFor(opts map[string]any) (string, error)

UrlFor performs reverse routing from an options map containing "controller" and "action" (plus any dynamic segment values), returning the path of the first route whose target and required segments match.

type ScopeOpts

type ScopeOpts struct {
	Path        string
	Module      string
	As          string
	Constraints map[string]string
	Defaults    map[string]string
}

ScopeOpts are the prefixes and options a Scope block applies.

type UnroutableParameters

type UnroutableParameters struct {
	Controller string
	Action     string
}

UnroutableParameters is returned by UrlFor when no route matches the given controller/action.

func (*UnroutableParameters) Error

func (e *UnroutableParameters) Error() string

Jump to

Keyboard shortcuts

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