update quick start (#63)
* rebase upstream * rebase * trim no need line * trim no need line * trim no need line * update doc * update readme.md Co-authored-by: kingxt <dream4kingxt@163.com>
This commit is contained in:
@@ -7,6 +7,11 @@
|
|||||||
* 生成MongoDB CURD+Cache
|
* 生成MongoDB CURD+Cache
|
||||||
|
|
||||||
## goctl使用说明
|
## goctl使用说明
|
||||||
|
|
||||||
|
#### 快速生成服务
|
||||||
|
* api: goctl api new xxxx
|
||||||
|
* rpc: goctl rpc new xxxx
|
||||||
|
|
||||||
#### goctl参数说明
|
#### goctl参数说明
|
||||||
|
|
||||||
`goctl api [go/java/ts] [-api user/user.api] [-dir ./src]`
|
`goctl api [go/java/ts] [-api user/user.api] [-dir ./src]`
|
||||||
|
|||||||
57
readme.md
57
readme.md
@@ -107,61 +107,10 @@ go get -u github.com/tal-tech/go-zero
|
|||||||
|
|
||||||
确保goctl可执行
|
确保goctl可执行
|
||||||
|
|
||||||
2. 定义API文件,比如greet.api,可以在vs code里安装`goctl`插件,支持api语法
|
2. 快速生成api服务
|
||||||
|
|
||||||
```go
|
|
||||||
type Request struct {
|
|
||||||
Name string `path:"name,options=you|me"` // 框架自动验证请求参数是否合法
|
|
||||||
}
|
|
||||||
|
|
||||||
type Response struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
}
|
|
||||||
|
|
||||||
service greet-api {
|
|
||||||
@server(
|
|
||||||
handler: GreetHandler
|
|
||||||
)
|
|
||||||
get /greet/from/:name(Request) returns (Response);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
也可以通过goctl生成api模本文件,命令如下:
|
|
||||||
|
|
||||||
```shell
|
|
||||||
goctl api -o greet.api
|
|
||||||
```
|
|
||||||
|
|
||||||
3. 生成go服务端代码
|
|
||||||
|
|
||||||
```shell
|
|
||||||
goctl api go -api greet.api -dir greet
|
|
||||||
```
|
|
||||||
|
|
||||||
生成的文件结构如下:
|
|
||||||
|
|
||||||
```
|
|
||||||
├── greet
|
|
||||||
│ ├── etc
|
|
||||||
│ │ └── greet-api.yaml // 配置文件
|
|
||||||
│ ├── greet.go // main文件
|
|
||||||
│ └── internal
|
|
||||||
│ ├── config
|
|
||||||
│ │ └── config.go // 配置定义
|
|
||||||
│ ├── handler
|
|
||||||
│ │ ├── greethandler.go // get/put/post/delete等路由定义文件
|
|
||||||
│ │ └── routes.go // 路由列表
|
|
||||||
│ ├── logic
|
|
||||||
│ │ └── greetlogic.go // 请求逻辑处理文件
|
|
||||||
│ ├── svc
|
|
||||||
│ │ └── servicecontext.go // 请求上下文,可以传入mysql, redis等依赖
|
|
||||||
│ └── types
|
|
||||||
│ └── types.go // 请求、返回等类型定义
|
|
||||||
└── greet.api // api描述文件
|
|
||||||
```
|
|
||||||
生成的代码可以直接运行:
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
|
goctl api new greet
|
||||||
cd greet
|
cd greet
|
||||||
go run greet.go -f etc/greet-api.yaml
|
go run greet.go -f etc/greet-api.yaml
|
||||||
```
|
```
|
||||||
@@ -185,7 +134,7 @@ Content-Length: 0
|
|||||||
* 可以在servicecontext.go里面传递依赖给logic,比如mysql, redis等
|
* 可以在servicecontext.go里面传递依赖给logic,比如mysql, redis等
|
||||||
* 在api定义的get/post/put/delete等请求对应的logic里增加业务处理逻辑
|
* 在api定义的get/post/put/delete等请求对应的logic里增加业务处理逻辑
|
||||||
|
|
||||||
4. 可以根据api文件生成前端需要的Java, TypeScript, Dart, JavaScript代码
|
3. 可以根据api文件生成前端需要的Java, TypeScript, Dart, JavaScript代码
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
goctl api java -api greet.api -dir greet
|
goctl api java -api greet.api -dir greet
|
||||||
|
|||||||
Reference in New Issue
Block a user