github.com/glycerine/zebrapack@v4.1.1-0.20181107023619-e955d028f9bf+incompatible/slides/state-of-go/stdlib/http2/http2.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "go/build" 6 "log" 7 "net/http" 8 "path/filepath" 9 ) 10 11 var cert, key string 12 13 func init() { 14 pkg, err := build.Import("golang.org/x/talks/2017/state-of-go/stdlib/http2", ".", build.FindOnly) 15 if err != nil { 16 log.Fatal(err) 17 } 18 cert = filepath.Join(pkg.Dir, "cert.pem") 19 key = filepath.Join(pkg.Dir, "key.pem") 20 } 21 22 func main() { 23 http.HandleFunc("/", rootHandler) 24 http.HandleFunc("/style.css", cssHandler) 25 26 go func() { 27 log.Fatal(http.ListenAndServeTLS("127.0.0.1:8081", cert, key, nil)) 28 }() 29 log.Fatal(http.ListenAndServe("127.0.0.1:8080", nil)) 30 } 31 32 func rootHandler(w http.ResponseWriter, r *http.Request) { 33 if p, ok := w.(http.Pusher); ok { // HL 34 err := p.Push("/style.css", nil) // HL 35 if err != nil { 36 log.Printf("could not push: %v", err) 37 } 38 } 39 40 fmt.Fprintln(w, html) 41 } 42 43 func cssHandler(w http.ResponseWriter, r *http.Request) { 44 fmt.Fprintln(w, css) 45 } 46 47 const ( 48 html = ` 49 <html> 50 <head> 51 <link rel="stylesheet" href="/style.css"> 52 <title>HTTP2 push test</title> 53 </head> 54 <body> 55 <h1>Hello</h1> 56 </body> 57 </html> 58 ` 59 css = ` 60 h1 { 61 color: red; 62 text-align: center; 63 text-shadow: green 0 0 40px; 64 font-size: 10em; 65 } 66 ` 67 )