httpx

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2015 License: BSD-2-Clause Imports: 7 Imported by: 0

README

Package httpx

Package httpx provides a layer of convenience over "net/http". Specifically:

  1. It's context.Context aware. This is good for storing request specific parameters, such as a request ids and for performing deadlines and cancellations across api boundaries in a generic way.
  2. httpx.Handler's return an error which makes handler implementations feel more idiomatic and reduces the chance of accidentally forgetting to return.

The most important part of package httpx is the Handler interface, which is defined as:

type Handler interface {
	ServeHTTPContext(context.Context, http.ResponseWriter, *http.Request) error
}

Usage

In order to use the httpx.Handler interface, you need a compatible router. One is provided within this package that wraps gorilla mux to make it context.Context aware.

r := httpx.NewRouter()
r.HandleFunc("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
	io.WriteString(w, `ok`)
	return nil
}).Methods("GET")

// Adapt the router to the http.Handler interface and insert a
// context.Background().
s := middleware.Background(r)

http.ListenAndServe(":8080", s)

Documentation

Overview

package httpx provides an extra layer of convenience over package http.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NotFound

func NotFound(ctx context.Context, w http.ResponseWriter, r *http.Request) error

NotFound is a HandlerFunc that just delegates off to http.NotFound.

func RequestFromContext

func RequestFromContext(ctx context.Context) (*http.Request, bool)

RequestFromContext extracts the http.Request from the context.Context.

func RequestID

func RequestID(ctx context.Context) string

RequestID extracts a RequestID from a context.

func Vars

func Vars(ctx context.Context) map[string]string

Vars extracts the route vars from a context.Context.

func WithRequest

func WithRequest(ctx context.Context, r *http.Request) context.Context

WithRequest inserts an http.Request into the context.

func WithRoute

func WithRoute(ctx context.Context, r *Route) context.Context

WithVars adds the current Route to the context.Context.

func WithVars

func WithVars(ctx context.Context, vars map[string]string) context.Context

WithVars adds the vars to the context.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

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

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

func RouteFromContext(ctx context.Context) *Route

RouteFromContext extracts the current Route from a context.Context.

func (*Route) GetName

func (r *Route) GetName() string

GetName returns the name for the route, if any.

func (*Route) GetPathTemplate

func (r *Route) GetPathTemplate() string

Returns the path template for this route, if any.

func (*Route) Handler

func (r *Route) Handler(h Handler) *Route

Handler sets the httpx.Handler for this route.

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

func (r *Route) Methods(methods ...string) *Route

Methods adds a matcher for HTTP methods. It accepts a sequence of one or more methods to be matched, e.g.: "GET", "POST", "PUT".

func (*Route) Name

func (r *Route) Name(name string) *Route

Name sets the name for the route, used to build URLs. If the name was registered already it will be overwritten.

func (*Route) URL

func (r *Route) URL(pairs ...string) (*url.URL, error)

See mux.Route.URL.

func (*Route) URLPath

func (r *Route) URLPath(pairs ...string) (*url.URL, error)

See mux.Route.URLPath.

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 NewRouter

func NewRouter() *Router

NewRouter returns a new Router instance.

func (*Router) Handle

func (r *Router) Handle(path string, h Handler) *Route

Handle registers a new route with a matcher for the URL path

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

func (r *Router) Handler(req *http.Request) (route *Route, h Handler, vars map[string]string)

Handler returns a Handler that can be used to serve the request. Most of this is pulled from http://goo.gl/tyxad8.

func (*Router) Headers

func (r *Router) Headers(pairs ...string) *Route

Header adds a route that will be used if the header value matches.

func (*Router) Match

func (r *Router) Match(f func(*http.Request) bool, h Handler)

Match adds a route that will be matched if f returns true.

func (*Router) Path

func (r *Router) Path(path string) *Route

Path registers a new route with a matcher for the URL path.

func (*Router) ServeHTTPContext

func (r *Router) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, req *http.Request) error

ServeHTTPContext implements the Handler interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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