1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-20 11:27:06 +00:00

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
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-10-09 18:31:38 +03:00
parent 1be327f6b4
commit ae8190eb97
2 changed files with 54 additions and 0 deletions

View File

@@ -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.

View File

@@ -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