mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
Add Route.RemoveHandler (a fast change to the previous commit)
This commit is contained in:
parent
ae8190eb97
commit
ff7417badd
|
@ -28,7 +28,8 @@ The codebase for Dependency Injection, Internationalization and localization and
|
||||||
|
|
||||||
## Fixes and Improvements
|
## 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:
|
- Add the new `Party.UseOnce` method to the `*Route`
|
||||||
|
- Add a new `*Route.RemoveHandler(interface{}) int`, deletes a handler from begin, main and done handlers based on its name or the handler pc function. Returns the total amount of handlers removed.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func middleware(ctx iris.Context) {
|
func middleware(ctx iris.Context) {
|
||||||
|
@ -45,7 +46,7 @@ func main() {
|
||||||
app.Get("/", index)
|
app.Get("/", index)
|
||||||
|
|
||||||
// Handlers = other
|
// Handlers = other
|
||||||
app.Get("/other", other).RemoveMiddleware(middleware)
|
app.Get("/other", other).RemoveHandler(middleware)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -145,10 +145,12 @@ func (r *Route) UseOnce(handlers ...context.Handler) {
|
||||||
r.beginHandlers = context.UpsertHandlers(r.beginHandlers, handlers)
|
r.beginHandlers = context.UpsertHandlers(r.beginHandlers, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveMiddleware deletes a begin handler based on its
|
// RemoveHandler deletes a handler from begin, main and done handlers
|
||||||
// name or the handler pc function.
|
// based on its name or the handler pc function.
|
||||||
|
// Returns the total amount of handlers removed.
|
||||||
|
//
|
||||||
// Should be called before Application Build.
|
// Should be called before Application Build.
|
||||||
func (r *Route) RemoveMiddleware(nameOrHandler interface{}) {
|
func (r *Route) RemoveHandler(nameOrHandler interface{}) (count int) {
|
||||||
handlerName := ""
|
handlerName := ""
|
||||||
switch h := nameOrHandler.(type) {
|
switch h := nameOrHandler.(type) {
|
||||||
case string:
|
case string:
|
||||||
|
@ -156,19 +158,34 @@ func (r *Route) RemoveMiddleware(nameOrHandler interface{}) {
|
||||||
case context.Handler:
|
case context.Handler:
|
||||||
handlerName = context.HandlerName(h)
|
handlerName = context.HandlerName(h)
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("remove begin handler: unexpected type of %T", h))
|
panic(fmt.Sprintf("remove handler: unexpected type of %T", h))
|
||||||
|
}
|
||||||
|
|
||||||
|
r.beginHandlers = removeHandler(handlerName, r.beginHandlers, &count)
|
||||||
|
r.Handlers = removeHandler(handlerName, r.Handlers, &count)
|
||||||
|
r.doneHandlers = removeHandler(handlerName, r.doneHandlers, &count)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeHandler(handlerName string, handlers context.Handlers, counter *int) (newHandlers context.Handlers) {
|
||||||
|
for _, h := range handlers {
|
||||||
|
if h == nil {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var newHandlers context.Handlers
|
|
||||||
for _, h := range r.beginHandlers {
|
|
||||||
if context.HandlerName(h) == handlerName {
|
if context.HandlerName(h) == handlerName {
|
||||||
|
if counter != nil {
|
||||||
|
*counter++
|
||||||
|
}
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
newHandlers = append(newHandlers, h)
|
newHandlers = append(newHandlers, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
r.beginHandlers = newHandlers
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Done adds explicit finish handlers to this route.
|
// Done adds explicit finish handlers to this route.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user