只需要写普通的函数,Gen会为其生成高效的路由源代码和文档
因为生成的是源码,所以这些都不会影响运行时性能
工具中的每个变化所引起的差异直接显示在生成的源代码中
也支持生成客户端
'#'是注释,注释是golang tag语法,这里唯一的区别是'#' 包裹而不是 '`'
// ItemService #path:"/item/"#
type ItemService struct {}
// Create a Item #route:"POST /"#
func (s *ItemService) Create(item *Item) (err error) {}
// Update the Item #route:"PUT /{item_id}"#
func (s *ItemService) Update(itemID int /* #name:"item_id"# */, item *Item) (err error) {}
// Delete the Item #route:"DELETE /{item_id}"#
func (s *ItemService) Delete(itemID int /* #name:"item_id"# */) (err error) {}
// Get the Item #route:"GET /{item_id}"#
func (s *ItemService) Get(itemID int /* #name:"item_id"# */) (item *ItemWithID, err error) {}
// List of the Item #route:"GET /"#
func (s *ItemService) List(offset, limit int) (items []*ItemWithID, err error) {}
- 安装 gen 工具
go get -v github.com/wzshiming/gen/cmd/gen
- 添加 gen 工具到 $PATH
- 启动
gen run github.com/wzshiming/gen-examples/service/...
- 在浏览器中打开 http://127.0.0.1:8080/swagger/?url=./openapi.json#
或者尝试从零快速搭建web服务
- 新建目录
mkdir -p $(go env GOPATH)/src/gentest
- 移动到刚创建的目录
cd $(go env GOPATH)/src/gentest/
- 定义数据类型
cat > models.go <<EOF
package gentest
type Gentest struct {
Name string \`json:"name"\`
Age int \`json:"age"\`
}
EOF
- 根据CRUD模板生成
gen crud -t mock -n Gentest
- 启动
GO111MODULE=off gen run gentest
- 生成文档
- RESTful
- 生成Go路由
- Security
- apiKey
- http
- basic
- bearer
- oauth2
- openIdConnet
- Content
- Query
- Path
- Header
- Cookie
- Body
- JSON
- XML
- Formdata
- File
- Value
- URLEncode
- Security
- 生成Go客户端
- Security
- apiKey
- http
- basic
- bearer
- oauth2
- openIdConnet
- Content
- Query
- Path
- Header
- Cookie
- Body
- JSON
- XML
- Formdata
- File
- Value
- URLEncode
- Security
- 其他客户端
- 生成Go路由
- gRPC & Proto3
软包根据MIT License。有关完整的许可证文本,请参阅LICENSE。