mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
new feature: handle different param types in the exact same path pattern
implements https://github.com/kataras/iris/issues/1315 Former-commit-id: 3e9276f2a95d6fc7c10fbf91186d041dcba72611
This commit is contained in:
@@ -5,11 +5,12 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/golog"
|
||||
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/errors"
|
||||
"github.com/kataras/iris/core/netutil"
|
||||
macroHandler "github.com/kataras/iris/macro/handler"
|
||||
|
||||
"github.com/kataras/golog"
|
||||
)
|
||||
|
||||
// RequestHandler the middle man between acquiring a context and releasing it.
|
||||
@@ -116,29 +117,63 @@ func (h *routerHandler) Build(provider RoutesProvider) error {
|
||||
})
|
||||
|
||||
for _, r := range registeredRoutes {
|
||||
// build the r.Handlers based on begin and done handlers, if any.
|
||||
r.BuildHandlers()
|
||||
if r.topLink != nil {
|
||||
bindMultiParamTypesHandler(r.topLink, r)
|
||||
}
|
||||
}
|
||||
|
||||
for _, r := range registeredRoutes {
|
||||
if r.Subdomain != "" {
|
||||
h.hosts = true
|
||||
}
|
||||
|
||||
// the only "bad" with this is if the user made an error
|
||||
// on route, it will be stacked shown in this build state
|
||||
// and no in the lines of the user's action, they should read
|
||||
// the docs better. Or TODO: add a link here in order to help new users.
|
||||
if err := h.addRoute(r); err != nil {
|
||||
// node errors:
|
||||
rp.Add("%v -> %s", err, r.String())
|
||||
continue
|
||||
if r.topLink == nil {
|
||||
// build the r.Handlers based on begin and done handlers, if any.
|
||||
r.BuildHandlers()
|
||||
|
||||
// the only "bad" with this is if the user made an error
|
||||
// on route, it will be stacked shown in this build state
|
||||
// and no in the lines of the user's action, they should read
|
||||
// the docs better. Or TODO: add a link here in order to help new users.
|
||||
if err := h.addRoute(r); err != nil {
|
||||
// node errors:
|
||||
rp.Add("%v -> %s", err, r.String())
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
golog.Debugf(r.Trace())
|
||||
golog.Debugf(r.Trace()) // keep log different parameter types in the same path as different routes.
|
||||
}
|
||||
|
||||
return rp.Return()
|
||||
}
|
||||
|
||||
func bindMultiParamTypesHandler(top *Route, r *Route) {
|
||||
r.BuildHandlers()
|
||||
|
||||
h := r.Handlers[1:] // remove the macro evaluator handler as we manually check below.
|
||||
f := macroHandler.MakeFilter(r.tmpl)
|
||||
if f == nil {
|
||||
return // should never happen, previous checks made to set the top link.
|
||||
}
|
||||
|
||||
decisionHandler := func(ctx context.Context) {
|
||||
currentRouteName := ctx.RouteName()
|
||||
if f(ctx) {
|
||||
ctx.SetCurrentRouteName(r.Name)
|
||||
ctx.HandlerIndex(0)
|
||||
ctx.Do(h)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.SetCurrentRouteName(currentRouteName)
|
||||
ctx.StatusCode(http.StatusOK)
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
r.topLink.beginHandlers = append(context.Handlers{decisionHandler}, r.topLink.beginHandlers...)
|
||||
}
|
||||
|
||||
func (h *routerHandler) HandleRequest(ctx context.Context) {
|
||||
method := ctx.Method()
|
||||
path := ctx.Path()
|
||||
|
||||
Reference in New Issue
Block a user