Skip to content

Commit 83b80c7

Browse files
docs: update hz usage document (#787)
Co-authored-by: copy rogers <[email protected]>
1 parent 0da25c9 commit 83b80c7

File tree

8 files changed

+222
-136
lines changed

8 files changed

+222
-136
lines changed

content/en/docs/hertz/getting-started/_index.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,18 @@ For more information on how to use hz, please refer to: [hz](https://www.cloudwe
111111
### Generate/Complete the Sample Code
112112

113113
1. Create the hertz_demo folder in the current directory and go to that directory.
114-
2. Generate code `hz new`. If your codes are not placed under `GOPATH`, you need to refer [here](https://www.cloudwego.io/docs/hertz/tutorials/toolkit/usage/) and add `-module` (or `-mod`) flag to name your custom module.
114+
2. Generating code
115+
- Use `hz new` directly, if not currently in `GOPATH`, you need to add `-module` or `-mod` flag to specify a custom module name. See [here](https://www.cloudwego.io/docs/hertz/tutorials/toolkit/usage/) for details.
116+
- Code generation by specifying an already defined idl file, e.g. `hz new -idl hello.thrift`.
117+
```thrift
118+
namespace go hello.world
119+
120+
service HelloService {
121+
string Hello(1: string name);
122+
}
123+
```
124+
After execution, a scaffolding of the Hertz project is created in the current directory, with a `ping` interface for testing.
125+
115126
3. Tidy & get dependencies.
116127
117128
```bash
@@ -148,6 +159,25 @@ If nothing goes wrong, we can see the following output:
148159

149160
You have now successfully launched Hertz Server successfully and completed an API call.
150161

162+
### Updating project code
163+
164+
If you need to make further updates to the project, you should use the `hz update` command, here is an example of adding a `Bye` method.
165+
166+
```thrift
167+
namespace go hello.world
168+
169+
service HelloService {
170+
string Hello(1: string name);
171+
string Bye(1: string name);
172+
}
173+
```
174+
175+
At this point, run `hz update` from the project root directory to update the project.
176+
177+
```bash
178+
hz update -idl hello.thrift
179+
```
180+
151181
## More examples
152182

153183
Please refer:[Example code](/docs/hertz/tutorials/example/)

content/en/docs/hertz/tutorials/toolkit/usage-protobuf.md

Lines changed: 74 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -29,71 +29,71 @@ description: "hz usage(protobuf)."
2929
option go_package = "/api";
3030
3131
extend google.protobuf.FieldOptions {
32-
optional string raw_body = 50101;
33-
optional string query = 50102;
34-
optional string header = 50103;
35-
optional string cookie = 50104;
36-
optional string body = 50105;
37-
optional string path = 50106;
38-
optional string vd = 50107;
39-
optional string form = 50108;
40-
optional string js_conv = 50109;
41-
optional string file_name = 50110;
42-
optional string none = 50111;
43-
44-
// 50131~50160 used to extend field option by hz
45-
optional string form_compatible = 50131;
46-
optional string js_conv_compatible = 50132;
47-
optional string file_name_compatible = 50133;
48-
optional string none_compatible = 50134;
49-
// 50135 is reserved to vt_compatible
50-
// optional FieldRules vt_compatible = 50135;
51-
52-
optional string go_tag = 51001;
32+
optional string raw_body = 50101;
33+
optional string query = 50102;
34+
optional string header = 50103;
35+
optional string cookie = 50104;
36+
optional string body = 50105;
37+
optional string path = 50106;
38+
optional string vd = 50107;
39+
optional string form = 50108;
40+
optional string js_conv = 50109;
41+
optional string file_name = 50110;
42+
optional string none = 50111;
43+
44+
// 50131~50160 used to extend field option by hz
45+
optional string form_compatible = 50131;
46+
optional string js_conv_compatible = 50132;
47+
optional string file_name_compatible = 50133;
48+
optional string none_compatible = 50134;
49+
// 50135 is reserved to vt_compatible
50+
// optional FieldRules vt_compatible = 50135;
51+
52+
optional string go_tag = 51001;
5353
}
5454
5555
extend google.protobuf.MethodOptions {
56-
optional string get = 50201;
57-
optional string post = 50202;
58-
optional string put = 50203;
59-
optional string delete = 50204;
60-
optional string patch = 50205;
61-
optional string options = 50206;
62-
optional string head = 50207;
63-
optional string any = 50208;
64-
optional string gen_path = 50301; // The path specified by the user when the client code is generated, with a higher priority than api_version
65-
optional string api_version = 50302; // Specify the value of the :version variable in path when the client code is generated
66-
optional string tag = 50303; // rpc tag, can be multiple, separated by commas
67-
optional string name = 50304; // Name of rpc
68-
optional string api_level = 50305; // Interface Level
69-
optional string serializer = 50306; // Serialization method
70-
optional string param = 50307; // Whether client requests take public parameters
71-
optional string baseurl = 50308; // Baseurl used in ttnet routing
72-
optional string handler_path = 50309; // handler_path specifies the path to generate the method
73-
74-
// 50331~50360 used to extend method option by hz
75-
optional string handler_path_compatible = 50331; // handler_path specifies the path to generate the method
56+
optional string get = 50201;
57+
optional string post = 50202;
58+
optional string put = 50203;
59+
optional string delete = 50204;
60+
optional string patch = 50205;
61+
optional string options = 50206;
62+
optional string head = 50207;
63+
optional string any = 50208;
64+
optional string gen_path = 50301; // The path specified by the user when the client code is generated, with a higher priority than api_version
65+
optional string api_version = 50302; // Specify the value of the :version variable in path when the client code is generated
66+
optional string tag = 50303; // rpc tag, can be multiple, separated by commas
67+
optional string name = 50304; // Name of rpc
68+
optional string api_level = 50305; // Interface Level
69+
optional string serializer = 50306; // Serialization method
70+
optional string param = 50307; // Whether client requests take public parameters
71+
optional string baseurl = 50308; // Baseurl used in ttnet routing
72+
optional string handler_path = 50309; // handler_path specifies the path to generate the method
73+
74+
// 50331~50360 used to extend method option by hz
75+
optional string handler_path_compatible = 50331; // handler_path specifies the path to generate the method
7676
}
7777
7878
extend google.protobuf.EnumValueOptions {
79-
optional int32 http_code = 50401;
79+
optional int32 http_code = 50401;
8080
8181
// 50431~50460 used to extend enum option by hz
8282
}
8383
8484
extend google.protobuf.ServiceOptions {
85-
optional string base_domain = 50402;
85+
optional string base_domain = 50402;
8686
87-
// 50731~50760 used to extend service option by hz
88-
optional string base_domain_compatible = 50731;
87+
// 50731~50760 used to extend service option by hz
88+
optional string base_domain_compatible = 50731;
8989
}
9090
9191
extend google.protobuf.MessageOptions {
92-
// optional FieldRules msg_vt = 50111;
93-
94-
optional string reserve = 50830;
95-
// 550831 is reserved to msg_vt_compatible
96-
// optional FieldRules msg_vt_compatible = 50831;
92+
// optional FieldRules msg_vt = 50111;
93+
94+
optional string reserve = 50830;
95+
// 550831 is reserved to msg_vt_compatible
96+
// optional FieldRules msg_vt_compatible = 50831;
9797
}
9898
```
9999
@@ -255,34 +255,37 @@ description: "hz usage(protobuf)."
255255
```bash
256256
hz update -I idl -idl idl/hello/hello.proto
257257
```
258+
**Note**.
259+
- If the dependency of the IDL is not in the same path as the main IDL, you need to add the `-I` option, which means the path to search for the IDL, equivalent to the `-I` command of protoc.
260+
- When writing update command, you need to specify not only the IDL file that defines `service`, but also all the dependency files, because protobuf's dependency files will not be updated automatically.
258261

259-
3. As you can see
262+
3. As you can see
260263

261-
Add new method under "biz/handler/hello/hello_service.go";
262-
The file "new_service.go" and the corresponding "Method3" method have been added under "biz/handler/hello".
264+
Add new method under `biz/handler/hello/hello_service.go`;
265+
The file `new_service.go` and the corresponding "Method3" method have been added under `biz/handler/hello`.
263266

264-
Now let's develop the "Method2" interface:
267+
Now let's develop the "Method2" interface:
265268

266-
```go
267-
// Method2 .
268-
// @router /other [POST]
269-
func Method2(ctx context.Context, c *app.RequestContext) {
270-
var err error
271-
var req hello.OtherReq
272-
err = c.BindAndValidate(&req)
273-
if err != nil {
274-
c.String(400, err.Error())
275-
return
276-
}
269+
```go
270+
// Method2 .
271+
// @router /other [POST]
272+
func Method2(ctx context.Context, c *app.RequestContext) {
273+
var err error
274+
var req hello.OtherReq
275+
err = c.BindAndValidate(&req)
276+
if err != nil {
277+
c.String(400, err.Error())
278+
return
279+
}
277280

278-
resp := new(hello.OtherResp)
281+
resp := new(hello.OtherResp)
279282

280-
// added logic
281-
resp.Resp = "Other method: " + req.Other
283+
// added logic
284+
resp.Resp = "Other method: " + req.Other
282285

283-
c.JSON(200, resp)
284-
}
285-
```
286+
c.JSON(200, resp)
287+
}
288+
```
286289

287290
4. Compile the project
288291

content/en/docs/hertz/tutorials/toolkit/usage-thrift.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,13 @@ description: "hz usage(thrift)."
158158
hz update -idl idl/hello.thrift
159159
```
160160

161+
**Note**:
162+
1. When writing the update command, you only need to specify the IDL file that defines the `service`. hz will automatically generate all the dependencies for that file.
163+
161164
3. As you can see
162165

163-
Add new method under "biz/handler/hello/example/hello_service.go"<br>
164-
The file "new_service.go" and the corresponding "NewMethod" method have been added under "biz/handler/hello/example"
166+
Add new method under `biz/handler/hello/example/hello_service.go`<br>
167+
The file `new_service.go` and the corresponding "NewMethod" method have been added under `biz/handler/hello/example`.
165168

166169
Now let's develop the "OtherMethod" interface:
167170

@@ -213,4 +216,4 @@ description: "hz usage(thrift)."
213216

214217
If it returns `{"Resp":"Other method: other method"}`, it works.
215218

216-
For more example code, please refer to [code](https://github.com/cloudwego/hertz-examples/tree/main/hz/thrift).
219+
For more example code, please refer to [code](https://github.com/cloudwego/hertz-examples/tree/main/hz/thrift).

content/en/docs/hertz/tutorials/toolkit/usage.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ description: "hz basic usage."
6666
```
6767

6868
If it returns `{"message":"pong"}`, it works.
69+
70+
### Note
71+
72+
`hz new` is only used to initialize the project, use `hz update` to update the project.
73+
See [using protobuf](./usage-protobuf.md) and [use thrift](./usage-thrift.md).

content/zh/docs/hertz/getting-started/_index.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,19 @@ hz 是 Hertz 框架提供的一个用于生成代码的命令行工具,可以
109109
### 生成/编写示例代码
110110

111111
1. 在当前目录下创建 hertz_demo 文件夹,进入该目录中。
112-
2. 生成代码 `hz new`,若当前不在 `GOPATH`,需要添加 `-module` 或者 `-mod` flag 指定一个自定义的模块名称。详细参考[这里](https://www.cloudwego.io/zh/docs/hertz/tutorials/toolkit/usage/)
112+
113+
2. 生成代码
114+
- 直接使用 `hz new`,若当前不在 `GOPATH`,需要添加 `-module` 或者 `-mod` flag 指定一个自定义的模块名称。详细参考[这里](https://www.cloudwego.io/zh/docs/hertz/tutorials/toolkit/usage/)
115+
- 通过指定已经定义好的 idl 文件进行代码生成,例如 `hz new -idl hello.thrift`
116+
```thrift
117+
namespace go hello.world
118+
119+
service HelloService {
120+
string Hello(1: string name);
121+
}
122+
```
123+
执行完毕后, 会在当前目录下生成 Hertz 项目的脚手架, 自带一个 `ping` 接口用于测试。
124+
113125
3. 整理 & 拉取依赖。
114126
115127
```bash
@@ -146,6 +158,25 @@ curl http://127.0.0.1:8888/ping
146158

147159
到现在,我们已经成功启动了 Hertz Server,并完成了一次调用。
148160

161+
### 更新项目代码
162+
163+
如果需要对项目进行进一步的更新, 应使用 `hz update` 命令, 这里以添加一个 `Bye` 方法为例。
164+
165+
```thrift
166+
namespace go hello.world
167+
168+
service HelloService {
169+
string Hello(1: string name);
170+
string Bye(1: string name);
171+
}
172+
```
173+
174+
此时在项目根目录执行 `hz update` 更新项目。
175+
176+
```bash
177+
hz update -idl hello.thrift
178+
```
179+
149180
## 更多示例
150181

151182
参考:[代码示例](/zh/docs/hertz/tutorials/example/)

0 commit comments

Comments
 (0)