diff --git a/README.md b/README.md index 997ac5f2..793d8a29 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ # Iris Web Framework -[![build status](https://img.shields.io/github/workflow/status/kataras/iris/CI/master?style=for-the-badge)](https://github.com/kataras/iris/actions) [![view examples](https://img.shields.io/badge/examples%20-269-a83adf.svg?style=for-the-badge&logo=go)](https://github.com/kataras/iris/tree/master/_examples) [![chat](https://img.shields.io/gitter/room/iris_go/community.svg?color=cc2b5e&logo=gitter&style=for-the-badge)](https://gitter.im/iris_go/community) [![donate](https://img.shields.io/badge/support-Iris-blue.svg?style=for-the-badge&logo=paypal)](https://iris-go.com/donate) +[![build status](https://img.shields.io/github/workflow/status/kataras/iris/CI/master?style=for-the-badge)](https://github.com/kataras/iris/actions) [![view examples](https://img.shields.io/badge/examples%20-270-a83adf.svg?style=for-the-badge&logo=go)](https://github.com/kataras/iris/tree/master/_examples) [![chat](https://img.shields.io/gitter/room/iris_go/community.svg?color=cc2b5e&logo=gitter&style=for-the-badge)](https://gitter.im/iris_go/community) [![donate](https://img.shields.io/badge/support-Iris-blue.svg?style=for-the-badge&logo=paypal)](https://iris-go.com/donate) diff --git a/_examples/README.md b/_examples/README.md index 28821cc0..4dbe6f81 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -70,7 +70,8 @@ * Convert net/http Handlers * [From func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)](convert-handlers/negroni-like/main.go) * [From http.Handler or http.HandlerFunc](convert-handlers/nethttp/main.go) - * [From func(http.HandlerFunc) http.HandlerFunc](convert-handlers/real-usecase-raven/writing-middleware/main.go) + * [From func(http.Handler) http.Handler](convert-handlers/wrapper/main.go) + * [Convert by your own: sentry/raven middleware](convert-handlers/real-usecase-raven/writing-middleware/main.go) * [Rewrite Middleware](routing/rewrite/main.go) * [Route State](routing/route-state/main.go) * [Remove Route](routing/remove-route/main.go) diff --git a/_examples/convert-handlers/nethttp/wrapper/main.go b/_examples/convert-handlers/nethttp/wrapper/main.go new file mode 100644 index 00000000..3bf03453 --- /dev/null +++ b/_examples/convert-handlers/nethttp/wrapper/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "context" + "net/http" + + "github.com/kataras/iris/v12" +) + +func main() { + app := iris.New() + + httpThirdPartyWrapper := StandardWrapper(Options{ + Message: "test_value", + }) + + // This case + app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) { + httpThirdPartyWrapper(router).ServeHTTP(w, r) + // If was func(http.HandlerFunc) http.HandlerFunc: + // httpThirdPartyWrapper(router.ServeHTTP).ServeHTTP(w, r) + }) + + app.Get("/", index) + app.Listen(":8080") +} + +func index(ctx iris.Context) { + ctx.Writef("Message: %s\n", ctx.Value(msgContextKey)) +} + +type Options struct { + Message string +} + +type contextKey uint8 + +var ( + msgContextKey contextKey = 1 +) + +func StandardWrapper(opts Options) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // ... + req := r.WithContext(context.WithValue(r.Context(), msgContextKey, opts.Message)) + next.ServeHTTP(w, req) + }) + } +} diff --git a/core/handlerconv/from_std.go b/core/handlerconv/from_std.go index 3bb8627f..8a4ec957 100644 --- a/core/handlerconv/from_std.go +++ b/core/handlerconv/from_std.go @@ -32,6 +32,17 @@ func FromStd(handler interface{}) context.Handler { // handlerFunc(w,r, http.HandlerFunc) // return FromStdWithNext(h) + case func(http.Handler) http.Handler: + panic(fmt.Errorf(` + Passed handler cannot be converted directly: + - http.Handler(http.Handler) + --------------------------------------------------------------------- + Please use the Application.WrapRouter method instead, example code: + app := iris.New() + // ... + app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) { + httpThirdPartyHandler(router).ServeHTTP(w, r) + })`)) default: // No valid handler passed panic(fmt.Errorf(`