mirror of
https://github.com/kataras/iris.git
synced 2026-01-25 04:45:57 +00:00
remove the complicated fallback handlers, that didn't work and not approve the coblexity addition of the https://github.com/kataras/iris/pull/919, RouteExists accepts first argument the Context, add new AllowMethods per party and fix cors by 048e2be034 https://github.com/kataras/iris/issues/922, rel: https://github.com/iris-contrib/middleware/issues/36, https://github.com/iris-contrib/middleware/issues/34, https://github.com/iris-contrib/middleware/issues/32, https://github.com/iris-contrib/middleware/issues/30, https://github.com/kataras/iris/pull/909
Former-commit-id: 5576c44b64014fb00dd79e618b815b5f52b705e4
This commit is contained in:
@@ -22,8 +22,8 @@ type RequestHandler interface {
|
||||
HandleRequest(context.Context)
|
||||
// Build should builds the handler, it's being called on router's BuildRouter.
|
||||
Build(provider RoutesProvider) error
|
||||
// RouteExists checks if a route exists
|
||||
RouteExists(method, path string, ctx context.Context) bool
|
||||
// RouteExists reports whether a particular route exists.
|
||||
RouteExists(ctx context.Context, method, path string) bool
|
||||
}
|
||||
|
||||
type tree struct {
|
||||
@@ -35,14 +35,8 @@ type tree struct {
|
||||
}
|
||||
|
||||
type routerHandler struct {
|
||||
trees []*tree
|
||||
hosts bool // true if at least one route contains a Subdomain.
|
||||
fallbackStack *FallbackStack
|
||||
// on build: true if fallbackStack.Size() > 0,
|
||||
// reduces the checks because fallbackStack is NEVER nil (api_builder.go always initializes it).
|
||||
// If re-checked needed (serve-time fallback handler added)
|
||||
// then a re-build/refresh of the application's router is necessary, as with every handler.
|
||||
hasFallbackHandlers bool
|
||||
trees []*tree
|
||||
hosts bool // true if at least one route contains a Subdomain.
|
||||
}
|
||||
|
||||
var _ RequestHandler = &routerHandler{}
|
||||
@@ -90,15 +84,11 @@ func NewDefaultHandler() RequestHandler {
|
||||
type RoutesProvider interface { // api builder
|
||||
GetRoutes() []*Route
|
||||
GetRoute(routeName string) *Route
|
||||
|
||||
GetFallBackStack() *FallbackStack
|
||||
}
|
||||
|
||||
func (h *routerHandler) Build(provider RoutesProvider) error {
|
||||
registeredRoutes := provider.GetRoutes()
|
||||
h.trees = h.trees[0:0] // reset, inneed when rebuilding.
|
||||
h.fallbackStack = provider.GetFallBackStack()
|
||||
h.hasFallbackHandlers = h.fallbackStack.Size() > 0
|
||||
|
||||
// sort, subdomains goes first.
|
||||
sort.Slice(registeredRoutes, func(i, j int) bool {
|
||||
@@ -262,16 +252,12 @@ func (h *routerHandler) HandleRequest(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
if h.hasFallbackHandlers {
|
||||
ctx.Do(h.fallbackStack.List())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.StatusCode(http.StatusNotFound)
|
||||
}
|
||||
|
||||
// RouteExists checks if a route exists
|
||||
func (h *routerHandler) RouteExists(method, path string, ctx context.Context) bool {
|
||||
// RouteExists reports whether a particular route exists
|
||||
// It will search from the current subdomain of context's host, if not inside the root domain.
|
||||
func (h *routerHandler) RouteExists(ctx context.Context, method, path string) bool {
|
||||
for i := range h.trees {
|
||||
t := h.trees[i]
|
||||
if method != t.Method {
|
||||
|
||||
Reference in New Issue
Block a user