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 ¶
const (
VERSION = "1.0.0"
)
Variables ¶
This section is empty.
Functions ¶
func NewRegexpSwitch ¶
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 ¶
"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 ¶
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.
type MethodSwitch ¶
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.