1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-25 04:45:57 +00:00
Former-commit-id: 5576c44b64014fb00dd79e618b815b5f52b705e4
This commit is contained in:
Gerasimos Maropoulos
2018-03-10 14:22:56 +02:00
parent 1165b4527a
commit 4993918a12
11 changed files with 131 additions and 364 deletions

View File

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