Skip to content

Commit

Permalink
support more middleware/afterware types
Browse files Browse the repository at this point in the history
notably `func(http.ResponseWriter, *http.Request) context.Context`
  • Loading branch information
guregu committed Aug 24, 2016
1 parent 096aa33 commit d3fd276
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions middleware_17.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ type Middleware func(context.Context, http.ResponseWriter, *http.Request) contex
// The following concrete types are accepted:
// - Middleware
// - func(context.Context, http.ResponseWriter, *http.Request) context.Context
// - func(http.ResponseWriter, *http.Request) context.Context
// - func(http.Handler) http.Handler [* see Use docs]
// - func(http.ContextHandler) http.ContextHandler [* see Use docs]
// - http.Handler [read only]
// - func(w http.ResponseWriter, r *http.Request) [read only]
// - func(http.ResponseWriter, *http.Request) [read only]
// The old x/net/context is also supported.
type MiddlewareType interface{}

Expand All @@ -42,7 +43,7 @@ type Afterware func(context.Context, mutil.WriterProxy, *http.Request) context.C
// - func(context.Context, http.ResponseWriter, *http.Request) context.Context
// - func(context.Context, *http.Request) context.Context
// - func(context.Context) context.Context
// - Middleware
// - Middleware types
// The old x/net/context is also supported.
type AfterwareType interface{}

Expand Down Expand Up @@ -184,6 +185,10 @@ func convert(mw MiddlewareType) Middleware {
x(w, r)
return r.Context()
})
case func(w http.ResponseWriter, r *http.Request) context.Context:
return Middleware(func(_ context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return x(w, r)
})
}
panic(fmt.Errorf("unsupported MiddlewareType: %T", mw))
}
Expand Down Expand Up @@ -227,6 +232,14 @@ func convertAW(aw AfterwareType) Afterware {
return func(ctx context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
return x(ctx, w, r)
}
case func(w http.ResponseWriter, r *http.Request) context.Context:
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
return x(w, r)
})
case func(w mutil.WriterProxy, r *http.Request) context.Context:
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
return x(w, r)
})
case http.Handler:
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
x.ServeHTTP(w, r)
Expand All @@ -237,6 +250,11 @@ func convertAW(aw AfterwareType) Afterware {
x(w, r)
return r.Context()
})
case func(w mutil.WriterProxy, r *http.Request):
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
x(w, r)
return r.Context()
})
}
panic(fmt.Errorf("unsupported AfterwareType: %T", aw))
}
Expand Down

0 comments on commit d3fd276

Please sign in to comment.