Skip to content
Augmenting the basic net/http package with functionality found in web frameworks without breaking the original API.
Go
Latest commit 2d75ac3 Aug 29, 2015 @surma Merge branch 'release/2.1.0'
Failed to load latest commit information.
LICENSE.md Add LICENSE Oct 30, 2013
README.md
doc.go Version bump Aug 29, 2015
handlerlist.go
handlerlist_test.go Rename L to List Aug 29, 2015
hostnameswitch.go
hostnameswitch_test.go
methodswitch.go
methodswitch_test.go Add documentation Nov 6, 2013
misc.go
misc_test.go
mounts.go
mounts_test.go
regexpswitch.go
regexpswitch_test.go Remove VarsResponseWriter and keep it simple Aug 29, 2015

README.md

Package httptools is a collection of simple helper types for Go’s net/http.

For details and examples, please see the documentation.

Build Status

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) {
                name := strings.StripPrefix(r.URL.Path, "/people/")
                AddNewPerson(name)
            })
        },
        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.

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 that list will be aborted.

Switches

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 against the request path.

HostnameSwitch

Dispatch requests to different handlers according to the hostname used in the request.

Mounts

Dispatch requests to different handlers according to path prefixes. The path prefix will be stripped from the request before being passed to the handler.


Version 2.1.0

Something went wrong with that request. Please try again.