muxr

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 11, 2024 License: MIT Imports: 4 Imported by: 1

README

muxr

go.dev reference License

A simple wrapper around the new Go 1.22 muxer. It decorates the net/http muxer with the following:

  1. Middleware functionality.
  2. Trailing slash footgun protection.
  3. Subrouting.
  4. HTTP methods as Go methods such as muxer.Post("/hello", helloHandler) instead of muxer.Handle("POST /hello", helloHandler).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware describes the signature of an http handler middleware.

type Router

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

Router wraps the net/http ServeMux with helpre methods.

func NewRouter

func NewRouter() *Router

NewRouter returns a ready ServeMux wrapper.

func (*Router) Connect

func (r *Router) Connect(path string, hf http.HandlerFunc)

Connect register a handler func under the given path for the http method CONNECT.

func (*Router) Delete

func (r *Router) Delete(path string, hf http.HandlerFunc)

Delete register a handler func under the given path for the http method DELETE.

func (*Router) Get

func (r *Router) Get(path string, hf http.HandlerFunc)

Get register a handler func under the given path for the http method GET.

func (*Router) Handle

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

Handle registers the given handler to the given path.

func (*Router) HandleFunc added in v0.0.1

func (r *Router) HandleFunc(path string, hf http.HandlerFunc)

Handle registers the given handler to the given path.

func (*Router) HandleMethod added in v0.0.1

func (r *Router) HandleMethod(method, path string, handler http.Handler)

HandleMethod registers the given handler to the given method and path.

func (*Router) Handler

func (r *Router) Handler(req *http.Request) (h http.Handler, pattern string)

Handler returns the handler to use for the given request, consulting r.Method, r.Host, and r.URL.Path. It always returns a non-nil handler. If the path is not in its canonical form, the handler will be an internally-generated handler that redirects to the canonical path. If the host contains a port, it is ignored when matching handlers.

The path and host are used unchanged for CONNECT requests.

Handler also returns the registered pattern that matches the request or, in the case of internally-generated redirects, the path that will match after following the redirect.

If there is no registered handler that applies to the request, Handler returns a “page not found” handler and an empty pattern.

func (*Router) Head

func (r *Router) Head(path string, hf http.HandlerFunc)

Head register a handler func under the given path for the http method HEAD.

func (*Router) Mount

func (r *Router) Mount(pathPrefix string, h http.Handler)

Mount mounts the given path prefix to the given http handler.

func (*Router) Options

func (r *Router) Options(path string, hf http.HandlerFunc)

Options register a handler func under the given path for the http method OPTIONS.

func (*Router) Patch

func (r *Router) Patch(path string, hf http.HandlerFunc)

Patch register a handler func under the given path for the http method PATCH.

func (*Router) Post

func (r *Router) Post(path string, hf http.HandlerFunc)

Post register a handler func under the given path for the http method POST.

func (*Router) Put

func (r *Router) Put(path string, hf http.HandlerFunc)

Put register a handler func under the given path for the http method PUT.

func (*Router) ServeHTTP

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

func (*Router) Trace

func (r *Router) Trace(path string, hf http.HandlerFunc)

Trace register a handler func under the given path for the http method TRACE.

func (*Router) Use

func (r *Router) Use(mws ...Middleware)

Use applies the given middleware in the given order

Jump to

Keyboard shortcuts

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