Documentation
¶
Overview ¶
Package router provides a flexible URI routing system with support for dynamic parameters and pattern matching.
Index ¶
- Variables
- type Handler
- type HandlerFunc
- type Mux
- func (m *Mux[T]) Execute(ctx context.Context, rawURI string) (T, error)
- func (m *Mux[T]) Handle(uri string, h Handler[T]) error
- func (m *Mux[T]) HandleFunc(uri string, f func(context.Context, *Request) (T, error)) error
- func (m *Mux[T]) SetNotFoundHandler(h Handler[T])
- func (m *Mux[T]) SetNotFoundHandlerFunc(f func(ctx context.Context, req *Request) (T, error))
- type Request
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("route not found")
ErrNotFound is returned when no matching route is found.
Functions ¶
This section is empty.
Types ¶
type HandlerFunc ¶
HandlerFunc is a function type that implements Handler[T].
type Mux ¶
type Mux[T any] struct { // contains filtered or unexported fields }
Mux is a request multiplexer that matches incoming requests against registered patterns and calls the corresponding handler.
func (*Mux[T]) Execute ¶
Execute processes an incoming request URI and calls the appropriate handler. It returns ErrNotFound if no matching route is found and no notFoundHandler is set.
func (*Mux[T]) Handle ¶
Handle registers a new route with a handler. uri is a URI string that will be matched against registered routes. uri can contains dynamic parameters like {param} in path and host. uri can contains query parameters like ?key=value. for example, "http://example.com/users/{id}" is a valid uri.
func (*Mux[T]) HandleFunc ¶
HandleFunc registers a new route with a handler function.
func (*Mux[T]) SetNotFoundHandler ¶
SetNotFoundHandler sets the handler to be called when no matching route is found.
type Request ¶
type Request struct {
// Query contains the query parameters from the URL.
Query map[string]string
// Params contains the dynamic parameters extracted from the URL path and host.
Params map[string]string
}
Request represents an incoming request with query parameters and path parameters.