router

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Route

type Route interface {
	// Handler returns the HTTP handler function (http.HandlerFunc)
	// that will be invoked when this route matches an incoming request.
	Handler() http.HandlerFunc

	// Method returns the HTTP method (e.g., "GET", "POST", "PUT", "DELETE")
	// that this route responds to.
	Method() string

	// Path returns the relative route path (e.g., "/users/{id}").
	// This is appended to the Router's Prefix when building full route paths.
	Path() string

	// Status returns true if the route is enabled and should
	// accept incoming requests; false otherwise.
	Status() bool

	// Experimental returns true if the route is experimental,
	// allowing for conditional enabling or feature flagging.
	Experimental() bool

	// Middleware returns route-level middleware functions specific
	// to this route, which will be executed after router-level middleware.
	Middleware() []func(http.Handler) http.Handler
}

Route represents a single HTTP endpoint with a method, path, handler function, status, experimental flag, and optional middleware.

It encapsulates all data necessary for routing a request to its handler with possible route-specific middleware.

func NewConnectRoute

func NewConnectRoute(path string, status, experimental bool, handler http.HandlerFunc, middleware []func(http.Handler) http.Handler, opts ...RouteWrapper) Route

NewConnectRoute creates a new CONNECT Route.

Example:

r := NewConnectRoute("/users/{id}", true, false, connectHandler, nil)

func NewDeleteRoute

func NewDeleteRoute(path string, status, experimental bool, handler http.HandlerFunc, middleware []func(http.Handler) http.Handler, opts ...RouteWrapper) Route

NewDeleteRoute creates a new DELETE Route.

Example:

r := NewDeleteRoute("/users/{id}", true, false, deleteUserHandler, nil)

func NewGetRoute

func NewGetRoute(path string, status, experimental bool, handler http.HandlerFunc, middleware []func(http.Handler) http.Handler, opts ...RouteWrapper) Route

NewGetRoute creates a new GET Route.

Example:

r := NewGetRoute("/users", true, false, usersHandler, nil)

func NewHeadRoute

func NewHeadRoute(path string, status, experimental bool, handler http.HandlerFunc, middleware []func(http.Handler) http.Handler, opts ...RouteWrapper) Route

NewHeadRoute creates a new HEAD Route.

Example:

r := NewHeadRoute("/users", true, false, headHandler, nil)

func NewOptionsRoute

func NewOptionsRoute(path string, status, experimental bool, handler http.HandlerFunc, middleware []func(http.Handler) http.Handler, opts ...RouteWrapper) Route

NewOptionsRoute creates a new OPTIONS Route.

Example:

r := NewOptionsRoute("/users", true, false, optionsHandler, nil)

func NewPatchRoute

func NewPatchRoute(path string, status, experimental bool, handler http.HandlerFunc, middleware []func(http.Handler) http.Handler, opts ...RouteWrapper) Route

NewPatchRoute creates a new PATCH Route.

Example:

r := NewPatchRoute("/users/{id}", true, false, patchUserHandler, nil)

func NewPostRoute

func NewPostRoute(path string, status, experimental bool, handler http.HandlerFunc, middleware []func(http.Handler) http.Handler, opts ...RouteWrapper) Route

NewPostRoute creates a new POST Route.

Example:

r := NewPostRoute("/users", true, false, createUserHandler, nil)

func NewPutRoute

func NewPutRoute(path string, status, experimental bool, handler http.HandlerFunc, middleware []func(http.Handler) http.Handler, opts ...RouteWrapper) Route

NewPutRoute creates a new PUT Route.

Example:

r := NewPutRoute("/users/{id}", true, false, updateUserHandler, nil)

func NewRoute

func NewRoute(method, path string, status, experimental bool, handler http.HandlerFunc, middleware []func(http.Handler) http.Handler, opts ...RouteWrapper) Route

NewRoute creates a new Route with the given HTTP method, path, status, experimental flag, handler function, and optional middleware. Additional options can be applied using RouteWrapper functions.

Example:

r := NewRoute(http.MethodGet, "/ping", true, false, pingHandler, nil)

func NewTraceRoute

func NewTraceRoute(path string, status, experimental bool, handler http.HandlerFunc, middleware []func(http.Handler) http.Handler, opts ...RouteWrapper) Route

NewTraceRoute creates a new TRACE Route.

Example:

r := NewTraceRoute("/users/{id}", true, false, traceHandler, nil)

type RouteWrapper

type RouteWrapper func(r Route) Route

RouteWrapper defines a function signature to wrap or modify a Route.

type Router

type Router interface {
	// Routes returns a slice of all registered Route instances
	// managed by this router.
	Routes() []Route

	// Status returns true if the router is active and should
	// handle requests; false otherwise.
	Status() bool

	// Prefix returns the base URL path prefix for all routes
	// in this router, e.g., "/api/v1".
	Prefix() string

	// Middleware returns the slice of middleware functions
	// applied at the router level. Middleware functions have
	// the signature func(http.Handler) http.Handler and wrap
	// the handler chain for all routes under this router.
	Middleware() []func(http.Handler) http.Handler
}

Router represents an HTTP router that manages multiple routes, their associated middleware, and a base URL prefix.

It provides methods to retrieve all registered routes, check if the router is active, get its base path prefix, and access router-level middleware.

Implementations of Router are responsible for handling request dispatching to the appropriate routes.

func NewRouter

func NewRouter(prefix string, routes []Route, status bool, middleware []func(http.Handler) http.Handler, opts ...RouterWrapper) Router

NewRouter creates a new Router with the specified prefix, routes, status, and optional middleware. Additional options can be applied using RouterWrapper functions.

Example:

r := NewRouter("/api", routes, true, []func(http.Handler) http.Handler{loggingMiddleware})

type RouterWrapper

type RouterWrapper func(r Router) Router

RouterWrapper defines a function signature to wrap or modify a Router.

Jump to

Keyboard shortcuts

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