How to make my web server written in Golang to support HTTP / 2 Server Push?

My web server is Golang encoded and supports HTTPS. I want to use the HTTP / 2 Server Push features on a web server. The following link explains how to convert an HTTP server to HTTP / 2 support: - https://www.ianlewis.org/en/http2-and-go
However, it is not clear how to implement Push Push notifications in the Golang.
- How to add Server Push functionality?
- How to manage or manage documents and files that need to be clicked?

+4
source share
2 answers

Go 1.7 HTTP/2 . push- ​​ 1.8 (. , - ).

Go 1.8 http.Pusher, net/http default ResponseWriter. Push-Push Push ErrNotSupported, (HTTP/1) ( ).

:

package main                                                                              

import (
    "io"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/pushed", func(w http.ResponseWriter, r *http.Request) {
        io.WriteString(w, "hello server push")
    })

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        if pusher, ok := w.(http.Pusher); ok {
            if err := pusher.Push("/pushed", nil); err != nil {
                log.Println("push failed")
            }
        }

        io.WriteString(w, "hello world")
    })

    http.ListenAndServeTLS(":443", "server.crt", "server.key", nil)
}

push- Go 1.7 , golang.org/x/net/http2 .

+5

, Go 1.8 ( http.Pusher, Push).

: HTTP2 .

, NGINX, . , Link URL-, .

// In the case of HTTP1.1 we make use of the `Link` header
// to indicate that the client (in our case, NGINX) should
// retrieve a certain URL.
//
// See more at https://www.w3.org/TR/preload/#server-push-http-2.
func handleIndex(w http.ResponseWriter, r *http.Request) {
  var err error

  if *http2 {
    pusher, ok := w.(http.Pusher)
    if ok {
      must(pusher.Push("/image.svg", nil))
    }
  } else {
    // This ends up taking the effect of a server push
    // when interacting directly with NGINX.
    w.Header().Add("Link", 
      "</image.svg>; rel=preload; as=image")
  }

  w.Header().Add("Content-Type", "text/html")
  _, err = w.Write(assets.Index)
  must(err)
}

ps.: https://ops.tips/blog/nginx-http2-server-push/, .

0

Source: https://habr.com/ru/post/1665903/


All Articles