2018-02-21 06:18:53 +01:00
|
|
|
package router
|
|
|
|
|
2018-02-23 03:06:05 +01:00
|
|
|
import "github.com/kataras/iris/context"
|
2018-02-21 06:18:53 +01:00
|
|
|
|
2018-02-21 10:27:01 +01:00
|
|
|
// FallbackStack is a stack (with LIFO calling order) for fallback handlers
|
|
|
|
// A fallback handler(s) is(are) called from Fallback stack
|
|
|
|
// when no route found and before sending NotFound status.
|
|
|
|
// Therefore Handler(s) in Fallback stack could send another thing than NotFound status,
|
2018-02-23 03:06:05 +01:00
|
|
|
// if `context#NextOrNotFound()` method is not called.
|
2018-02-21 10:27:01 +01:00
|
|
|
// Done & DoneGlobal Handlers are not called.
|
2018-02-21 06:18:53 +01:00
|
|
|
type FallbackStack struct {
|
2018-02-21 10:27:01 +01:00
|
|
|
parent *FallbackStack
|
2018-02-21 06:18:53 +01:00
|
|
|
handlers context.Handlers
|
|
|
|
}
|
|
|
|
|
2018-02-21 10:27:01 +01:00
|
|
|
// _size is a terminal recursive method for computing size the stack
|
|
|
|
func (stk *FallbackStack) _size(i int) int {
|
|
|
|
res := i + len(stk.handlers)
|
2018-02-21 06:18:53 +01:00
|
|
|
|
2018-02-21 10:27:01 +01:00
|
|
|
if stk.parent == nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
return stk.parent._size(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// populate is a recursive method for concatenating handlers to `list` parameter
|
|
|
|
func (stk *FallbackStack) populate(list context.Handlers) {
|
|
|
|
n := copy(list, stk.handlers)
|
|
|
|
|
|
|
|
if stk.parent != nil {
|
|
|
|
stk.parent.populate(list[n:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size gives the size of the full stack hierarchy
|
|
|
|
func (stk *FallbackStack) Size() int {
|
|
|
|
return stk._size(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add appends handlers to the beginning of the stack to have a LIFO calling order
|
|
|
|
func (stk *FallbackStack) Add(h context.Handlers) {
|
2018-02-21 06:18:53 +01:00
|
|
|
stk.handlers = append(stk.handlers, h...)
|
|
|
|
|
|
|
|
copy(stk.handlers[len(h):], stk.handlers)
|
|
|
|
copy(stk.handlers, h)
|
|
|
|
}
|
|
|
|
|
2018-02-21 10:27:01 +01:00
|
|
|
// Fork make a new stack from this stack, and so create a stack child (leaf from a tree of stacks)
|
|
|
|
func (stk *FallbackStack) Fork() *FallbackStack {
|
|
|
|
return &FallbackStack{
|
|
|
|
parent: stk,
|
|
|
|
}
|
|
|
|
}
|
2018-02-21 06:18:53 +01:00
|
|
|
|
2018-02-21 10:27:01 +01:00
|
|
|
// List concatenate all handlers in stack hierarchy
|
|
|
|
func (stk *FallbackStack) List() context.Handlers {
|
|
|
|
res := make(context.Handlers, stk.Size())
|
|
|
|
stk.populate(res)
|
2018-02-21 06:18:53 +01:00
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-02-23 03:06:05 +01:00
|
|
|
// NewFallbackStack create a new empty Fallback stack.
|
|
|
|
func NewFallbackStack() *FallbackStack { return &FallbackStack{} }
|