From f7757c0793b04bafde5356efcad78afcdef8e164 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Wed, 27 Jan 2021 01:22:20 +0200 Subject: [PATCH] fix https://github.com/kataras/iris/issues/1713 and add a simple usage example of the 'RemoveHandler' --- _examples/README.md | 1 + _examples/routing/remove-handler/main.go | 29 ++++++++++++ _examples/routing/remove-handler/main_test.go | 14 ++++++ context/context.go | 2 +- core/handlerconv/from_std.go | 44 ++++++------------- core/router/api_builder.go | 2 +- core/router/route.go | 2 +- 7 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 _examples/routing/remove-handler/main.go create mode 100644 _examples/routing/remove-handler/main_test.go diff --git a/_examples/README.md b/_examples/README.md index cbf770df..22f7485f 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -48,6 +48,7 @@ * Middleware * [Per Route](routing/writing-a-middleware/per-route/main.go) * [Globally](routing/writing-a-middleware/globally/main.go) + * [Remove a Handler](routing/remove-handler/main.go) * Share Values * [Share Services](routing/writing-a-middleware/share-services/main.go) * [Share Functions](routing/writing-a-middleware/share-funcs/main.go) diff --git a/_examples/routing/remove-handler/main.go b/_examples/routing/remove-handler/main.go new file mode 100644 index 00000000..7aca5fd2 --- /dev/null +++ b/_examples/routing/remove-handler/main.go @@ -0,0 +1,29 @@ +package main + +import "github.com/kataras/iris/v12" + +func main() { + app := newApp() + app.Listen(":8080") +} + +func newApp() *iris.Application { + app := iris.New() + + api := app.Party("/api") + api.Use(myMiddleware) + users := api.Party("/users") + users.Get("/", usersIndex).RemoveHandler(myMiddleware) + // OR for all routes under a Party (or Application): + // users.RemoveHandler(...) + + return app +} + +func myMiddleware(ctx iris.Context) { + ctx.WriteString("Middleware\n") +} + +func usersIndex(ctx iris.Context) { + ctx.WriteString("OK") +} diff --git a/_examples/routing/remove-handler/main_test.go b/_examples/routing/remove-handler/main_test.go new file mode 100644 index 00000000..fa80d177 --- /dev/null +++ b/_examples/routing/remove-handler/main_test.go @@ -0,0 +1,14 @@ +package main + +import ( + "testing" + + "github.com/kataras/iris/v12/httptest" +) + +func TestSimpleRouteRemoveHandler(t *testing.T) { + app := newApp() + e := httptest.New(t, app) + + e.GET("/api/users").Expect().Status(httptest.StatusOK).Body().Equal("OK") +} diff --git a/context/context.go b/context/context.go index f447f633..83a05454 100644 --- a/context/context.go +++ b/context/context.go @@ -2280,7 +2280,7 @@ type internalJSONDecoder interface { func (cfg JSONReader) getDecoder(r io.Reader, globalShouldOptimize bool) (decoder internalJSONDecoder) { if cfg.Optimize || globalShouldOptimize { - decoder = jsoniter.NewDecoder(r) + decoder = jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(r) } else { decoder = json.NewDecoder(r) } diff --git a/core/handlerconv/from_std.go b/core/handlerconv/from_std.go index d42ac1c0..404657b2 100644 --- a/core/handlerconv/from_std.go +++ b/core/handlerconv/from_std.go @@ -16,48 +16,30 @@ import ( func FromStd(handler interface{}) context.Handler { switch h := handler.(type) { case context.Handler: - { - // - // it's already an Iris Handler - // - return h - } + return h + case func(*context.Context): + return h case http.Handler: - { - // - // handlerFunc.ServeHTTP(w,r) - // - return func(ctx *context.Context) { - h.ServeHTTP(ctx.ResponseWriter(), ctx.Request()) - } + // handlerFunc.ServeHTTP(w,r) + return func(ctx *context.Context) { + h.ServeHTTP(ctx.ResponseWriter(), ctx.Request()) } case func(http.ResponseWriter, *http.Request): - { - // - // handlerFunc(w,r) - // - return FromStd(http.HandlerFunc(h)) - } + // handlerFunc(w,r) + return FromStd(http.HandlerFunc(h)) case func(http.ResponseWriter, *http.Request, http.HandlerFunc): - { - // - // handlerFunc(w,r, http.HandlerFunc) - // - return FromStdWithNext(h) - } + // handlerFunc(w,r, http.HandlerFunc) + // + return FromStdWithNext(h) default: - { - // - // No valid handler passed - // - panic(fmt.Errorf(` + // No valid handler passed + panic(fmt.Errorf(` Passed argument is not a func(iris.Context) neither one of these types: - http.Handler - func(w http.ResponseWriter, r *http.Request) - func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) --------------------------------------------------------------------- It seems to be a %T points to: %v`, handler, handler)) - } } } diff --git a/core/router/api_builder.go b/core/router/api_builder.go index b960d91b..790052b3 100644 --- a/core/router/api_builder.go +++ b/core/router/api_builder.go @@ -1210,7 +1210,7 @@ func (api *APIBuilder) RemoveHandler(namesOrHandlers ...interface{}) Party { switch h := nameOrHandler.(type) { case string: handlerName = h - case context.Handler: + case context.Handler, func(*context.Context): handlerName = context.HandlerName(h) case *int: counter = h diff --git a/core/router/route.go b/core/router/route.go index 7015912e..c78ba3ce 100644 --- a/core/router/route.go +++ b/core/router/route.go @@ -159,7 +159,7 @@ func (r *Route) RemoveHandler(namesOrHandlers ...interface{}) (count int) { switch h := nameOrHandler.(type) { case string: handlerName = h - case context.Handler: + case context.Handler, func(*context.Context): handlerName = context.HandlerName(h) default: panic(fmt.Sprintf("remove handler: unexpected type of %T", h))