1.3 KiB
1.3 KiB
Adapters
Adapters is a collection of useful HTTP middleware or "Adapters". They follow the Adapter Pattern described by Mat Ryer in his blog post Writing middleware in #golang and how Go makes it so much fun.
Adapters can be chained in any way and will be executed in the order they are specified.
Usage
package main
import (
"fmt"
"log"
"net/http"
"github.com/mastertinner/adapters"
"github.com/mastertinner/adapters/logging"
)
// IndexHandler says what it loves
func IndexHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
})
}
func main() {
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
http.Handle("/", adapters.Adapt(IndexHandler(), logging.Handler(logger)))
log.Fatal(http.ListenAndServe(":8080", nil))
}
Adapters
This package contains the following adapters: