Print Routes

Using =web.PrintTree() to print all routes:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/server/web"
  5. )
  6. type UserController struct {
  7. web.Controller
  8. }
  9. func (u *UserController) HelloWorld() {
  10. u.Ctx.WriteString("hello, world")
  11. }
  12. func main() {
  13. web.BConfig.RouterCaseSensitive = false
  14. web.AutoRouter(&UserController{})
  15. tree := web.PrintTree()
  16. methods := tree["Data"].(web.M)
  17. for k, v := range methods {
  18. fmt.Printf("%s => %v\n", k, v)
  19. }
  20. }

If you register a route that uses * as a method, which means it matches any HTTP method, then it will print out one for each method. The AutoRouter is the one that matches any HTTP method, so it will end up printing out a bunch of things.

  1. MKCOL => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  2. CONNECT => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  3. POST => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  4. UNLOCK => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  5. PROPFIND => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  6. PATCH => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  7. GET => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  8. DELETE => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  9. PROPPATCH => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  10. COPY => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  11. OPTIONS => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  12. HEAD => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  13. LOCK => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  14. PUT => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  15. TRACE => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]
  16. MOVE => &[[/user/helloworld/* map[*:HelloWorld] main.UserController]]

Let’s use POST => &[[/user/helloworld/* map[*:HelloWorld] main.UserController] as an example to show how to interpret. It means that the POST method accesses the path of the pattern /user/helloworld/*, then it will execute the HelloWorld method inside main.UserController.

Reference