~netlandish/gobwebs

173c8363228e7aa7fe01a3a060e4118b8912c432 — Peter Sanchez 1 year, 6 months ago f7098bd
Adding support for auto TLS
1 files changed, 16 insertions(+), 1 deletions(-)

M server/server.go
M server/server.go => server/server.go +16 -1
@@ 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)