router

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package router provides pluggable HTTP routing with path parameter support.

This package defines a flexible routing interface that supports both exact matches and path parameters. It includes a built-in implementation with colon-style path parameters and can be extended with custom routing logic.

Route Mutation: The builtin router is not thread-safe for concurrent route mutations. Register or unregister routes during startup, or guard runtime changes with your own synchronization.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BuiltinRouter

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

BuiltinRouter supports exact and param (colon/braces) patterns. Matching is deterministic: exact first, then param routes in registration order.

func NewBuiltinRouter

func NewBuiltinRouter() *BuiltinRouter

NewBuiltinRouter creates a new BuiltinRouter.

Returns:

  • *BuiltinRouter: A new BuiltinRouter instance.

func (*BuiltinRouter) Match

func (r *BuiltinRouter) Match(req *http.Request) *Matched

Match matches a request to a route.

Parameters:

  • req: The request to match.

Returns:

  • *Matched: A Matched instance if the request matches a route.

func (*BuiltinRouter) MethodsFor

func (r *BuiltinRouter) MethodsFor(path string) []string

MethodsFor returns the set of allowed methods for a given path. Provided for BuiltinRouter; adapters can implement the same method.

func (*BuiltinRouter) Register

func (r *BuiltinRouter) Register(
	method, pattern string, h http.Handler,
) error

Register registers a new route.

Parameters:

  • method: The HTTP method of the route.
  • pattern: The pattern of the route.
  • h: The handler of the route.

Returns:

  • error: An error if the route registration fails.

func (*BuiltinRouter) Unregister

func (r *BuiltinRouter) Unregister(method, pattern string) error

Unregister unregisters a route.

Parameters:

  • method: The HTTP method of the route.
  • pattern: The pattern of the route.

Returns:

  • error: An error if the route unregistration fails.

type Matched

type Matched struct {
	Handler  http.Handler
	Params   Params
	Endpoint any
}

Matched holds match result and extracted params.

type Params

type Params map[string]string

Params is a generic map for route params.

type Router

type Router interface {
	Register(method, pattern string, h http.Handler) error
	Unregister(method, pattern string) error
	Match(r *http.Request) *Matched
}

Router is the pluggable routing surface.

Jump to

Keyboard shortcuts

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