Home > other >  What is the meaning of Go/Gin debug output - (x handlers)
What is the meaning of Go/Gin debug output - (x handlers)

Time:01-21

In the following Go/Gin debug output, what is the meaning of (5 handlers). As you can see, I obviously have more than 5 handlers.

[GIN-debug] GET    /                         --> .../handlers.APIDetail (5 handlers)
[GIN-debug] POST   /signup                   --> .../handlers.SignUp (5 handlers)
[GIN-debug] POST   /signin                   --> .../handlers.SignIn (5 handlers)
[GIN-debug] POST   /refresh-token            --> .../handlers.RefreshToken (5 handlers)
[GIN-debug] POST   /verify-email             --> .../handlers.VerifyEmailVerificationToken (5 handlers)
[GIN-debug] POST   /resend-verification-email --> .../handlers.ResendEmailVerificationEmail (5 handlers)
[GIN-debug] POST   /reset-password           --> .../handlers.ResetPassword (5 handlers)
[GIN-debug] POST   /change-password          --> .../handlers.ChangePassword (5 handlers)
[GIN-debug] PATCH  /users/me                 --> .../handlers.UpdateProfile (5 handlers)
[GIN-debug] POST   /signout                  --> .../handlers.SignOut (5 handlers)
[GIN-debug] GET    /orgs/:id                 --> .../handlers.GetOrganizations (5 handlers)
[GIN-debug] GET    /orgs                     --> .../handlers.GetOrganizations (5 handlers)

CodePudding user response:

It should be the number of handlers in each route's handler chain, i.e. the maximum number of handlers, including middleware, that will be executed when a request is routed to a certain endpoint.

Relevant code from Gin sources @latest:

func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
    if IsDebugging() {
        nuHandlers := len(handlers)
        handlerName := nameOfFunction(handlers.Last())
        if DebugPrintRouteFunc == nil {
            debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
        } else {
            DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)
        }
    }
}

If you are setting a few global middlewares, e.g. with router.Use, before declaring the routes, and no route has per-route middlewares, that explains why the number is always the same.

For example, consider the following:

    r.Use(func(*gin.Context) { fmt.Println("FIRST") })
    r.GET("/foo", func(c *gin.Context) {
        c.Status(http.StatusOK)
    })

    r.Use(func(*gin.Context) { fmt.Println("SECOND") })
    r.GET("/bar", func(c *gin.Context) {
        c.Status(http.StatusOK)
    })

This prints:

[GIN-debug] GET    /foo    --> main.main.func2 (2 handlers)
[GIN-debug] GET    /bar    --> main.main.func4 (3 handlers)

Because /foo's chain has the FIRST middleware, and the handler itself (2), and /bar's chain has both FIRST and SECOND middleware, plus the handler itself (3).

  • Related