路由规则
概述
在 go-zero 中,我们通过 api 语言来声明 HTTP 服务,然后通过 goctl 生成 HTTP 服务代码,在之前我们系统性的介绍了 API 规范。
在 api 描述语言中,有特定的路由规则,这些路由规则并非和 HTTP 的路由规则完全引用,接下来我们来看一下 api 描述语言中的路由规则吧。
路由规则
在 api 描述语言中,路由需要满足如下规则
- 路由必须以
/开头 - 路由节点必须以
/分隔 - 路由节点中可以包含
:,但是:必须是路由节点的第一个字符,:后面的节点值必须要在结请求体中有pathtag 声明,用于接收路由参数,详细规则可参考 路由参数。 - 路由节点可以包含字母、数字(
goctl 1.5.1支持,可参考新版 API 解析器使用)、下划线、中划线
路由示例:
syntax = "v1"type DemoPath3Req {Id int64 `path:"id"`}type DemoPath4Req {Id int64 `path:"id"`Name string `path:"name"`}type DemoPath5Req {Id int64 `path:"id"`Name string `path:"name"`Age int `path:"age"`}type DemoReq {}type DemoResp {}service Demo {// 示例路由 /foo@handler demoPath1get /foo (DemoReq) returns (DemoResp)// 示例路由 /foo/bar@handler demoPath2get /foo/bar (DemoReq) returns (DemoResp)// 示例路由 /foo/bar/:id,其中 id 为请求体中的字段@handler demoPath3get /foo/bar/:id (DemoPath3Req) returns (DemoResp)// 示例路由 /foo/bar/:id/:name,其中 id,name 为请求体中的字段@handler demoPath4get /foo/bar/:id/:name (DemoPath4Req) returns (DemoResp)// 示例路由 /foo/bar/:id/:name/:age,其中 id,name,age 为请求体中的字段@handler demoPath5get /foo/bar/:id/:name/:age (DemoPath5Req) returns (DemoResp)// 示例路由 /foo/bar/baz-qux@handler demoPath6get /foo/bar/baz-qux (DemoReq) returns (DemoResp)// 示例路由 /foo/bar_baz/123(goctl 1.5.1 支持)@handler demoPath7get /foo/bar_baz/123 (DemoReq) returns (DemoResp)}