Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware (handler wrapping) #36

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type Router struct {
strictSlash bool
// If true, do not clear the request context after handling the request
KeepContext bool
// Middleware layers to be wrapped around matched handler
layers []Middleware
}

// Match matches registered routes against the request.
Expand Down Expand Up @@ -87,6 +89,12 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if !r.KeepContext {
defer context.Clear(req)
}

for i := len(r.layers) - 1; i >= 0; i-- {
layer := r.layers[i]
handler = layer(handler)
}

handler.ServeHTTP(w, req)
}

Expand Down Expand Up @@ -114,6 +122,21 @@ func (r *Router) StrictSlash(value bool) *Router {
return r
}

// ----------------------------------------------------------------------------
// Middleware
// ----------------------------------------------------------------------------

// Middleware wraps an http.Handler to provide additional functionality
type Middleware func(http.Handler) http.Handler

// Use registers middleware layers to be used
func (r *Router) Use(layers ...Middleware) *Router {
for _, layer := range layers {
r.layers = append(r.layers, layer)
}
return r
}

// ----------------------------------------------------------------------------
// parentRoute
// ----------------------------------------------------------------------------
Expand Down
33 changes: 33 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,39 @@ func TestSubrouterHeader(t *testing.T) {
}
}

func TestUse(t *testing.T) {
expected := "foobarbaz"

mw1 := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "foo")
next.ServeHTTP(w, req)
})
}

mw2 := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "bar")
next.ServeHTTP(w, req)
})
}

handler := func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "baz")
}

r := NewRouter().Use(mw1, mw2)
r.HandleFunc("/", handler)

req, _ := http.NewRequest("GET", "http://localhost/", nil)
resp := NewRecorder()

r.ServeHTTP(resp, req)
if resp.Body.String() != expected {
t.Errorf("Expecting %q got %q", expected, resp.Body.String())
}
}

// mapToPairs converts a string map to a slice of string pairs
func mapToPairs(m map[string]string) []string {
var i int
Expand Down