chirouter

package
v0.2.68 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package chirouter provides instrumentation for chi.Router.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func PathToURLValues

func PathToURLValues(r *http.Request) (url.Values, error)

PathToURLValues is a decoder function for parameters in path.

Example
// Instantiate decoder factory with gorillamux.PathToURLValues.
// Single factory can be used to create multiple request decoders.
decoderFactory := request.NewDecoderFactory()
decoderFactory.ApplyDefaults = true
decoderFactory.SetDecoderFunc(rest.ParamInPath, chirouter.PathToURLValues)

// Define request structure for your HTTP handler.
type myRequest struct {
	Query1    int     `query:"query1"`
	Path1     string  `path:"path1"`
	Path2     int     `path:"path2"`
	Header1   float64 `header:"X-Header-1"`
	FormData1 bool    `formData:"formData1"`
	FormData2 string  `formData:"formData2"`
}

// Create decoder for that request structure.
dec := decoderFactory.MakeDecoder(http.MethodPost, myRequest{}, nil)

router := chi.NewRouter()

// Now in router handler you can decode *http.Request into a Go structure.
router.Handle("/foo/{path1}/bar/{path2}", http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
	var in myRequest

	_ = dec.Decode(r, &in, nil)

	fmt.Printf("%+v\n", in)
}))

// Serving example URL.
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPost, "/foo/abc/bar/123?query1=321",
	bytes.NewBufferString("formData1=true&formData2=def"))

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("X-Header-1", "1.23")

router.ServeHTTP(w, req)
Output:

{Query1:321 Path1:abc Path2:123 Header1:1.23 FormData1:true FormData2:def}

Types

type Wrapper

type Wrapper struct {
	chi.Router
	// contains filtered or unexported fields
}

Wrapper wraps chi.Router to enable unwrappable handlers in middlewares.

Middlewares can call nethttp.HandlerAs to inspect wrapped handlers.

func NewWrapper

func NewWrapper(r chi.Router) *Wrapper

NewWrapper creates router wrapper to upgrade middlewares processing.

func (*Wrapper) Connect

func (r *Wrapper) Connect(pattern string, handlerFn http.HandlerFunc)

Connect adds the route `pattern` that matches a CONNECT http method to execute the `handlerFn` http.HandlerFunc.

func (*Wrapper) Delete

func (r *Wrapper) Delete(pattern string, handlerFn http.HandlerFunc)

Delete adds the route `pattern` that matches a DELETE http method to execute the `handlerFn` http.HandlerFunc.

func (*Wrapper) Get

func (r *Wrapper) Get(pattern string, handlerFn http.HandlerFunc)

Get adds the route `pattern` that matches a GET http method to execute the `handlerFn` http.HandlerFunc.

func (*Wrapper) Group

func (r *Wrapper) Group(fn func(r chi.Router)) chi.Router

Group adds a new inline-router along the current routing path, with a fresh middleware stack for the inline-router.

func (*Wrapper) Handle

func (r *Wrapper) Handle(pattern string, h http.Handler)

Handle adds routes for `basePattern` that matches all HTTP methods.

func (*Wrapper) HandlerFunc

func (r *Wrapper) HandlerFunc(h http.Handler) http.HandlerFunc

HandlerFunc prepares handler and returns its function.

Can be used as input for NotFound, MethodNotAllowed.

func (*Wrapper) Head

func (r *Wrapper) Head(pattern string, handlerFn http.HandlerFunc)

Head adds the route `pattern` that matches a HEAD http method to execute the `handlerFn` http.HandlerFunc.

func (*Wrapper) Method

func (r *Wrapper) Method(method, pattern string, h http.Handler)

Method adds routes for `basePattern` that matches the `method` HTTP method.

func (*Wrapper) MethodFunc

func (r *Wrapper) MethodFunc(method, pattern string, handlerFn http.HandlerFunc)

MethodFunc adds the route `pattern` that matches `method` http method to execute the `handlerFn` http.HandlerFunc.

func (*Wrapper) Mount

func (r *Wrapper) Mount(pattern string, h http.Handler)

Mount attaches another http.Handler along "./basePattern/*".

func (*Wrapper) Options

func (r *Wrapper) Options(pattern string, handlerFn http.HandlerFunc)

Options adds the route `pattern` that matches a OPTIONS http method to execute the `handlerFn` http.HandlerFunc.

func (*Wrapper) Patch

func (r *Wrapper) Patch(pattern string, handlerFn http.HandlerFunc)

Patch adds the route `pattern` that matches a PATCH http method to execute the `handlerFn` http.HandlerFunc.

func (*Wrapper) Post

func (r *Wrapper) Post(pattern string, handlerFn http.HandlerFunc)

Post adds the route `pattern` that matches a POST http method to execute the `handlerFn` http.HandlerFunc.

func (*Wrapper) Put

func (r *Wrapper) Put(pattern string, handlerFn http.HandlerFunc)

Put adds the route `pattern` that matches a PUT http method to execute the `handlerFn` http.HandlerFunc.

func (*Wrapper) Route

func (r *Wrapper) Route(pattern string, fn func(r chi.Router)) chi.Router

Route mounts a sub-router along a `basePattern` string.

func (*Wrapper) Trace

func (r *Wrapper) Trace(pattern string, handlerFn http.HandlerFunc)

Trace adds the route `pattern` that matches a TRACE http method to execute the `handlerFn` http.HandlerFunc.

func (*Wrapper) Use

func (r *Wrapper) Use(middlewares ...func(http.Handler) http.Handler)

Use appends one of more middlewares onto the Router stack.

func (Wrapper) With

func (r Wrapper) With(middlewares ...func(http.Handler) http.Handler) chi.Router

With adds inline middlewares for an endpoint handler.

func (*Wrapper) Wrap

func (r *Wrapper) Wrap(wraps ...func(handler http.Handler) http.Handler)

Wrap appends one or more wrappers that will be applied to handler before adding to Router. It is different from middleware in the sense that it is handler-centric, rather than request-centric. Wraps are invoked once for each added handler, they are not invoked for http requests. Wraps can leverage nethttp.HandlerAs to inspect and access deeper layers. For most cases Wrap can be safely used instead of Use, Use is mandatory for middlewares that affect routing (such as middleware.StripSlashes for example).

Jump to

Keyboard shortcuts

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