From ae8190eb972096ed0d9b1b1d7dba135fefdc977d Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Fri, 9 Oct 2020 18:31:38 +0300 Subject: [PATCH] add Route.RemoveMiddleware method Useful when all routes under a Party accept a middleware with some exceptions. For Party-level use its Reset method instead, unless otherwise requested --- HISTORY.md | 21 +++++++++++++++++++++ core/router/route.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index c2206108..fb75e10a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -28,6 +28,27 @@ The codebase for Dependency Injection, Internationalization and localization and ## Fixes and Improvements +- Add the new `Party.UseOnce` method to the `*Route` too and a new `*Route.RemoveMiddleware(interface{})` method was added in order to remove a specific middleware from a specific Route. Example: + +```go +func middleware(ctx iris.Context) { + // [...] +} + +func main() { + app := iris.New() + + // Register the middleware to all matched routes. + app.Use(middleware) + + // Handlers = middleware, other + app.Get("/", index) + + // Handlers = other + app.Get("/other", other).RemoveMiddleware(middleware) +} +``` + - Redis Driver is now based on the [go-redis](https://github.com/go-redis/redis/) module. Radix and redigo removed entirely. Sessions are now stored in hashes which fixes [issue #1610](https://github.com/kataras/iris/issues/1610). The only breaking change on default configuration is that the `redis.Config.Delim` option was removed. The redis sessions database driver is now defaults to the `&redis.GoRedisDriver{}`. End-developers can implement their own implementations too. The `Database#Close` is now automatically called on interrupt signals, no need to register it by yourself. - Add builtin support for **[i18n pluralization](https://github.com/kataras/iris/tree/master/_examples/i18n/plurals)**. Please check out the [following yaml locale example](https://github.com/kataras/iris/tree/master/_examples/i18n/plurals/locales/en-US/welcome.yml) to see an overview of the supported formats. diff --git a/core/router/route.go b/core/router/route.go index c0a01042..b3b627ff 100644 --- a/core/router/route.go +++ b/core/router/route.go @@ -138,6 +138,39 @@ func (r *Route) Use(handlers ...context.Handler) { r.beginHandlers = append(r.beginHandlers, handlers...) } +// UseOnce like Use but it replaces any duplicate handlers with +// the new ones. +// Should be called before Application Build. +func (r *Route) UseOnce(handlers ...context.Handler) { + r.beginHandlers = context.UpsertHandlers(r.beginHandlers, handlers) +} + +// RemoveMiddleware deletes a begin handler based on its +// name or the handler pc function. +// Should be called before Application Build. +func (r *Route) RemoveMiddleware(nameOrHandler interface{}) { + handlerName := "" + switch h := nameOrHandler.(type) { + case string: + handlerName = h + case context.Handler: + handlerName = context.HandlerName(h) + default: + panic(fmt.Sprintf("remove begin handler: unexpected type of %T", h)) + } + + var newHandlers context.Handlers + for _, h := range r.beginHandlers { + if context.HandlerName(h) == handlerName { + continue + } + + newHandlers = append(newHandlers, h) + } + + r.beginHandlers = newHandlers +} + // Done adds explicit finish handlers to this route. // Alternatively the end-dev can append to the `Handlers` field. // Should be used before the `BuildHandlers` which is