Auto TLS

Automatic TLS certificates from Let’s Encrypt recipe for Echo

This recipe demonstrates how to obtain TLS certificates for a domain automatically from
Let’s Encrypt. Echo#StartAutoTLS accepts an address which should listen on port 443.

Browse to https://<DOMAIN>. If everything goes fine, you should see a welcome
message with TLS enabled on the website.

Server

server.go

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/labstack/echo"
  5. "github.com/labstack/echo/middleware"
  6. "golang.org/x/crypto/acme/autocert"
  7. )
  8. func main() {
  9. e := echo.New()
  10. // e.AutoTLSManager.HostPolicy = autocert.HostWhitelist("<DOMAIN>")
  11. // Cache certificates
  12. e.AutoTLSManager.Cache = autocert.DirCache("/var/www/.cache")
  13. e.Use(middleware.Recover())
  14. e.Use(middleware.Logger())
  15. e.GET("/", func(c echo.Context) error {
  16. return c.HTML(http.StatusOK, `
  17. <h1>Welcome to Echo!</h1>
  18. <h3>TLS certificates automatically installed from Let's Encrypt :)</h3>
  19. `)
  20. })
  21. e.Logger.Fatal(e.StartAutoTLS(":443"))
  22. }

Source Code

Maintainers