update database schema to avoid bool confusion
Some checks are pending
CI / Lint (push) Waiting to run
CI / Test (push) Waiting to run
CI / End-to-End (push) Waiting to run
CI / Publish Docker (push) Blocked by required conditions
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-09-27 11:07:51 +10:00
parent b371e28469
commit c691763430
8 changed files with 214 additions and 72 deletions

View File

@@ -3,6 +3,7 @@ package server
import (
"context"
"crypto/tls"
"fmt"
"log/slog"
"net/http"
"os"
@@ -47,16 +48,21 @@ func New(logger *slog.Logger, cron gocron.Scheduler, cancel context.CancelFunc,
TLSConfig: tlsConfig,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}
for _, opt := range opts {
opt(&Server{srv: srv})
}
return &Server{
// Set the initial server values
server := &Server{
srv: srv,
logger: logger,
cron: cron,
cancel: cancel,
}
// Apply any options
for _, opt := range opts {
opt(server)
}
return server
}
// Option represents a server option.
@@ -83,19 +89,26 @@ func WithRouter(handler http.Handler) Option {
}
}
// DisableTls sets the disable tls value
func (s *Server) DisableTls(disableTls bool) {
s.disableTls = disableTls
// SetTls sets the disable tls value
func SetTls(disableTls bool) Option {
return func(s *Server) {
s.disableTls = disableTls
}
}
// SetCertificate sets the path to the certificate used for TLS, in PEM format
func (s *Server) SetCertificate(tlsCertFilename string) {
s.tlsCertFilename = tlsCertFilename
func SetCertificate(tlsCertFilename string) Option {
return func(s *Server) {
fmt.Printf("Setting tlsCertFilename to '%s'\n", tlsCertFilename)
s.tlsCertFilename = tlsCertFilename
}
}
// SetPrivateKey sets the path to the private key used for TLS, in PEM format
func (s *Server) SetPrivateKey(tlsKeyFilename string) {
s.tlsKeyFilename = tlsKeyFilename
func SetPrivateKey(tlsKeyFilename string) Option {
return func(s *Server) {
s.tlsKeyFilename = tlsKeyFilename
}
}
// StartAndWait starts the server and waits for a signal to shut down.
@@ -111,12 +124,14 @@ func (s *Server) Start() {
if s.disableTls {
s.logger.Info("starting server", "port", s.srv.Addr)
if err := s.srv.ListenAndServe(); err != nil {
s.logger.Warn("failed to start server", "error", err)
s.logger.Error("failed to start server", "error", err)
os.Exit(1)
}
} else {
s.logger.Info("starting TLS server", "port", s.srv.Addr, "cert", s.tlsCertFilename, "key", s.tlsKeyFilename)
if err := s.srv.ListenAndServeTLS(s.tlsCertFilename, s.tlsKeyFilename); err != nil && err != http.ErrServerClosed {
s.logger.Warn("failed to start server", "error", err)
s.logger.Error("failed to start server", "error", err)
os.Exit(1)
}
}
}()