# HG changeset patch # User Peter Sanchez # Date 1684803394 21600 # Mon May 22 18:56:34 2023 -0600 # Node ID fc53d174a4eb413ce97cb4725f54d63974115f43 # Parent 0dc479f6ca0c2ca64ac75a0c767cfa1f5824d735 Adding support for auto TLS diff --git a/server/server.go b/server/server.go --- a/server/server.go +++ b/server/server.go @@ -30,6 +30,7 @@ "github.com/alexedwards/scs/v2" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" + "golang.org/x/crypto/acme/autocert" "hg.code.netlandish.com/~netlandish/gobwebs" "hg.code.netlandish.com/~netlandish/gobwebs/config" "hg.code.netlandish.com/~netlandish/gobwebs/email" @@ -85,6 +86,8 @@ e *echo.Echo ai *appInfo + certman autocert.Manager + autocert bool csrfSkip []string queues []*work.Queue } @@ -564,6 +567,13 @@ return s } +// WithCertManager adds an autocert.Manager to be used for auto TLS +func (s *Server) WithCertManager(cm autocert.Manager) *Server { + s.certman = cm + s.autocert = true + return s +} + // WithMiddleware add user-defined middleware to the server func (s *Server) WithMiddleware(middlewares ...echo.MiddlewareFunc) *Server { s.e.Use(middlewares...) @@ -646,7 +656,12 @@ // Run will run the server func (s *Server) Run() { - go s.e.Start(fmt.Sprintf("%s:%d", s.Config.ListenAddr, s.Config.ListenPort)) + laddrp := fmt.Sprintf("%s:%d", s.Config.ListenAddr, s.Config.ListenPort) + if s.autocert { + go s.e.StartAutoTLS(laddrp) + } else { + go s.e.Start(laddrp) + } sigs := make(chan os.Signal, 1) signal.Notify(sigs, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGUSR1, syscall.SIGINT)