Documentation
¶
Overview ¶
package httpx provides an extra layer of convenience over package http.
Index ¶
- func NotFound(ctx context.Context, w http.ResponseWriter, r *http.Request) error
- func RequestFromContext(ctx context.Context) (*http.Request, bool)
- func RequestID(ctx context.Context) string
- func Vars(ctx context.Context) map[string]string
- func WithRequest(ctx context.Context, r *http.Request) context.Context
- func WithRoute(ctx context.Context, r *Route) context.Context
- func WithVars(ctx context.Context, vars map[string]string) context.Context
- type Handler
- type HandlerFunc
- type Route
- func (r *Route) GetName() string
- func (r *Route) GetPathTemplate() string
- func (r *Route) Handler(h Handler) *Route
- func (r *Route) HandlerFunc(f func(context.Context, http.ResponseWriter, *http.Request) error) *Route
- func (r *Route) Methods(methods ...string) *Route
- func (r *Route) Name(name string) *Route
- func (r *Route) URL(pairs ...string) (*url.URL, error)
- func (r *Route) URLPath(pairs ...string) (*url.URL, error)
- type Router
- func (r *Router) Handle(path string, h Handler) *Route
- func (r *Router) HandleFunc(path string, f func(context.Context, http.ResponseWriter, *http.Request) error) *Route
- func (r *Router) Handler(req *http.Request) (route *Route, h Handler, vars map[string]string)
- func (r *Router) Headers(pairs ...string) *Route
- func (r *Router) Match(f func(*http.Request) bool, h Handler)
- func (r *Router) Path(path string) *Route
- func (r *Router) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, req *http.Request) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RequestFromContext ¶
RequestFromContext extracts the http.Request from the context.Context.
func WithRequest ¶
WithRequest inserts an http.Request into the context.
Types ¶
type Handler ¶
type Handler interface {
ServeHTTPContext(context.Context, http.ResponseWriter, *http.Request) error
}
Handler is represents a Handler that can take a context.Context as the first argument.
type HandlerFunc ¶
The HandlerFunc type is an adapter to allow the use of ordinary functions as httpx handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.
func (HandlerFunc) ServeHTTPContext ¶
func (f HandlerFunc) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) error
ServeHTTPContext calls f(ctx, w, r)
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
Route wraps a mux.Route.
func RouteFromContext ¶
RouteFromContext extracts the current Route from a context.Context.
func (*Route) GetPathTemplate ¶
Returns the path template for this route, if any.
func (*Route) HandlerFunc ¶
func (r *Route) HandlerFunc(f func(context.Context, http.ResponseWriter, *http.Request) error) *Route
HandlerFunc sets the httpx.Handler for this route.
func (*Route) Methods ¶
Methods adds a matcher for HTTP methods. It accepts a sequence of one or more methods to be matched, e.g.: "GET", "POST", "PUT".
type Router ¶
type Router struct { // NotFoundHandler is a Handler that will be called when a route is not // found. NotFoundHandler Handler // contains filtered or unexported fields }
Router is an httpx.Handler router.
func (*Router) HandleFunc ¶
func (r *Router) HandleFunc(path string, f func(context.Context, http.ResponseWriter, *http.Request) error) *Route
HandleFunc registers a new route with a matcher for the URL path
func (*Router) Handler ¶
Handler returns a Handler that can be used to serve the request. Most of this is pulled from http://goo.gl/tyxad8.
func (*Router) ServeHTTPContext ¶
func (r *Router) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, req *http.Request) error
ServeHTTPContext implements the Handler interface.