HTTP2
The Go Blog - HTTP/2 Server Push
Pusher
package main
import (
"log"
"net/http"
"golang.org/x/net/http2"
"github.com/vardius/gorouter/v4"
)
func Pusher(w http.ResponseWriter, r *http.Request) {
if pusher, ok := w.(http.Pusher); ok {
// Push is supported.
options := &http.PushOptions{
Header: http.Header{
"Accept-Encoding": r.Header["Accept-Encoding"],
},
}
if err := pusher.Push("/script.js", options); err != nil {
log.Printf("Failed to push: %v", err)
}
}
// ...
}
func main() {
router := gorouter.New()
router.GET("/", http.HandlerFunc(Pusher))
http2.ConfigureServer(router, &http2.Server{})
log.Fatal(router.ListenAndServeTLS("router.crt", "router.key"))
}
HTTP/2 implementation for fasthttp is under construction...