httpx

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2018 License: MIT Imports: 3 Imported by: 0

README

httpx is wrapper around the Chi router (github.com/go-chi/chi) that uses a non-statndard, error returning, Handler interface.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(status int, message string) error

Error returns a new error object that includes an http status code.

func Errorf

func Errorf(status int, format string, v ...interface{}) error

Error returns a new error object that includes an http status code and a formatted error message.

func URLParam

func URLParam(r *http.Request, key string) string

URLParam returns the url parameter from a http.Request object.

Types

type Chain

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

Chain acts as a list of Handler middlewares. Chain is effectively immutable: once created, it will always hold the same set of middlewares in the same order.

func NewChain

func NewChain(middlewares ...Middleware) Chain

NewChain creates a new chain, memorizing the given list of middleware middlewares. New serves no other function, middlewares are only called upon a call to Then().

func (Chain) Append

func (c Chain) Append(middlewares ...Middleware) Chain

Append extends a chain, adding the specified middlewares as the last ones in the request flow.

Append returns a new chain, leaving the original one untouched.

stdChain := alice.New(m1, m2)
extChain := stdChain.Append(m3, m4)
// requests in stdChain go m1 -> m2
// requests in extChain go m1 -> m2 -> m3 -> m4

func (Chain) Extend

func (c Chain) Extend(chain Chain) Chain

Extend extends a chain by adding the specified chain as the last one in the request flow.

Extend returns a new chain, leaving the original one untouched.

stdChain := alice.New(m1, m2)
ext1Chain := alice.New(m3, m4)
ext2Chain := stdChain.Extend(ext1Chain)
// requests in stdChain go  m1 -> m2
// requests in ext1Chain go m3 -> m4
// requests in ext2Chain go m1 -> m2 -> m3 -> m4

Another example:

 aHtmlAfterNosurf := alice.New(m2)
	aHtml := alice.New(m1, func(h Handler) Handler {
		csrf := nosurf.New(h)
		csrf.SetFailureHandler(aHtmlAfterNosurf.ThenFunc(csrfFail))
		return csrf
	}).Extend(aHtmlAfterNosurf)
		// requests to aHtml hitting nosurfs success handler go m1 -> nosurf -> m2 -> target-handler
		// requests to aHtml hitting nosurfs failure handler go m1 -> nosurf -> m2 -> csrfFail

func (Chain) Then

func (c Chain) Then(h Handler) Handler

Then chains the middleware and returns the final Handler.

New(m1, m2, m3).Then(h)

is equivalent to:

m1(m2(m3(h)))

When the request comes in, it will be passed to m1, then m2, then m3 and finally, the given handler (assuming every middleware calls the following one).

A chain can be safely reused by calling Then() several times.

stdStack := alice.New(ratelimitHandler, csrfHandler)
indexPipe = stdStack.Then(indexHandler)
authPipe = stdStack.Then(authHandler)

Note that middlewares are called on every call to Then() and thus several instances of the same middleware will be created when a chain is reused in this way. For proper middleware, this should cause no problems.

Then() treats nil as http.DefaultServeMux.

func (Chain) ThenFunc

func (c Chain) ThenFunc(fn HandlerFunc) Handler

ThenFunc works identically to Then, but takes a HandlerFunc instead of a Handler.

The following two statements are equivalent:

c.Then(HandlerFunc(fn))
c.ThenFunc(fn)

ThenFunc provides all the guarantees of Then.

type Handler

type Handler interface {
	ServeHTTP(http.ResponseWriter, *http.Request) error
}

A Handler responds to an HTTP request.

func ErrorHandler

func ErrorHandler(code int, message string) Handler

func FileServer

func FileServer(root http.FileSystem) Handler

FileServer returns a handler that serves HTTP requests with the contents of the file system rooted at root.

To use the operating system's file system implementation, use http.Dir:

http.Handle("/", http.FileServer(http.Dir("/tmp")))

As a special case, the returned file server redirects any request ending in "/index.html" to the same path, without the final "index.html".

func RedirectHandler

func RedirectHandler(url string, code int) Handler

RedirectHandler returns a request handler that redirects each request it receives to the given url using the given status code.

