Skip to content

HTTP/2 Server

HTTP/2 improves latency through request multiplexing and header compression. Go’s HTTP server negotiates HTTP/2 automatically over TLS, so serving HTTP/2 with Echo is a matter of starting the server with a certificate.

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

Section titled “1. Generate a self-signed X.509 TLS certificate”

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

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

2. Create a handler that echoes request information

Section titled “2. Create a handler that echoes request information”
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))
})

Start the server with the generated certificate and key:

sc := echo.StartConfig{Address: ":1323"}
if err := sc.StartTLS(context.Background(), e, "cert.pem", "key.pem"); err != nil {
e.Logger.Error("failed to start server", "error", err)
}

Alternatively, use a custom http.Server with your own tls.Config:

s := http.Server{
Addr: ":1323",
Handler: e,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
// ReadTimeout: 30 * time.Second, // use custom timeouts
}
if err := s.ListenAndServeTLS("cert.pem", "key.pem"); err != nil && !errors.Is(err, http.ErrServerClosed) {
e.Logger.Error("failed to start server", "error", err)
}
Terminal window
go run . -custom-server

Start the server and browse to https://localhost:1323/request. You should see output similar to:

Terminal window
Protocol: HTTP/2.0
Host: localhost:1323
Remote Address: [::1]:60288
Method: GET
Path: /
package main
import (
"context"
"crypto/tls"
"errors"
"flag"
"fmt"
"net/http"
"github.com/labstack/echo/v5"
)
func main() {
customServer := flag.Bool("custom-server", false, "use a standard http.Server")
flag.Parse()
e := echo.New()
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))
})
if *customServer {
customHTTPServer(e)
return
}
sc := echo.StartConfig{Address: ":1323"}
if err := sc.StartTLS(context.Background(), e, "cert.pem", "key.pem"); err != nil {
e.Logger.Error("failed to start server", "error", err)
}
}
// customHTTPServer demonstrates configuring TLS with a standard HTTP server.
func customHTTPServer(e *echo.Echo) {
s := http.Server{
Addr: ":1323",
Handler: e,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
// ReadTimeout: 30 * time.Second, // use custom timeouts
}
if err := s.ListenAndServeTLS("cert.pem", "key.pem"); err != nil && !errors.Is(err, http.ErrServerClosed) {
e.Logger.Error("failed to start server", "error", err)
}
}