Documentation
¶
Index ¶
- Constants
- Variables
- func ChainHandlers(lastHandler http.Handler, handlers ...func(http.Handler) http.Handler) http.Handler
- func RegisterRoute(fullPath, pattern string, logger *slog.Logger)
- func SplitPattern(pattern string) (string, string, error)
- type Router
- func (r *Router) ExactHandleFunc(pattern string, handler http.HandlerFunc, ...)
- func (r *Router) FullPath() string
- func (r *Router) GetMiddlewares() []func(http.Handler) http.Handler
- func (r *Router) HandleFunc(pattern string, handler http.HandlerFunc, ...)
- func (r *Router) Handler() http.Handler
- func (r *Router) Logger() *slog.Logger
- func (r *Router) Method() string
- func (r *Router) Mode() *goflagsmode.Flag
- func (r *Router) Mux() *http.ServeMux
- func (r *Router) NewGroup(pattern string, middlewares ...func(next http.Handler) http.Handler) RouterWrapper
- func (r *Router) Pattern() string
- func (r *Router) RegisterExactRoute(pattern string, handler http.HandlerFunc, ...)
- func (r *Router) RegisterGroup(router RouterWrapper)
- func (r *Router) RegisterHandler(pattern string, handler http.Handler)
- func (r *Router) RegisterRoute(pattern string, handler http.HandlerFunc, ...)
- func (r *Router) RelativePath() string
- func (r *Router) ServeStaticFiles(pattern, path string)
- type RouterWrapper
Constants ¶
const (
ErrNilMiddleware = "%s: middleware at index %d cannot be nil"
)
Variables ¶
var ( ErrNilRouter = errors.New("router cannot be nil") ErrEmptyPattern = errors.New("pattern cannot be empty") )
Functions ¶
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 RegisterRoute ¶ added in v0.10.0
RegisterRoute registers a route
Parameters:
- fullPath: The full path of the route
- pattern: The pattern of the route
- logger: The logger
Types ¶
type Router ¶ added in v0.2.2
type Router struct {
// contains filtered or unexported fields
}
Router is the route group struct
func (*Router) ExactHandleFunc ¶ added in v0.7.24
func (r *Router) ExactHandleFunc( pattern string, handler http.HandlerFunc, middlewares ...func(http.Handler) http.Handler, )
ExactHandleFunc 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) FullPath ¶ added in v0.7.22
FullPath returns the full path
Returns:
- string: The full path
func (*Router) GetMiddlewares ¶ added in v0.7.10
GetMiddlewares returns the middlewares
Returns:
- []func(http.Handler) http.Handler: The middlewares
func (*Router) HandleFunc ¶ added in v0.2.2
func (r *Router) HandleFunc( pattern string, handler http.HandlerFunc, middlewares ...func(http.Handler) http.Handler, )
HandleFunc 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) Handler ¶ added in v0.2.3
Handler returns the first handler
Returns:
- http.Handler: The first handler
func (*Router) Logger ¶ added in v0.9.15
Logger returns the logger
Returns:
- *slog.Logger: The logger
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
Mux returns the multiplexer
Returns:
- *http.ServeMux: The multiplexer
func (*Router) NewGroup ¶ added in v0.4.2
func (r *Router) NewGroup( pattern string, middlewares ...func(next http.Handler) http.Handler, ) RouterWrapper
NewGroup 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
func (*Router) RegisterExactRoute ¶ added in v0.7.24
func (r *Router) RegisterExactRoute( pattern string, handler http.HandlerFunc, middlewares ...func(http.Handler) http.Handler, )
RegisterExactRoute registers a new route with a path, the handler function and the middlewares. This matches the exact path
Parameters:
- pattern: The pattern of the route
- handler: The handler function
- middlewares: The middlewares to apply to the route
func (*Router) RegisterGroup ¶ added in v0.4.2
func (r *Router) RegisterGroup(router RouterWrapper)
RegisterGroup registers a new router group with a path and a router
Parameters:
- router: The router group
func (*Router) RegisterHandler ¶ added in v0.2.2
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) RegisterRoute ¶ added in v0.2.2
func (r *Router) RegisterRoute( pattern string, handler http.HandlerFunc, middlewares ...func(http.Handler) http.Handler, )
RegisterRoute registers a new route with a path, the handler function and the middlewares. This does not match the exact path
Parameters:
- pattern: The pattern of the route
- handler: The handler function
- middlewares: The middlewares to apply to the route
func (*Router) RelativePath ¶ added in v0.7.22
RelativePath returns the relative path
Returns:
- string: The relative path
func (*Router) ServeStaticFiles ¶ added in v0.9.11
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
HandleFunc(
pattern string,
handler http.HandlerFunc,
middlewares ...func(next http.Handler) http.Handler,
)
ExactHandleFunc(
pattern string,
handler http.HandlerFunc,
middlewares ...func(next http.Handler) http.Handler,
)
RegisterRoute(
pattern string,
handler http.HandlerFunc,
middlewares ...func(next http.Handler) http.Handler,
)
RegisterExactRoute(
pattern string,
handler http.HandlerFunc,
middlewares ...func(next http.Handler) http.Handler,
)
RegisterHandler(pattern string, handler http.Handler)
NewGroup(
pattern string,
middlewares ...func(next http.Handler) http.Handler,
) RouterWrapper
RegisterGroup(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, logger *slog.Logger, middlewares ...func(next http.Handler) http.Handler, ) (RouterWrapper, error)
NewBaseRouter creates a new base router
Parameters:
- mode: The flag mode
- logger: The logger
- middlewares: The middlewares to apply to the router
Returns:
- RouterWrapper: The router
- error: The error if any
func NewGroup ¶
func NewGroup( baseRouter RouterWrapper, pattern string, middlewares ...func(next http.Handler) http.Handler, ) (RouterWrapper, error)
NewGroup creates a new router group
Parameters:
- baseRouter: The base router
- 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 NewRouter ¶ added in v0.2.2
func NewRouter( pattern string, mode *goflagsmode.Flag, 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
- logger: The logger
- middlewares: The middlewares to apply to the router
Returns:
- RouterWrapper: The router