The provided code should be in the 3xx range and is usually StatusMovedPermanently, StatusFound or StatusSeeOther.

func StripPrefix

func StripPrefix(prefix string, h Handler) Handler

StripPrefix returns a handler that serves HTTP requests by removing the given prefix from the request URL's Path and invoking the handler h. StripPrefix handles a request for a path that doesn't begin with prefix by replying with an HTTP 404 not found error.

type HandlerFunc

type HandlerFunc func(http.ResponseWriter, *http.Request) error

The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

func (HandlerFunc) ServeHTTP

func (fn HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error

type Middleware

type Middleware func(Handler) Handler

Middleware for a piece of middleware. Some middleware use this middleware out of the box, so in most cases you can just pass somepackage.New

type Mux

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

Mux is a simple HTTP route multiplexer that parses a request path, records any URL params, and executes an end handler. It implements the http.Handler interface and is friendly with the standard library.

Mux is designed to be fast, minimal and offer a powerful API for building modular and composable HTTP services with a large set of handlers. It's particularly useful for writing large REST API services that break a handler into many smaller parts composed of middlewares and end handlers.

func NewMux

func NewMux() *Mux

NewMux returns a newly initialized Mux object

func (*Mux) Connect

func (m *Mux) Connect(pattern string, handlerFn HandlerFunc)

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

func (*Mux) Delete

func (m *Mux) Delete(pattern string, handlerFn HandlerFunc)

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

func (*Mux) Get

func (m *Mux) Get(pattern string, handlerFn HandlerFunc)

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

func (*Mux) Group

func (m *Mux) Group(fn func(*Mux)) *Mux

Group creates a new inline-Mux with a fresh middleware stack. It's useful for a group of handlers along the same routing path that use an additional set of middlewares.

func (*Mux) Handle

func (m *Mux) Handle(pattern string, handler Handler)

Handle adds the route `pattern` that matches any http method to execute the `handler` httpx.Handler.

func (*Mux) HandleFunc

func (m *Mux) HandleFunc(pattern string, handlerFn HandlerFunc)

HandleFunc adds the route `pattern` that matches any http method to execute the `handlerFn` httpx.HandlerFunc.

func (*Mux) Head

func (m *Mux) Head(pattern string, handlerFn HandlerFunc)

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

func (*Mux) Method

func (m *Mux) Method(method, pattern string, h Handler)

Method adds the route `pattern` that matches `method` http method to execute the `handler` httpx.Handler.

func (*Mux) MethodFunc

func (m *Mux) MethodFunc(method, pattern string, handlerFn HandlerFunc)

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

func (*Mux) MethodNotAllowed

func (m *Mux) MethodNotAllowed(handlerFn HandlerFunc)

MethodNotAllowed sets a custom http.HandlerFunc for routing paths where the method is unresolved. The default handler returns a 405 with an empty body.

func (*Mux) NotFound

func (m *Mux) NotFound(handlerFn HandlerFunc)

NotFound sets a custom http.HandlerFunc for routing paths that could not be found. The default 404 handler is `http.NotFound`.

func (*Mux) Options

func (m *Mux) Options(pattern string, handlerFn HandlerFunc)

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

func (*Mux) Patch

func (m *Mux) Patch(pattern string, handlerFn HandlerFunc)

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

func (*Mux) Post

func (m *Mux) Post(pattern string, handlerFn HandlerFunc)

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

func (*Mux) Put

func (m *Mux) Put(pattern string, handlerFn HandlerFunc)

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

func (*Mux) Route

func (m *Mux) Route(pattern string, fn func(*Mux)) *Mux

Route creates a new Mux with a fresh middleware stack and mounts it along the `pattern` as a subrouter.

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the standard go http.Handler interface.

func (*Mux) Trace

func (m *Mux) Trace(pattern string, handlerFn HandlerFunc)

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

func (*Mux) Use

func (m *Mux) Use(middlewares ...Middleware)

Use appends a middleware handler to the Mux middleware stack.

func (*Mux) With

func (m *Mux) With(middlewares ...Middleware) *Mux

With adds inline middlewares for an endpoint handler.

type StatusError

type StatusError interface {
	error
	Status() int
}

StatusError is an error type that contains an http status.

Jump to

Keyboard shortcuts

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