httpmux

package
v0.17.17 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DecoderFunc

type DecoderFunc func(ctx *silverlining.Context, bag Params, data []byte) (kit.Message, error)

type Mux

type Mux struct {

	// Enables automatic redirection if the current route can't be matched but a
	// handler for the path with (without) the trailing slash exists.
	// For example if /foo/ is requested but a route only exists for /foo, the
	// client is redirected to /foo with http status code 301 for GET requests
	// and 308 for all other request methods.
	RedirectTrailingSlash bool

	// If enabled, the Mux tries to fix the current request path, if no
	// handle is registered for it.
	// First superfluous path elements like ../ or // are removed.
	// Afterwards the Mux does a case-insensitive lookup of the cleaned path.
	// If a handle can be found for this route, the Mux makes a redirection
	// to the corrected path with status code 301 for GET requests and 308 for
	// all other request methods.
	// For example /FOO and /..//Foo could be redirected to /foo.
	// RedirectTrailingSlash is independent of this option.
	RedirectFixedPath bool

	// If enabled, the Mux checks if another Method is allowed for the
	// current route, if the current request can not be routed.
	// If this is the case, the request is answered with 'Method Not Allowed'
	// and HTTP status code 405.
	// If no other Method is allowed, the request is delegated to the NotFound
	// handler.
	HandleMethodNotAllowed bool

	// If enabled, the Mux automatically replies to `OPTIONS` requests.
	// Custom OPTIONS handlers take priority over automatic replies.
	HandleOPTIONS bool

	// An optional http.Handler that is called on automatic OPTIONS requests.
	// The handler is only called if HandleOPTIONS is true and no OPTIONS
	// handler for the specific path was set.
	// The "Allowed" header is set before calling the handler.
	GlobalOPTIONS http.Handler

	// Configurable http.Handler which is called when no matching route is
	// found. If it is not set, http.NotFound is used.
	NotFound http.Handler

	// Configurable http.Handler which is called when a request
	// cannot be routed and HandleMethodNotAllowed is true.
	// If it is not set, http.Error with http.StatusMethodNotAllowed is used.
	// The "Allow" header with allowed request methods is set before the handler
	// is called.
	MethodNotAllowed http.Handler

	// Function to handle panics recovered from http handlers.
	// It should be used to generate an error page and return the http error code
	// 500 (Internal Server Error).
	// The handler can be used to keep your server from crashing because of
	// un-recovered panics.
	PanicHandler func(http.ResponseWriter, *http.Request, interface{})
	// contains filtered or unexported fields
}

Mux is a http.Handler which can be used to dispatch requests to different handler functions via configurable routes.

func (*Mux) DELETE

func (r *Mux) DELETE(path string, handle *RouteData)

DELETE is a shortcut for httpMux.Handle(http.MethodDelete, path, handle).

func (*Mux) GET

func (r *Mux) GET(path string, handle *RouteData)

GET is a shortcut for httpMux.Handle(http.MethodGet, path, handle).

func (*Mux) HEAD

func (r *Mux) HEAD(path string, handle *RouteData)

HEAD is a shortcut for httpMux.Handle(http.MethodHead, path, handle).

func (*Mux) Handle

func (r *Mux) Handle(method, path string, handle *RouteData)

Handle registers a new request handle with the given path and Method.

For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

func (*Mux) Lookup

func (r *Mux) Lookup(method, path string) (*RouteData, Params, bool)

Lookup allows the manual lookup of a Method + path combo. This is e.g. useful to build a framework around this httpMux. If the path was found, it returns the handle function and the path parameter values. Otherwise, the third return value indicates whether a redirection to the same path with an extra / without the trailing slash should be performed.

func (*Mux) OPTIONS

func (r *Mux) OPTIONS(path string, handle *RouteData)

OPTIONS is a shortcut for httpMux.Handle(http.MethodOptions, path, handle).

func (*Mux) PATCH

func (r *Mux) PATCH(path string, handle *RouteData)

PATCH is a shortcut for httpMux.Handle(http.MethodPatch, path, handle).

func (*Mux) POST

func (r *Mux) POST(path string, handle *RouteData)

POST is a shortcut for httpMux.Handle(http.MethodPost, path, handle).

func (*Mux) PUT

func (r *Mux) PUT(path string, handle *RouteData)

PUT is a shortcut for httpMux.Handle(http.MethodPut, path, handle).

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

type Params

type Params []Param

Params is a Param-slice, as returned by the httpMux. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.

func (Params) ByName

func (ps Params) ByName(name string) string

ByName returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.

type RouteData

type RouteData struct {
	Method      string
	Path        string
	Predicate   string
	ServiceName string
	ContractID  string
	Decoder     DecoderFunc
	Factory     kit.MessageFactoryFunc
}

Jump to

Keyboard shortcuts

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