1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 20:41:57 +00:00

more route info improvements

Former-commit-id: ccbe95de0badb1bf448fcc443cecda60772716dc
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-04-28 22:34:36 +03:00
parent 128cd255cb
commit 2a4043a3c2
14 changed files with 147 additions and 61 deletions

View File

@@ -399,7 +399,7 @@ func (api *APIBuilder) HandleDir(requestPath, directory string, opts ...DirOptio
for _, route := range routes {
if route.Method == http.MethodHead {
} else {
route.SetDescription(description)
route.Describe(description)
route.SetSourceLine(fileName, lineNumber)
}
@@ -453,6 +453,7 @@ func (api *APIBuilder) CreateRoutes(methods []string, relativePath string, handl
// before join the middleware + handlers + done handlers and apply the execution rules.
mainHandlerName, mainHandlerIndex := context.MainHandlerName(mainHandlers)
mainHandlerFileName, mainHandlerFileNumber := context.HandlerFileLineRel(handlers[mainHandlerIndex])
// re-calculate mainHandlerIndex in favor of the middlewares.
@@ -939,7 +940,7 @@ func (api *APIBuilder) Favicon(favPath string, requestPath ...string) *Route {
reqPath = requestPath[0]
}
return api.registerResourceRoute(reqPath, h).SetDescription(description)
return api.registerResourceRoute(reqPath, h).Describe(description)
}
// OnErrorCode registers an error http status code

View File

@@ -117,7 +117,15 @@ func (api *APIContainer) Done(handlersFn ...interface{}) {
// See `OnError`, `RegisterDependency`, `Use`, `Done`, `Get`, `Post`, `Put`, `Patch` and `Delete` too.
func (api *APIContainer) Handle(method, relativePath string, handlersFn ...interface{}) *Route {
handlers := api.convertHandlerFuncs(relativePath, handlersFn...)
return api.Self.Handle(method, relativePath, handlers...)
route := api.Self.Handle(method, relativePath, handlers...)
// Fix main handler name and source modified by execution rules wrapper.
route.MainHandlerName, route.MainHandlerIndex = context.MainHandlerName(handlersFn...)
if len(handlersFn) > route.MainHandlerIndex {
route.SourceFileName, route.SourceLineNumber = context.HandlerFileLineRel(handlersFn[route.MainHandlerIndex])
}
return route
}
// Get registers a route for the Get HTTP Method.

View File

@@ -145,11 +145,11 @@ func (r *Route) SetStatusOffline() bool {
return r.ChangeMethod(MethodNone)
}
// SetDescription sets the route's description
// Describe sets the route's description
// that will be logged alongside with the route information
// in DEBUG log level.
// Returns the `Route` itself.
func (r *Route) SetDescription(description string) *Route {
func (r *Route) Describe(description string) *Route {
r.Description = description
return r
}
@@ -341,38 +341,10 @@ func (r *Route) ResolvePath(args ...string) string {
return formattedPath
}
var ignoreHandlersTraces = [...]string{
"iris/macro/handler.MakeHandler",
"iris/hero.makeHandler.func2",
"iris/core/router.(*APIBuilder).Favicon",
"iris/core/router.StripPrefix",
}
func ignoreHandlerTrace(name string) bool {
for _, ignore := range ignoreHandlersTraces {
if name == ignore {
return true
}
}
return false
}
func traceHandlerFile(method, name, line string, number int) string {
file := fmt.Sprintf("(%s:%d)", line, number)
// trim the path for Iris' internal middlewares, e.g.
// irs/mvc.GRPC.Apply.func1
if internalName := context.PackageName; strings.HasPrefix(name, internalName) {
name = strings.Replace(name, internalName, "iris", 1)
}
if internalName := "github.com/iris-contrib/middleware"; strings.HasPrefix(name, internalName) {
name = strings.Replace(name, internalName, "iris-contrib", 1)
}
name = strings.TrimSuffix(name, ".func1")
if ignoreHandlerTrace(name) {
if context.IgnoreHandlerName(name) {
return ""
}
@@ -457,7 +429,7 @@ func (r *Route) Trace(w io.Writer) {
line int
)
if i == r.MainHandlerIndex {
if i == r.MainHandlerIndex && r.MainHandlerName != "" {
// Main handler info can be programmatically
// changed to be more specific, respect these changes.
name = r.MainHandlerName
@@ -466,7 +438,6 @@ func (r *Route) Trace(w io.Writer) {
} else {
name = context.HandlerName(h)
file, line = context.HandlerFileLineRel(h)
// If a middleware, e.g (macro) which changes the main handler index,
// skip it.
if file == r.SourceFileName && line == r.SourceLineNumber {