Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Augmenting the basic net/http package with functionality found in web frameworks without breaking the original API.
Go

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
LICENSE.md
README.md
doc.go
handlerlist.go
handlerlist_test.go
hostnameswitch.go
hostnameswitch_test.go
methodswitch.go
methodswitch_test.go
misc.go
misc_test.go
mounts.go
mounts_test.go
regexpswitch.go
regexpswitch_test.go
responsewriter.go

README.md

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.

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

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 agains 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 1.2.1

Something went wrong with that request. Please try again.