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: 4 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

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

// 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 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 WithRequestID

func WithRequestID(ctx context.Context, requestID string) context.Context

WithRequestID inserts a RequestID into the 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 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(method, path string, h Handler)

Handle adds a new router that routes requests using the method verb against path to the given Handler.

func (*Router) Handler

func (r *Router) Handler(req *http.Request) (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) Header

func (r *Router) Header(key, value string, h Handler)

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) 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