|
|
@@ -54,6 +54,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi
|
|
|
- [Build a single binary with templates](#build-a-single-binary-with-templates)
|
|
|
- [Bind form-data request with custom struct](#bind-form-data-request-with-custom-struct)
|
|
|
- [Try to bind body into different structs](#try-to-bind-body-into-different-structs)
|
|
|
+ - [http2 server push](#http2-server-push)
|
|
|
- [Testing](#testing)
|
|
|
- [Users](#users--)
|
|
|
|
|
|
@@ -1656,6 +1657,55 @@ enough to call binding at once.
|
|
|
can be called by `c.ShouldBind()` multiple times without any damage to
|
|
|
performance (See [#1341](https://github.com/gin-gonic/gin/pull/1341)).
|
|
|
|
|
|
+### http2 server push
|
|
|
+
|
|
|
+http.Pusher is supported only **go1.8+**. See the [golang blog](https://blog.golang.org/h2push) for detail information.
|
|
|
+
|
|
|
+[embedmd]:# (examples/http-pusher/main.go go)
|
|
|
+```go
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "html/template"
|
|
|
+ "log"
|
|
|
+
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+)
|
|
|
+
|
|
|
+var html = template.Must(template.New("https").Parse(`
|
|
|
+<html>
|
|
|
+<head>
|
|
|
+ <title>Https Test</title>
|
|
|
+ <script src="/assets/app.js"></script>
|
|
|
+</head>
|
|
|
+<body>
|
|
|
+ <h1 style="color:red;">Welcome, Ginner!</h1>
|
|
|
+</body>
|
|
|
+</html>
|
|
|
+`))
|
|
|
+
|
|
|
+func main() {
|
|
|
+ r := gin.Default()
|
|
|
+ r.Static("/assets", "./assets")
|
|
|
+ r.SetHTMLTemplate(html)
|
|
|
+
|
|
|
+ r.GET("/", func(c *gin.Context) {
|
|
|
+ if pusher := c.Writer.Pusher(); pusher != nil {
|
|
|
+ // use pusher.Push() to do server push
|
|
|
+ if err := pusher.Push("/assets/app.js", nil); err != nil {
|
|
|
+ log.Printf("Failed to push: %v", err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ c.HTML(200, "https", gin.H{
|
|
|
+ "status": "success",
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ // Listen and Server in https://127.0.0.1:8080
|
|
|
+ r.RunTLS(":8080", "./testdata/server.pem", "./testdata/server.key")
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
## Testing
|
|
|
|
|
|
The `net/http/httptest` package is preferable way for HTTP testing.
|