HTTP/2 Recipe

How to run an HTTP/2 server?

Step 1: Generate a self-signed X.509 TLS certificate

Run the following command to generate cert.pem and key.pem files:

go run $GOROOT/src/crypto/tls/generate_cert.go --host localhost

For demo purpose, we are using a self-signed certificate. Ideally, you should obtain a certificate from CA.

Step 2: Create a handler which simply outputs the request information to the client

e.GET("/request", func(c echo.Context) error {
  req := c.Request()
  format := `
    <code>
      Protocol: %s<br>
      Host: %s<br>
      Remote Address: %s<br>
      Method: %s<br>
      Path: %s<br>
    </code>
  `
  return c.HTML(http.StatusOK, fmt.Sprintf(format, req.Proto, req.Host, req.RemoteAddr, req.Method, req.URL.Path))
})

Step 3: Configure TLS server using cert.pem and key.pem

e.StartTLS(":1323", "cert.pem", "key.pem")

Step 4: Start the server and browse to https://localhost:1323/request to see the following output

Protocol: HTTP/2.0
Host: localhost:1323
Remote Address: [::1]:60288
Method: GET
Path: /

Source Code

server.go




Maintainers