HTTP2 server push

http.Pusher sólo es compatible con versiones go1.8+ en adelante. Véase el blog de golang para información detallada.

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() {
	router := gin.Default()
	router.Static("/assets", "./assets")
	router.SetHTMLTemplate(html)

	router.GET("/", func(c *gin.Context) {
		if pusher := c.Writer.Pusher(); pusher != nil {
			// Utilice pusher.Push() para hacer 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",
		})
	})

	// Escucha y sirve peticiones en https://127.0.0.1:8080
	router.RunTLS(":8080", "./testdata/server.pem", "./testdata/server.key")
}
Última modificación 16.03.2025: docs: add Don't trust all proxies section (70aad7e)