Skip to content

Commit fa26419

Browse files
committed
docs: improve documentation and examples for server push with Gin framework
- Change section headers to proper markdown format - Add a new section for "Server Push" with example Go code using the Gin framework and gzip middleware Signed-off-by: appleboy <[email protected]>
1 parent 47135c9 commit fa26419

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func main() {
4949
}
5050
```
5151

52-
Customized Excluded Extensions
52+
### Customized Excluded Extensions
5353

5454
```go
5555
package main
@@ -77,7 +77,7 @@ func main() {
7777
}
7878
```
7979

80-
Customized Excluded Paths
80+
### Customized Excluded Paths
8181

8282
```go
8383
package main
@@ -105,7 +105,7 @@ func main() {
105105
}
106106
```
107107

108-
Customized Excluded Paths
108+
### Customized Excluded Paths with Regex
109109

110110
```go
111111
package main
@@ -132,3 +132,38 @@ func main() {
132132
}
133133
}
134134
```
135+
136+
### Server Push
137+
138+
```go
139+
package main
140+
141+
import (
142+
"fmt"
143+
"log"
144+
"net/http"
145+
"time"
146+
147+
"github.com/gin-contrib/gzip"
148+
"github.com/gin-gonic/gin"
149+
)
150+
151+
func main() {
152+
r := gin.Default()
153+
r.Use(gzip.Gzip(gzip.DefaultCompression))
154+
r.GET("/stream", func(c *gin.Context) {
155+
c.Header("Content-Type", "text/event-stream")
156+
c.Header("Connection", "keep-alive")
157+
for i := 0; i < 10; i++ {
158+
fmt.Fprintf(c.Writer, "id: %d\ndata: tick %d\n\n", i, time.Now().Unix())
159+
c.Writer.Flush()
160+
time.Sleep(1 * time.Second)
161+
}
162+
})
163+
164+
// Listen and Server in 0.0.0.0:8080
165+
if err := r.Run(":8080"); err != nil {
166+
log.Fatal(err)
167+
}
168+
}
169+
```

0 commit comments

Comments
 (0)