goctl-swagger/README.md

123 lines
3.1 KiB
Markdown
Raw Normal View History

2021-01-01 20:30:52 +08:00
# goctl-swagger
### 1. 编译goctl-swagger插件
```
2024-12-18 12:08:19 +08:00
GOPROXY=https://goproxy.cn/,direct go install github.com/devttl/goctl-swagger@latest
2021-01-01 20:30:52 +08:00
```
### 2. 配置环境
2022-03-24 19:20:24 +08:00
2021-01-01 20:30:52 +08:00
将$GOPATH/bin中的goctl-swagger添加到环境变量
### 3. 使用姿势
* 创建api文件
2022-03-24 19:20:24 +08:00
2021-01-01 20:30:52 +08:00
```go
info(
2022-03-24 19:20:24 +08:00
title: "type title here"
desc: "type desc here"
author: "type author here"
email: "type email here"
version: "type version here"
2021-01-01 20:30:52 +08:00
)
type (
2022-03-24 19:20:24 +08:00
RegisterReq {
Username string `json:"username"`
Password string `json:"password"`
Mobile string `json:"mobile"`
}
LoginReq {
Username string `json:"username"`
Password string `json:"password"`
}
UserInfoReq {
Id string `path:"id"`
}
UserInfoReply {
Name string `json:"name"`
Age int `json:"age"`
Birthday string `json:"birthday"`
Description string `json:"description"`
Tag []string `json:"tag"`
}
UserSearchReq {
KeyWord string `form:"keyWord"`
}
2021-01-01 20:30:52 +08:00
)
service user-api {
2022-03-24 19:20:24 +08:00
@doc(
summary: "注册"
)
@handler register
post /api/user/register (RegisterReq)
@doc(
summary: "登录"
)
@handler login
post /api/user/login (LoginReq)
@doc(
summary: "获取用户信息"
)
@handler getUserInfo
get /api/user/:id (UserInfoReq) returns (UserInfoReply)
@doc(
summary: "用户搜索"
)
@handler searchUser
get /api/user/search (UserSearchReq) returns (UserInfoReply)
2021-01-01 20:30:52 +08:00
}
```
2022-03-24 19:20:24 +08:00
2021-01-01 20:30:52 +08:00
* 生成swagger.json 文件
2022-03-24 19:20:24 +08:00
2021-01-01 20:30:52 +08:00
```shell script
2022-03-24 19:20:24 +08:00
goctl api plugin -plugin goctl-swagger="swagger -filename user.json" -api user.api -dir .
2021-01-01 20:30:52 +08:00
```
2022-03-24 19:20:24 +08:00
2024-12-18 14:19:21 +08:00
* -pack 开启外层响应包装并指定外层响应结构名称
```bash
goctl api plugin -plugin goctl-swagger='swagger -filename user.json -pack Response' -api user.api -dir .
```
- 使用 -response指定相应结构
```bash
goctl api plugin -plugin goctl-swagger='swagger -filename user.json -pack Response -response "[{\"name\":\"trace_id\",\"type\":\"string\",\"description\":\"链路追踪id\"},{\"name\":\"code\",\"type\":\"integer\",\"description\":\"状态码\"},{\"name\":\"msg\",\"type\":\"string\",\"description\":\"消息\"},{\"name\":\"data\",\"type\":\"object\",\"description\":\"数据\",\"is_data\":true}]";' -api user.api -dir .
```
2023-11-19 12:39:59 +08:00
* 指定HostbasePathschemes [api-host-and-base-path](https://swagger.io/docs/specification/2-0/api-host-and-base-path/)
2022-03-24 19:20:24 +08:00
2021-07-30 14:33:10 +08:00
```shell script
2023-11-19 12:39:59 +08:00
goctl api plugin -plugin goctl-swagger="swagger -filename user.json -host 127.0.0.2 -basepath /api -schemes https,wss" -api user.api -dir .
2021-07-30 14:33:10 +08:00
```
2022-03-24 19:20:24 +08:00
2021-01-22 09:48:46 +08:00
* swagger ui 查看生成的文档
2022-03-24 19:20:24 +08:00
2021-01-22 09:48:46 +08:00
```shell script
2022-03-24 19:20:24 +08:00
docker run --rm -p 8083:8080 -e SWAGGER_JSON=/foo/user.json -v $PWD:/foo swaggerapi/swagger-ui
2021-01-22 09:48:46 +08:00
```
2022-03-24 19:20:24 +08:00
2021-01-22 09:50:32 +08:00
* Swagger Codegen 生成客户端调用代码(go,javascript,php)
2022-03-24 19:20:24 +08:00
2021-01-22 09:48:46 +08:00
```shell script
2021-01-22 09:50:32 +08:00
for l in go javascript php; do
docker run --rm -v "$(pwd):/go-work" swaggerapi/swagger-codegen-cli generate \
-i "/go-work/rest.swagger.json" \
-l "$l" \
-o "/go-work/clients/$l"
done
2024-12-18 14:19:21 +08:00
```