route

package
v0.14.2 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2025 License: GPL-3.0 Imports: 8 Imported by: 2

Documentation

Index

Constants

View Source
const (
	ErrNilMiddleware      = "%s: middleware at index %d cannot be nil"
	ErrNilEndpointHandler = "endpoint handler cannot be nil, pattern: %s"
	ErrNilHandlerFunc     = "handler function cannot be nil, pattern: %s"
)

Variables

View Source
var (
	ErrNilRouter         = errors.New("router cannot be nil")
	ErrEmptyPattern      = errors.New("pattern cannot be empty")
	ErrEmptyWildcard     = errors.New("wildcard cannot be empty")
	ErrWildcardNotClosed = errors.New("wildcard not closed")
)

Functions

func AddHandlersToStart added in v0.11.2

func AddHandlersToStart(
	originalHandlers *[]func(http.Handler) http.Handler,
	handlersToAdd ...func(http.Handler) http.Handler,
)

AddHandlersToStart adds a handler to the start of the handlers slice

Parameters:

  • originalHandlers: The original handlers slice
  • handlersToAdd: The handlers to be added

func AddRouter added in v0.12.0

func AddRouter(fullPath, pattern string, logger *slog.Logger)

AddRouter registers a route

Parameters:

  • fullPath: The full path of the route
  • pattern: The pattern of the route
  • logger: The logger

func ChainHandlers added in v0.5.10

func ChainHandlers(
	lastHandler http.Handler,
	handlers ...func(http.Handler) http.Handler,
) http.Handler

ChainHandlers chains the handlers

Parameters:

  • lastHandler: The last handler to be executed
  • handlers: The handlers to be chained

Returns:

  • http.Handler: The chained handler

func GetWildcards added in v0.11.2

func GetWildcards(pattern string) (parsedPattern string, wildcards []string, err error)

GetWildcards returns the wildcards from the route pattern

Parameters:

  • pattern: The route pattern

Returns:

  • string: The parsed pattern
  • []string: The wildcards
  • error: The error if any

func SetCtxQueryParametersMiddleware added in v0.11.2

func SetCtxQueryParametersMiddleware(next http.Handler) http.Handler

SetCtxQueryParametersMiddleware is the middleware to add the query parameters to the context

Parameters:

  • next: The next handler to be executed

Returns:

  • http.Handler: The middleware handler

func SetCtxWildcardsMiddleware added in v0.11.2

func SetCtxWildcardsMiddleware(
	wildcardKeys []string,
) func(next http.Handler) http.Handler

SetCtxWildcardsMiddleware is the middleware to add the wildcards to the context

Parameters:

  • wildcardKeys: The wildcard keys to be added to the context

Returns:

  • func(next http.Handler) http.Handler: The middleware handler

func SplitPattern added in v0.7.25

func SplitPattern(pattern string) (method, path string, err error)

SplitPattern returns the method and the path from the pattern

Parameters:

  • pattern: The pattern to split

Returns:

  • string: The method
  • string: The path
  • error: The error if any

Types

type Router added in v0.2.2

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

Router is the route group struct

func (*Router) AddEndpointHandler added in v0.12.0

func (r *Router) AddEndpointHandler(
	pattern string,
	handler func(w http.ResponseWriter, r *http.Request) error,
	middlewares ...func(http.Handler) http.Handler,
)

AddEndpointHandler adds a new endpoint with a path, the handler function and the middlewares

Parameters:

  • pattern: The pattern of the endpoint
  • handler: The handler function
  • middlewares: The middlewares to apply to the endpoint

func (*Router) AddExactEndpointHandler added in v0.12.0

func (r *Router) AddExactEndpointHandler(
	pattern string,
	handler func(w http.ResponseWriter, r *http.Request) error,
	middlewares ...func(next http.Handler) http.Handler,
)

AddExactEndpointHandler adds a new endpoint with a path, the handler function and the middlewares

Parameters:

  • pattern: The pattern of the endpoint
  • handler: The handler function
  • middlewares: The middlewares to apply to the endpoint

func (*Router) AddExactHandleFunc added in v0.12.0

func (r *Router) AddExactHandleFunc(
	pattern string,
	handler http.HandlerFunc,
	middlewares ...func(http.Handler) http.Handler,
)

AddExactHandleFunc registers a new route with a path, the handler function and the middlewares

Parameters:

  • pattern: The pattern of the route
  • handler: The handler function
  • middlewares: The middlewares to apply to the route

func (*Router) AddHandleFunc added in v0.12.0

func (r *Router) AddHandleFunc(
	pattern string,
	handler http.HandlerFunc,
	middlewares ...func(http.Handler) http.Handler,
)

AddHandleFunc registers a new route with a path, the handler function and the middlewares

Parameters:

  • pattern: The pattern of the route
  • handler: The handler function
  • middlewares: The middlewares to apply to the route

func (*Router) AddRouter added in v0.12.0

