httptools

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2013 License: MIT Imports: 3 Imported by: 0

README

Package httptools tries to augment the basic net/http package with functionality found in webframeworks without breaking the original API.

For details and examples, please see the documentation.

Contrived example

r := httptools.NewRegexpSwitch(map[string]http.Handler{
	"/people/(.+)": httptools.L{
		httptools.SilentHandler(AuthenticationHandler),
		httptools.MethodSwitch{
			"GET": ListPeople,
			"PUT": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				vars := w.(httptools.VarsResponseWriter).Vars()
				AddNewPerson(vars["1"])
			})
		},
		SaveSessionHandler,
	},
	"/.+": http.FileServer(http.Dir("./static")),
})
http.ListenAndServe("localhost:8080", r)

Tools

httptools provides the following tools:

Handler list

Define a sequence of http.Handler. One will be executed after another. A customized http.ResponseWriter allows the passing of data in between handlers.

Silent handler

If a silent handler produces output, it is assumed to be an error. If the silent handler is in a handler list, the execution of the list will be aborted.

Method switch

Dispatch requests to different handlers according the the HTTP verb used in the request.

RegexpSwitch

Dispatch requests to different handlers according to regexps being matched agains the request path.


Version 1.0.0

Documentation

Overview

Package httptools tries to augment the basic net/http package with functionality found in webframeworks without breaking the original API.

Index

Examples

Constants

View Source
const (
	VERSION = "1.0.0"
)

Variables

This section is empty.

Functions

func NewRegexpSwitch

func NewRegexpSwitch(routes map[string]http.Handler) http.Handler

A regexp switch takes a map of regexp strings and handlers. If a request path matches a regexp, the corresponding handler is executed. Submatches will be put inside a VarsResponseWriter with the keys "1", "2", ...

Example
rr := NewRegexpSwitch(map[string]http.Handler{
	"/people/([a-z]+)": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		vars := w.(VarsResponseWriter).Vars()
		fmt.Printf("You are looking for %s", vars["1"].(string))
	}),
})
req, _ := http.NewRequest("GET", "/people/peter", nil)
rr.ServeHTTP(httptest.NewRecorder(), req)
Output:
You are looking for peter

func SilentHandler

func SilentHandler(h http.Handler) http.Handler

"Casts" the given handler into a silent handler. Silent handlers are expected to produce no output. If they do, it is assumend to be an error message/error code. In a HandlerList, this execution of the list will be aborted if a SilentHandler produces output.

Types

type CheckResponseWriter

type CheckResponseWriter interface {
	http.ResponseWriter
	// Returns true if the headers have been written
	WasWritten() bool
}

CheckResponseWriter is a http.ResponseWriter which saves wether it has been written to or not.

type L

type L []http.Handler

A handler list is a list of http.Handlers which are executed sequentially. If a handler is a SilentHandler and it produces output (i.e. calls WriteHeader()), it is assumed to be an error message/error code and executing the remaining handlers in the list will be skipped. The ResponseWriter will be an VarsResponseWriter to make data passing between handlers more convenient.

func (L) ServeHTTP

func (l L) ServeHTTP(w http.ResponseWriter, r *http.Request)

type MethodSwitch

type MethodSwitch map[string]http.Handler

MethodSwitch offers a simple way to apply different handlers depending on the HTTP verb used in the request.

Example
ms := MethodSwitch{
	"GET": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("A GET request")
	}),
	"POST": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("A POST request")
	}),
}
req, _ := http.NewRequest("GET", "/", nil)
ms.ServeHTTP(httptest.NewRecorder(), req)
req, _ = http.NewRequest("POST", "/", nil)
ms.ServeHTTP(httptest.NewRecorder(), req)
Output:
A GET request
A POST request

func (MethodSwitch) ServeHTTP

func (ms MethodSwitch) ServeHTTP(w http.ResponseWriter, r *http.Request)

type VarsResponseWriter

type VarsResponseWriter interface {
	http.ResponseWriter
	Vars() map[string]interface{}
}

VarsResponseWriter is a http.ResponseWriter which gives access to a map. The map can be filled with arbitrary data and is supposed to be out-of-band channel to pass data between handlers in a handler list or any kind of handler switch.

Jump to

Keyboard shortcuts

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