Skip to content

Commit 8d03cea

Browse files
authored
fix: hertz getting-started uses wrong IDL file (cloudwego#1372)
1 parent 67899d4 commit 8d03cea

File tree

2 files changed

+77
-25
lines changed

2 files changed

+77
-25
lines changed

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

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,19 @@ For more information on how to use hz, please refer to: [hz](/zh/docs/hertz/tuto
119119
- Code generation by specifying an already defined idl file, e.g. `hz new -idl hello.thrift`.
120120

121121
```thrift
122-
namespace go hello.world
123-
122+
// idl/hello.thrift
123+
namespace go hello.example
124+
125+
struct HelloReq {
126+
1: string Name (api.query="name"); // Add api annotation for easy parameter binding
127+
}
128+
129+
struct HelloResp {
130+
1: string RespBody;
131+
}
132+
124133
service HelloService {
125-
string Hello(1: string name) (api.get="/hello");
134+
HelloResp HelloMethod(1: HelloReq request) (api.get="/hello");
126135
}
127136
```
128137
@@ -150,30 +159,41 @@ If the server is launched successfully, you will see following message:
150159
2022/05/17 21:47:09.629874 transport.go:84: [Info] HERTZ: HTTP server listening on address=[::]:8888
151160
```
152161

153-
Then, we can test the interface:
162+
Then, we can test the `ping` interface:
154163

155164
```bash
156165
curl http://127.0.0.1:8888/ping
166+
{"message":"pong"}
157167
```
158168

159-
If nothing goes wrong, we can see the following output:
169+
To test the `hello` interface:
160170

161171
```bash
162-
{"message":"pong"}
172+
curl http://127.0.0.1:8888/hello?name=bob
173+
{"RespBody":""}
163174
```
164175

165-
You have now successfully launched Hertz Server successfully and completed an API call.
176+
You have now successfully launched Hertz Server successfully and completed two API calls.
166177

167178
### Updating project code
168179

169180
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.
170181

171182
```thrift
172-
namespace go hello.world
183+
// idl/hello.thrift
184+
namespace go hello.example
185+
186+
struct HelloReq {
187+
1: string Name (api.query="name"); // 添加 api 注解为方便进行参数绑定
188+
}
189+
190+
struct HelloResp {
191+
1: string RespBody;
192+
}
173193
174194
service HelloService {
175-
string Hello(1: string name) (api.get="/hello");
176-
string Bye(1: string name) (api.get="/bye");
195+
HelloResp HelloMethod(1: HelloReq request) (api.get="/hello");
196+
HelloResp ByeMethod(1: HelloReq request) (api.get="/bye");
177197
}
178198
```
179199

@@ -183,6 +203,12 @@ At this point, run `hz update` from the project root directory to update the pro
183203
hz update -idl hello.thrift
184204
```
185205

206+
Compile & launch the server again.
207+
208+
```bash
209+
go build -o hertz_demo && ./hertz_demo
210+
```
211+
186212
## More examples
187213

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

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

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,22 @@ hz 是 Hertz 框架提供的一个用于生成代码的命令行工具,可以
116116
- 直接使用 `hz new`,若当前不在 `GOPATH`,需要添加 `-module` 或者 `-mod` flag 指定一个自定义的模块名称。详细参考[这里](/zh/docs/hertz/tutorials/toolkit/usage/)
117117
- 通过指定已经定义好的 idl 文件进行代码生成,例如 `hz new -idl hello.thrift`
118118

119-
```thrift
120-
namespace go hello.world
121-
122-
service HelloService {
123-
string Hello(1: string name) (api.get="/hello");
124-
}
125-
```
119+
```thrift
120+
// idl/hello.thrift
121+
namespace go hello.example
122+
123+
struct HelloReq {
124+
1: string Name (api.query="name"); // 添加 api 注解为方便进行参数绑定
125+
}
126+
127+
struct HelloResp {
128+
1: string RespBody;
129+
}
130+
131+
service HelloService {
132+
HelloResp HelloMethod(1: HelloReq request) (api.get="/hello");
133+
}
134+
```
126135
127136
执行完毕后, 会在当前目录下生成 Hertz 项目的脚手架, 自带一个 `ping` 接口用于测试。
128137
@@ -148,30 +157,41 @@ go build -o hertz_demo && ./hertz_demo
148157
2022/05/17 21:47:09.629874 transport.go:84: [Info] HERTZ: HTTP server listening on address=[::]:8888
149158
```
150159

151-
接下来,我们可以对接口进行测试
160+
接下来,我们可以对 `ping` 接口进行测试
152161

153162
```bash
154163
curl http://127.0.0.1:8888/ping
164+
{"message":"pong"}
155165
```
156166

157-
如果不出意外,我们可以看到类似如下输出
167+
接下来,我们可以对 `hello` 接口进行测试
158168

159169
```bash
160-
{"message":"pong"}
170+
curl http://127.0.0.1:8888/hello?name=bob
171+
{"RespBody":""}
161172
```
162173

163-
到现在,我们已经成功启动了 Hertz Server,并完成了一次调用
174+
到现在,我们已经成功启动了 Hertz Server,并完成了两次调用
164175

165176
### 更新项目代码
166177

167-
如果需要对项目进行进一步的更新, 应使用 `hz update` 命令, 这里以添加一个 `Bye` 方法为例。
178+
如果需要对项目进行进一步的更新, 应使用 `hz update` 命令, 这里以添加一个 `ByeMethod` 方法为例。
168179

169180
```thrift
170-
namespace go hello.world
181+
// idl/hello.thrift
182+
namespace go hello.example
183+
184+
struct HelloReq {
185+
1: string Name (api.query="name"); // 添加 api 注解为方便进行参数绑定
186+
}
187+
188+
struct HelloResp {
189+
1: string RespBody;
190+
}
171191
172192
service HelloService {
173-
string Hello(1: string name) (api.get="/hello");
174-
string Bye(1: string name) (api.get="/bye");
193+
HelloResp HelloMethod(1: HelloReq request) (api.get="/hello");
194+
HelloResp ByeMethod(1: HelloReq request) (api.get="/bye");
175195
}
176196
```
177197

@@ -181,6 +201,12 @@ service HelloService {
181201
hz update -idl hello.thrift
182202
```
183203

204+
重新编译并启动 Server。
205+
206+
```bash
207+
go build -o hertz_demo && ./hertz_demo
208+
```
209+
184210
## 更多示例
185211

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

0 commit comments

Comments
 (0)