router

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2025 License: MIT Imports: 6 Imported by: 1

Documentation

Overview

Package router provides a flexible URI routing system with support for dynamic parameters and pattern matching.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("route not found")

ErrNotFound is returned when no matching route is found.

Functions

This section is empty.

Types

type Handler

type Handler[T any] interface {
	Handle(ctx context.Context, req *Request) (T, error)
}

Handler is an interface that processes requests and returns results of type T.

type HandlerFunc

type HandlerFunc[T any] func(ctx context.Context, req *Request) (T, error)

HandlerFunc is a function type that implements Handler[T].

func (HandlerFunc[T]) Handle

func (f HandlerFunc[T]) Handle(ctx context.Context, req *Request) (T, error)

Handle implements the Handler interface for HandlerFunc.

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 NewMux

func NewMux[T any]() *Mux[T]

NewMux creates a new Mux instance.

func (*Mux[T]) Execute

func (m *Mux[T]) Execute(ctx context.Context, rawURI string) (T, error)

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

func (m *Mux[T]) Handle(uri string, h Handler[T]) error

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

func (m *Mux[T]) HandleFunc(uri string, f func(context.Context, *Request) (T, error)) error

HandleFunc registers a new route with a handler function.

func (*Mux[T]) SetNotFoundHandler

func (m *Mux[T]) SetNotFoundHandler(h Handler[T])

SetNotFoundHandler sets the handler to be called when no matching route is found.

func (*Mux[T]) SetNotFoundHandlerFunc

func (m *Mux[T]) SetNotFoundHandlerFunc(f func(ctx context.Context, req *Request) (T, error))

SetNotFoundHandlerFunc sets a function 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.

Jump to

Keyboard shortcuts

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