func (r *Router) AddRouter(router RouterWrapper)

AddRouter registers a new router group with a path and a router

Parameters:

  • router: The router group

func (*Router) FullPath added in v0.7.22

func (r *Router) FullPath() string

FullPath returns the full path

Returns:

  • string: The full path

func (*Router) GetMiddlewares added in v0.7.10

func (r *Router) GetMiddlewares() []func(http.Handler) http.Handler

GetMiddlewares returns the middlewares

Returns:

  • []func(http.Handler) http.Handler: The middlewares

func (*Router) Handler added in v0.2.3

func (r *Router) Handler() http.Handler

Handler returns the first handler

Returns:

  • http.Handler: The first handler

func (*Router) Logger added in v0.9.15

func (r *Router) Logger() *slog.Logger

Logger returns the logger

Returns:

  • *slog.Logger: The logger

func (*Router) Method added in v0.7.25

func (r *Router) Method() string

Method returns the method

Returns:

  • string: The method

func (*Router) Mode added in v0.9.15

func (r *Router) Mode() *goflagsmode.Flag

Mode returns the mode

Returns:

  • *goflagsmode.Flag: The mode

func (*Router) Mux added in v0.5.10

func (r *Router) Mux() *http.ServeMux

Mux returns the multiplexer

Returns:

  • *http.ServeMux: The multiplexer

func (*Router) NewRouter added in v0.12.0

func (r *Router) NewRouter(
	pattern string,
	middlewares ...func(next http.Handler) http.Handler,
) (RouterWrapper, error)

NewRouter creates a new router group with a path

Parameters:

  • pattern: The pattern of the group
  • middlewares: The middlewares to apply to the group

Returns:

  • RouterWrapper: The router group
  • error: The error if any

func (*Router) Pattern added in v0.7.25

func (r *Router) Pattern() string

Pattern returns the pattern

Returns:

  • string: The pattern

func (*Router) RegisterHandler added in v0.2.2

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

RegisterHandler registers a new route group with a path and a handler function

Parameters:

  • pattern: The pattern of the route group
  • handler: The handler function

func (*Router) RelativePath added in v0.7.22

func (r *Router) RelativePath() string

RelativePath returns the relative path

Returns:

  • string: The relative path

func (*Router) ServeStaticFiles added in v0.9.11

func (r *Router) ServeStaticFiles(
	pattern,
	path string,
)

ServeStaticFiles serves the static files

Parameters:

  • pattern: The pattern to serve the static files
  • path: The path to the static files

type RouterWrapper added in v0.2.2

type RouterWrapper interface {
	Handler() http.Handler
	Mux() *http.ServeMux
	GetMiddlewares() []func(http.Handler) http.Handler
	AddHandleFunc(
		pattern string,
		handler http.HandlerFunc,
		middlewares ...func(next http.Handler) http.Handler,
	)
	AddExactHandleFunc(
		pattern string,
		handler http.HandlerFunc,
		middlewares ...func(next http.Handler) http.Handler,
	)
	AddEndpointHandler(
		pattern string,
		endpointHandler func(w http.ResponseWriter, r *http.Request) error,
		middlewares ...func(next http.Handler) http.Handler,
	)
	AddExactEndpointHandler(
		pattern string,
		endpointHandler func(w http.ResponseWriter, r *http.Request) error,
		middlewares ...func(next http.Handler) http.Handler,
	)
	RegisterHandler(pattern string, handler http.Handler)
	NewRouter(
		pattern string,
		middlewares ...func(next http.Handler) http.Handler,
	) (RouterWrapper, error)
	AddRouter(router RouterWrapper)
	Pattern() string
	RelativePath() string
	FullPath() string
	Method() string
	ServeStaticFiles(pattern, path string)
	Logger() *slog.Logger
	Mode() *goflagsmode.Flag
}

RouterWrapper is the interface for the routes

func NewBaseRouter added in v0.4.2

func NewBaseRouter(
	mode *goflagsmode.Flag,
	handler gonethttphandler.Handler,
	logger *slog.Logger,
	middlewares ...func(next http.Handler) http.Handler,
) (RouterWrapper, error)

NewBaseRouter creates a new base router

Parameters:

  • mode: The flag mode
  • handler: The handler to handle the errors
  • logger: The logger
  • middlewares: The middlewares to apply to the router

Returns:

  • RouterWrapper: The router
  • error: The error if any

func NewRouter added in v0.2.2

func NewRouter(
	pattern string,
	mode *goflagsmode.Flag,
	handler gonethttphandler.Handler,
	logger *slog.Logger,
	middlewares ...func(next http.Handler) http.Handler,
) (RouterWrapper, error)

NewRouter creates a new router

Parameters:

  • pattern: The pattern of the router
  • mode: The flag mode
  • handler: The handler to handle the errors
  • logger: The logger
  • middlewares: The middlewares to apply to the router

Returns:

  • RouterWrapper: The router

Jump to

Keyboard shortcuts

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