跳转到内容

HTTP/2 服务器

HTTP/2 通过请求复用、header 压缩和 server push 改善延迟。Go 的 HTTP 服务器会在 TLS 上自动协商 HTTP/2,因此使用 Echo 提供 HTTP/2 只需用证书启动服务器。

运行以下命令生成 cert.pemkey.pem

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

2. 创建回显请求信息的处理函数

Section titled “2. 创建回显请求信息的处理函数”
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))
})

使用生成的证书和 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)
}

或者使用带自定义 tls.Config 的自定义 http.Server

s := http.Server{
Addr: ":8443",
Handler: e, // set Echo as handler
TLSConfig: &tls.Config{
//Certificates: nil, // <-- s.ListenAndServeTLS will populate this field
},
//ReadTimeout: 30 * time.Second, // use custom timeouts
}
if err := s.ListenAndServeTLS("cert.pem", "key.pem"); err != http.ErrServerClosed {
log.Fatal(err)
}

启动服务器并访问 https://localhost:1323/request。你应该会看到类似下面的输出:

Terminal window
Protocol: HTTP/2.0
Host: localhost:1323
Remote Address: [::1]:60288
Method: GET
Path: /
package main
import (
"context"
"fmt"
"net/http"
"github.com/labstack/echo/v5"
)
func main() {
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))
})
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)
}
}