router

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package router provides route grouping and method helpers on top of http.ServeMux.

It adds .Get()/.Post() method helpers, Group(prefix, ...middleware) for prefix grouping, and per-group middleware — all delegating to a single http.ServeMux underneath.

Usage:

r := router.New()
r.Use(middleware.RequestID(), middleware.Logger(logger))

r.Get("/health", healthHandler)

api := r.Group("/api/v1", authMiddleware)
api.Get("/users", listUsers)
api.Post("/users", createUser)

srv := server.New(r) // router implements http.Handler

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultErrorHandler

func DefaultErrorHandler(w http.ResponseWriter, _ *http.Request, err error)

DefaultErrorHandler writes a JSON error response matching the standard envelope format. It uses errors.As to extract *errors.Error; unrecognized errors become 500 Internal Server Error.

Types

type ErrorHandler

type ErrorHandler func(http.ResponseWriter, *http.Request, error)

ErrorHandler handles errors returned by HandlerFunc handlers.

type Group

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

Group represents a collection of routes that share a common prefix and middleware.

func (*Group) Delete

func (g *Group) Delete(pattern string, fn HandlerFunc)

Delete registers an error-returning handler for DELETE requests.

func (*Group) DeleteFunc

func (g *Group) DeleteFunc(pattern string, fn http.HandlerFunc)

DeleteFunc registers a standard http.HandlerFunc for DELETE requests.

func (*Group) Get

func (g *Group) Get(pattern string, fn HandlerFunc)

Get registers an error-returning handler for GET requests.

func (*Group) GetFunc

func (g *Group) GetFunc(pattern string, fn http.HandlerFunc)

GetFunc registers a standard http.HandlerFunc for GET requests.

func (*Group) Group

func (g *Group) Group(prefix string, mw ...middleware.Middleware) *Group

Group creates a child group with the given prefix and optional middleware.

func (*Group) Handle

func (g *Group) Handle(pattern string, handler http.Handler)

Handle registers an http.Handler for the given pattern. The pattern may include a method prefix (e.g. "GET /path").

func (*Group) HandleFunc

func (g *Group) HandleFunc(pattern string, fn http.HandlerFunc)

HandleFunc registers an http.HandlerFunc for the given pattern. The pattern may include a method prefix (e.g. "GET /path").

func (*Group) Patch

func (g *Group) Patch(pattern string, fn HandlerFunc)

Patch registers an error-returning handler for PATCH requests.

func (*Group) PatchFunc

func (g *Group) PatchFunc(pattern string, fn http.HandlerFunc)

PatchFunc registers a standard http.HandlerFunc for PATCH requests.

func (*Group) Post

func (g *Group) Post(pattern string, fn HandlerFunc)

Post registers an error-returning handler for POST requests.

func (*Group) PostFunc

func (g *Group) PostFunc(pattern string, fn http.HandlerFunc)

PostFunc registers a standard http.HandlerFunc for POST requests.

func (*Group) Put

func (g *Group) Put(pattern string, fn HandlerFunc)

Put registers an error-returning handler for PUT requests.

func (*Group) PutFunc

func (g *Group) PutFunc(pattern string, fn http.HandlerFunc)

PutFunc registers a standard http.HandlerFunc for PUT requests.

func (*Group) Use

func (g *Group) Use(mw ...middleware.Middleware)

Use appends middleware to this group. Middleware added via Use only applies to routes registered after the call.

type HandlerFunc

type HandlerFunc func(http.ResponseWriter, *http.Request) error

HandlerFunc is an HTTP handler that returns an error. Errors are handled by the router's ErrorHandler.

type Option

type Option func(*Router)

Option configures a Router.

func WithErrorHandler

func WithErrorHandler(fn ErrorHandler) Option

WithErrorHandler sets a custom error handler for the router.

type Router

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

Router is a thin wrapper around http.ServeMux that provides method helpers, route grouping, and per-group middleware.

func New

func New(opts ...Option) *Router

New creates a new Router with the given options.

func (*Router) Delete

func (r *Router) Delete(pattern string, fn HandlerFunc)

Delete registers an error-returning handler for DELETE requests.

func (*Router) DeleteFunc

func (r *Router) DeleteFunc(pattern string, fn http.HandlerFunc)

DeleteFunc registers a standard http.HandlerFunc for DELETE requests.

func (*Router) Get

func (r *Router) Get(pattern string, fn HandlerFunc)

Get registers an error-returning handler for GET requests.

func (*Router) GetFunc

func (r *Router) GetFunc(pattern string, fn http.HandlerFunc)

GetFunc registers a standard http.HandlerFunc for GET requests.

func (*Router) Group

func (r *Router) Group(prefix string, mw ...middleware.Middleware) *Group

Group creates a new route group with the given prefix and optional middleware.

func (*Router) Handle

func (r *Router) Handle(pattern string, handler http.Handler)

Handle registers an http.Handler for the given pattern.

func (*Router) HandleFunc

func (r *Router) HandleFunc(pattern string, fn http.HandlerFunc)

HandleFunc registers an http.HandlerFunc for the given pattern.

func (*Router) Patch

func (r *Router) Patch(pattern string, fn HandlerFunc)

Patch registers an error-returning handler for PATCH requests.

func (*Router) PatchFunc

func (r *Router) PatchFunc(pattern string, fn http.HandlerFunc)

PatchFunc registers a standard http.HandlerFunc for PATCH requests.

func (*Router) Post

func (r *Router) Post(pattern string, fn HandlerFunc)

Post registers an error-returning handler for POST requests.

func (*Router) PostFunc

func (r *Router) PostFunc(pattern string, fn http.HandlerFunc)

PostFunc registers a standard http.HandlerFunc for POST requests.

func (*Router) Put

func (r *Router) Put(pattern string, fn HandlerFunc)

Put registers an error-returning handler for PUT requests.

func (*Router) PutFunc

func (r *Router) PutFunc(pattern string, fn http.HandlerFunc)

PutFunc registers a standard http.HandlerFunc for PUT requests.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements http.Handler.

func (*Router) Use

func (r *Router) Use(mw ...middleware.Middleware)

Use adds middleware to the root group.

Jump to

Keyboard shortcuts

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