h2demo.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2014 The Go Authors.
  2. // See https://code.google.com/p/go/source/browse/CONTRIBUTORS
  3. // Licensed under the same terms as Go itself:
  4. // https://code.google.com/p/go/source/browse/LICENSE
  5. package main
  6. import (
  7. "flag"
  8. "io"
  9. "log"
  10. "net/http"
  11. "os/exec"
  12. "runtime"
  13. "time"
  14. "github.com/bradfitz/http2"
  15. )
  16. var openFirefox = flag.Bool("openff", false, "Open Firefox")
  17. func main() {
  18. var srv http.Server
  19. flag.BoolVar(&http2.VerboseLogs, "verbose", false, "Verbose HTTP/2 debugging.")
  20. flag.StringVar(&srv.Addr, "addr", "localhost:4430", "host:port to listen on ")
  21. flag.Parse()
  22. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  23. io.WriteString(w, "<h1>Hello, world!</h1>Greetings from Go's HTTP/2 server.")
  24. })
  25. url := "https://" + srv.Addr + "/"
  26. log.Printf("Listening on " + url)
  27. http2.ConfigureServer(&srv, &http2.Server{})
  28. go func() {
  29. log.Fatal(srv.ListenAndServeTLS("server.crt", "server.key"))
  30. }()
  31. if *openFirefox && runtime.GOOS == "darwin" {
  32. time.Sleep(250 * time.Millisecond)
  33. exec.Command("open", "-b", "org.mozilla.nightly", "https://localhost:4430/").Run()
  34. }
  35. select {}
  36. }