Skip to content
Rafał Lorenz edited this page Jun 1, 2017 · 3 revisions
  1. Pusher
  2. The Go Blog - HTTP/2 Server Push

Pusher

package main

import (
    "log"
    "net/http"
	
    "golang.org/x/net/http2"
    "github.com/vardius/router"
)

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"))
}
You can’t perform that action at this time.