Route Rules

Overview

In go-zero, we declared HTTP service via api language, and then generated HTTP service code via goctl, after our systematic introduction to API norm.

In api description, there are specific routing rules that are not fully referenced to HTTP routing rules. Next, let’s take a look at the routing rules in api description.

Route Rules

In api description, routing needs to meet the following rules

  1. Route must start with /
  2. Route node must be separated by /
  3. A router node can contain :but : must be the first character of a router node,: The next node value must have path in the checkout body tag statement, used to receive routing parameters, refer to detailed rules routing parameters.
  4. Route nodes can contain letters, numbers (goctl 1.5.1 , referencenew API solver use), underscore, dash

Route Example:

  1. syntax = "v1"
  2. type DemoPath3Req {
  3. Id int64 `path:"id"`
  4. }
  5. type DemoPath4Req {
  6. Id int64 `path:"id"`
  7. Name string `path:"name"`
  8. }
  9. type DemoPath5Req {
  10. Id int64 `path:"id"`
  11. Name string `path:"name"`
  12. Age int `path:"age"`
  13. }
  14. type DemoReq {}
  15. type DemoResp {}
  16. service Demo {
  17. // route example /foo
  18. @handler demoPath1
  19. get /foo (DemoReq) returns (DemoResp)
  20. // route example /foo/bar
  21. @handler demoPath2
  22. get /foo/bar (DemoReq) returns (DemoResp)
  23. // route example /foo/bar/:id,where id is a field in the request body
  24. @handler demoPath3
  25. get /foo/bar/:id (DemoPath3Req) returns (DemoResp)
  26. // route example /foo/bar/:id/:name,where id and name are fields in the request body
  27. @handler demoPath4
  28. get /foo/bar/:id/:name (DemoPath4Req) returns (DemoResp)
  29. // route example /foo/bar/:id/:name/:age,where id, name, age are the fields in the request body
  30. @handler demoPath5
  31. get /foo/bar/:id/:name/:age (DemoPath5Req) returns (DemoResp)
  32. // route example /foo/bar/baz-qux
  33. @handler demoPath6
  34. get /foo/bar/baz-qux (DemoReq) returns (DemoResp)
  35. // route example/foo/bar_baz/123(support version after goctl 1.5.1)
  36. @handler demoPath7
  37. get /foo/bar_baz/123 (DemoReq) returns (DemoResp)
  38. }