Skip to content

Commit f63ff55

Browse files
authored
fix: improve readability of websocket and swagger docs (#1480)
1 parent 967538e commit f63ff55

File tree

4 files changed

+94
-71
lines changed

4 files changed

+94
-71
lines changed

content/en/docs/hertz/tutorials/third-party/middleware/swagger.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Swagger"
33
date: 2025-12-04
44
weight: 8
55
keywords: ["Swagger", "RESTful API"]
6-
description: "Hertz middleware to automatically generate RESTful API documentation with Swagger 2.0."
6+
description: "Generate RESTful API documentation with the official Swagger toolchain and HTTP adaptor."
77
---
88

99
> **⚠️ Deprecated**
@@ -28,26 +28,29 @@ github.com/swaggo/files
2828
go install github.com/swaggo/swag/cmd/swag@latest
2929
```
3030

31-
3. Replace swagger handler
32-
33-
If the project has already been set up, replace the code below.
31+
3. Use hertz adaptor
3432

3533
```go
36-
// Before (using hertz-contrib/swagger)
34+
// Before (deprecated: hertz-contrib/swagger)
3735
import "github.com/hertz-contrib/swagger"
3836
import swaggerFiles "github.com/swaggo/files"
3937

4038
url := swagger.URL("http://localhost:8888/swagger/doc.json")
4139
h.GET("/swagger/*any", swagger.WrapHandler(swaggerFiles.Handler, url))
40+
```
4241

43-
// After (using http adaptor)
42+
```go
43+
// After (recommended: adaptor)
4444
import "github.com/cloudwego/hertz/pkg/common/adaptor"
4545
import httpSwagger "github.com/swaggo/http-swagger"
4646

4747
h.GET("/swagger/*any", adaptor.HertzHandler(httpSwagger.WrapHandler))
4848
```
4949

50-
If starting a new project, follow the sample code below as an example.
50+
## Example Usage
51+
52+
The following example demonstrates how to integrate Swagger documentation and Swagger UI
53+
into a Hertz application using the Hertz HTTP adaptor.
5154

5255
```go
5356
package main
@@ -105,13 +108,13 @@ func main() {
105108
}
106109
```
107110

108-
4. Generate Swagger docs
111+
1. Generate Swagger docs
109112

110113
```sh
111114
swag init
112115
```
113116

114-
5. Run program
117+
2. Run program
115118

116119
Start the Hertz server.
117120

@@ -121,18 +124,19 @@ go run main.go
121124

122125
On a web browser, go to http://localhost:8888/swagger/index.html or whichever address that is configured to see the Swagger UI.
123126

124-
6. Provide custom Swagger UI
127+
### Custom Swagger UI
125128

126129
For users who want to create their own custom UI, they will need to download the Swagger UI dist files, and serve the UI files as static assets.
127-
Copy the following files from [swagger-ui/dist](https://github.com/swagger-api/swagger-ui/tree/master/dist) and place them in `swagger-ui/`.
130+
131+
1. Copy the following files from [swagger-ui/dist](https://github.com/swagger-api/swagger-ui/tree/master/dist) and place them in `swagger-ui/`.
128132

129133
- https://github.com/swagger-api/swagger-ui/blob/master/dist/favicon-16x16.png
130134
- https://github.com/swagger-api/swagger-ui/blob/master/dist/favicon-32x32.png
131135
- https://github.com/swagger-api/swagger-ui/blob/master/dist/swagger-ui.css
132136
- https://github.com/swagger-api/swagger-ui/blob/master/dist/swagger-ui-bundle.js
133137
- https://github.com/swagger-api/swagger-ui/blob/master/dist/swagger-ui-standalone-preset.js
134138

135-
Create an `index.html` file in the same directory with the following template. The original configuration options present in the old `hertz-contrib/swagger` middleware can be directly configured in the HTML file.
139+
2. Create an `index.html` file in the same directory with the following template. The original configuration options present in the old `hertz-contrib/swagger` middleware can be directly configured in the HTML file.
136140

137141
**Note: The HTML file below is just a sample and should be modified accordingly to the user's needs.**
138142

@@ -259,7 +263,7 @@ project/
259263
└── go.sum
260264
```
261265

262-
7. Serve static files
266+
3. Serve static files
263267

264268
Add this line into `main.go`
265269

@@ -274,7 +278,7 @@ h.Spin()
274278
// Rest of code
275279
```
276280

277-
8. Run program
281+
4. Run program
278282

279283
```sh
280284
go run main.go
@@ -288,7 +292,7 @@ Hertz middleware to automatically generate RESTful API documentation with Swagge
288292

289293
The implementation of the [swagger](https://github.com/hertz-contrib/swagger) extension refers to the implementation of [Gin](https://github.com/swaggo/gin-swagger).
290294

291-
## Usage
295+
### Usage
292296

293297
1. Add comments to your API source code, See [Declarative Comments Format](https://github.com/swaggo/swag/blob/master/README.md#declarative-comments-format).
294298

@@ -341,7 +345,7 @@ import "github.com/hertz-contrib/swagger" // hertz-swagger middleware
341345
import "github.com/swaggo/files" // swagger embed files
342346
```
343347

344-
## Example
348+
### Example
345349

346350
Now assume you have implemented a simple api as following:
347351

@@ -450,11 +454,11 @@ Demo project tree, `swag init` is run at relative `.`.
450454
└── main.go
451455
```
452456

453-
## Multiple APIs
457+
### Multiple APIs
454458

455459
This feature was introduced in swag v1.7.9.
456460

457-
## Configuration
461+
### Configuration
458462

459463
You can configure Swagger using different configuration options.
460464

content/en/docs/hertz/tutorials/third-party/protocol/websocket.md

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@ keywords: ["WebSocket", "HTTP", "hijack", "TCP"]
66
description: "Hertz implements support for Gorilla WebSocket based on hijack."
77
---
88

9-
WebSocket is a type of full-duplex communication that can be performed on a single TCP connection and is located at the application layer of the OSI model.
10-
WebSocket makes data exchange between client and server easier, allowing the server to actively push data to the client.
11-
In the WebSocket API, the browser and the server only need to complete a handshake that a persistent connection can be created between the two, and two-way data transmission can be performed.
12-
13-
Hertz provides support for WebSocket and adapts it in Hertz by referring to [gorilla/websocket](https://github.com/gorilla/websocket) using `hijack`.
14-
The usage and parameters are basically the same.
15-
169
> **⚠️ Deprecated**
1710
>
1811
> The `hertz-contrib/websocket` middleware is now deprecated.
1912
> Hertz recommends all users to migrate to the official `gorilla/websocket` toolchain using the [Hertz HTTP Adaptor](../../basic-feature/http-adaptor/).
2013
>
2114
> See the migration guide below.
2215
16+
## Overview
17+
18+
WebSocket is a type of full-duplex communication that can be performed on a single TCP connection and is located at the application layer of the OSI model.
19+
WebSocket makes data exchange between client and server easier, allowing the server to actively push data to the client.
20+
In the WebSocket API, the browser and the server only need to complete a handshake that a persistent connection can be created between the two, and two-way data transmission can be performed.
21+
22+
Historically, Hertz provided WebSocket support via [`hertz-contrib/websocket`](https://github.com/hertz-contrib/websocket), which adapts
23+
[`gorilla/websocket`](https://github.com/gorilla/websocket) using HTTP connection hijacking.
24+
This approach is now deprecated in favor of using the official Gorilla WebSocket implementation directly via the Hertz HTTP Adaptor.
25+
2326
## Migration Guide
2427

2528
1. Remove deprecated dependencies
@@ -31,7 +34,7 @@ github.com/hertz-contrib/websocket
3134
2. Use hertz adaptor
3235

3336
```go
34-
// Before (using hertz-contrib/websocket)
37+
// Before (deprecated: hertz-contrib/websocket)
3538
import "github.com/hertz-contrib/websocket"
3639
var upgrader = websocket.HertzUpgrader{}
3740
func echo(_ context.Context, c *app.RequestContext) {
@@ -40,9 +43,12 @@ func echo(_ context.Context, c *app.RequestContext) {
4043
})
4144
}
4245
h.GET("/echo", echo)
46+
```
4347

44-
// After (using hertz adaptor)
48+
```go
49+
// After (recommended: gorilla/websocket + adaptor)
4550
import "github.com/gorilla/websocket"
51+
import "github.com/cloudwego/hertz/pkg/common/adaptor"
4652
var upgrader = websocket.Upgrader{}
4753
func echo(w http.ResponseWriter, r *http.Request) {
4854
c, err := upgrader.Upgrade(w, r, nil)
@@ -114,13 +120,15 @@ func main() {
114120
var homeTemplate = ""
115121
```
116122

117-
## Install (Deprecated)
123+
## Legacy Documentation (Deprecated)
124+
125+
### Install
118126

119127
```shell
120128
go get github.com/hertz-contrib/websocket
121129
```
122130

123-
## Example
131+
### Example
124132

125133
```go
126134
package main
@@ -189,7 +197,7 @@ go run server.go
189197

190198
In the example code above, the server includes a simple web client. To use this client, open [http://127.0.0.1:8080]() in your browser and follow the instructions on the page.
191199

192-
## Config
200+
### Config
193201

194202
The following is the optional configuration parameters for using Hertz WebSocket.
195203

@@ -205,7 +213,7 @@ This section is organized around the `websocket.HertzUpgrader` structure.
205213
| `CheckOrigin` | Used to set a check function for Origin header for the request. If the Origin header of the request is acceptable, CheckOrigin returns true. |
206214
| `EnableCompression` | Used to set whether the server should attempt to negotiate compression for each message (RFC 7692). Setting this value to true does not guarantee that compression will be supported. |
207215

208-
### WriteBufferPool
216+
#### WriteBufferPool
209217

210218
If this value is not set, an additional write buffer is initialized and allocated to the connection for the current lifetime. The buffer pool is most useful when the application has a moderate amount of writes on a large number of connections.
211219

@@ -246,13 +254,13 @@ var upgrader = websocket.HertzUpgrader{
246254
}
247255
```
248256

249-
### Subprotocols
257+
#### Subprotocols
250258

251259
WebSocket simply defines a mechanism for exchanging arbitrary messages. What those messages mean, what kind of messages the client can expect at any given point in time, or what kind of messages they are allowed to send, depends entirely on the implementing application.
252260

253261
So you need an agreement between the server and the client about these things. The subprotocol parameters simply allow the client and server to formally exchange this information. You can make up any name for any protocol you want. The server can simply check that the client has followed that protocol during the handshake.
254262

255-
### Error
263+
#### Error
256264

257265
If Error is nil, then use the API provided by Hertz to generate the HTTP error response.
258266

@@ -273,7 +281,7 @@ var upgrader = websocket.HertzUpgrader{
273281
}
274282
```
275283

276-
### CheckOrigin
284+
#### CheckOrigin
277285

278286
CheckOrigin returns true if the request Origin header is acceptable. If CheckOrigin is nil, then a safe default is used: return false if the Origin request header is present and the origin host is not equal to request Host header.
279287

@@ -302,13 +310,13 @@ func fastHTTPCheckSameOrigin(c *app.RequestContext) bool {
302310
}
303311
```
304312

305-
### EnableCompression
313+
#### EnableCompression
306314

307315
The server accepts one or more extension fields that are included in the `Sec-WebSocket-Extensions` header field extensions requested by the client. When EnableCompression is true, the server matches the extensions it currently supports with its extensions, and supports compression if the match is successful.
308316

309317
Currently only the "no context takeover" mode is supported, as described in the `HertzUpgrader.Upgrade`.
310318

311-
### SetReadTimeout/SetWriteTimeout
319+
#### SetReadTimeout/SetWriteTimeout
312320

313321
When using websockets for reading and writing, the read timeout or write timeout can be set similarly as follows.
314322

@@ -330,7 +338,7 @@ func echo(_ context.Context, c *app.RequestContext) {
330338
}
331339
```
332340

333-
## NoHijackConnPool
341+
### NoHijackConnPool
334342

335343
> The hijack conn used for Hertz connection hijacking is pooled and therefore does not support asynchronous operations when the hijacked connection is used in a websocket.
336344

0 commit comments

Comments
 (0)