From 173c8363228e7aa7fe01a3a060e4118b8912c432 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Mon, 22 May 2023 18:56:34 -0600 Subject: [PATCH] Adding support for auto TLS --- server/server.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/server/server.go b/server/server.go index a100cd2..39a8cae 100644 --- a/server/server.go +++ b/server/server.go @@ -30,6 +30,7 @@ import ( "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 @@ type Server struct { e *echo.Echo ai *appInfo + certman autocert.Manager + autocert bool csrfSkip []string queues []*work.Queue } @@ -564,6 +567,13 @@ func (s *Server) DefaultMiddlewareWithConfig(conf *MiddlewareConfig) *Server { 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 @@ func (s *Server) Echo() *echo.Echo { // 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) -- 2.45.2