mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
various improvements and new 'UseOnce' method - read HISTORY.md
This commit is contained in:
@@ -138,6 +138,8 @@ func overlapRoute(r *Route, next *Route) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.SetErr(nil) // clear any stored error.
|
||||
// Set the route to the next one and execute it.
|
||||
ctx.SetCurrentRoute(next.ReadOnly)
|
||||
ctx.HandlerIndex(0)
|
||||
ctx.Do(nextHandlers)
|
||||
@@ -768,6 +770,25 @@ func (api *APIBuilder) Use(handlers ...context.Handler) {
|
||||
api.middleware = append(api.middleware, handlers...)
|
||||
}
|
||||
|
||||
// UseOnce either inserts a middleware,
|
||||
// or on the basis of the middleware already existing,
|
||||
// replace that existing middleware instead.
|
||||
func (api *APIBuilder) UseOnce(handlers ...context.Handler) {
|
||||
reg:
|
||||
for _, handler := range handlers {
|
||||
name := context.HandlerName(handler)
|
||||
for i, registeredHandler := range api.middleware {
|
||||
registeredName := context.HandlerName(registeredHandler)
|
||||
if name == registeredName {
|
||||
api.middleware[i] = handler // replace this handler with the new one.
|
||||
continue reg // break and continue to the next handler.
|
||||
}
|
||||
}
|
||||
|
||||
api.middleware = append(api.middleware, handler) // or just insert it.
|
||||
}
|
||||
}
|
||||
|
||||
// UseGlobal registers handlers that should run at the very beginning.
|
||||
// It prepends those handler(s) to all routes,
|
||||
// including all parties, subdomains.
|
||||
|
||||
@@ -46,7 +46,7 @@ type Attachments struct {
|
||||
type DirCacheOptions struct {
|
||||
// Enable or disable cache.
|
||||
Enable bool
|
||||
// Minimium content size for compression in bytes.
|
||||
// Minimum content size for compression in bytes.
|
||||
CompressMinSize int64
|
||||
// Ignore compress files that match this pattern.
|
||||
CompressIgnore *regexp.Regexp
|
||||
|
||||
@@ -76,9 +76,14 @@ type Party interface {
|
||||
// Use appends Handler(s) to the current Party's routes and child routes.
|
||||
// If the current Party is the root, then it registers the middleware to all child Parties' routes too.
|
||||
Use(middleware ...context.Handler)
|
||||
// UseOnce either inserts a middleware,
|
||||
// or on the basis of the middleware already existing,
|
||||
// replace that existing middleware instead.
|
||||
UseOnce(handlers ...context.Handler)
|
||||
// Done appends to the very end, Handler(s) to the current Party's routes and child routes.
|
||||
// The difference from .Use is that this/or these Handler(s) are being always running last.
|
||||
Done(handlers ...context.Handler)
|
||||
|
||||
// Reset removes all the begin and done handlers that may derived from the parent party via `Use` & `Done`,
|
||||
// and the execution rules.
|
||||
// Note that the `Reset` will not reset the handlers that are registered via `UseGlobal` & `DoneGlobal`.
|
||||
|
||||
Reference in New Issue
Block a user