about summary refs log tree commit diff stats
path: root/internal/server
diff options
context:
space:
mode:
authorAlan Pearce2024-06-29 21:27:35 +0200
committerAlan Pearce2024-06-29 21:27:35 +0200
commit973fa31b6743a3ea84c86ab7671349cc70a69142 (patch)
tree4c583c8ef5e7a9117fe997a4f861ae7466fa0864 /internal/server
parent7b82d3ef3e93c0c8efe489d6b11b30f092667bf2 (diff)
downloadwebsite-973fa31b6743a3ea84c86ab7671349cc70a69142.tar.lz
website-973fa31b6743a3ea84c86ab7671349cc70a69142.tar.zst
website-973fa31b6743a3ea84c86ab7671349cc70a69142.zip
enable filtered debug logging via DEBUG="component.*"
Diffstat (limited to 'internal/server')
-rw-r--r--internal/server/dev.go19
-rw-r--r--internal/server/logging.go2
-rw-r--r--internal/server/mime.go6
-rw-r--r--internal/server/server.go32
-rw-r--r--internal/server/tcp.go2
-rw-r--r--internal/server/tls.go7
6 files changed, 35 insertions, 33 deletions
diff --git a/internal/server/dev.go b/internal/server/dev.go
index e83201a..4db9465 100644
--- a/internal/server/dev.go
+++ b/internal/server/dev.go
@@ -3,7 +3,6 @@ package server
 import (
 	"fmt"
 	"io/fs"
-	"log/slog"
 	"os"
 	"path"
 	"path/filepath"
@@ -21,6 +20,7 @@ type FileWatcher struct {
 }
 
 var (
+	l       *log.Logger
 	ignores = []string{
 		"*.templ",
 		"*.go",
@@ -32,7 +32,7 @@ func matches(name string) func(string) bool {
 	return func(pattern string) bool {
 		matched, err := path.Match(pattern, name)
 		if err != nil {
-			log.Warn("error checking watcher ignores", "error", err)
+			l.Warn("error checking watcher ignores", "error", err)
 		}
 
 		return matched
@@ -43,23 +43,24 @@ func ignored(pathname string) bool {
 	return slices.ContainsFunc(ignores, matches(path.Base(pathname)))
 }
 
-func NewFileWatcher() (*FileWatcher, error) {
+func NewFileWatcher(log *log.Logger) (*FileWatcher, error) {
 	watcher, err := fsnotify.NewWatcher()
 	if err != nil {
 		return nil, errors.WithMessage(err, "could not create watcher")
 	}
+	l = log
 
 	return &FileWatcher{watcher}, nil
 }
 
 func (watcher FileWatcher) AddRecursive(from string) error {
-	log.Debug("walking directory tree", "root", from)
+	l.Debug("walking directory tree", "root", from)
 	err := filepath.WalkDir(from, func(path string, entry fs.DirEntry, err error) error {
 		if err != nil {
 			return errors.WithMessagef(err, "could not walk directory %s", path)
 		}
 		if entry.IsDir() {
-			log.Debug("adding directory to watcher", "path", path)
+			l.Debug("adding directory to watcher", "path", path)
 			if err = watcher.Add(path); err != nil {
 				return errors.WithMessagef(err, "could not add directory %s to watcher", path)
 			}
@@ -77,17 +78,17 @@ func (watcher FileWatcher) Start(callback func(string)) {
 		select {
 		case event := <-watcher.Events:
 			if !ignored(event.Name) {
-				log.Debug("watcher event", "name", event.Name, "op", event.Op.String())
+				l.Debug("watcher event", "name", event.Name, "op", event.Op.String())
 				if event.Has(fsnotify.Create) || event.Has(fsnotify.Rename) {
 					f, err := os.Stat(event.Name)
 					if err != nil {
-						slog.Error(
+						l.Error(
 							fmt.Sprintf("error handling %s event: %v", event.Op.String(), err),
 						)
 					} else if f.IsDir() {
 						err = watcher.Add(event.Name)
 						if err != nil {
-							slog.Error(fmt.Sprintf("error adding new folder to watcher: %v", err))
+							l.Error(fmt.Sprintf("error adding new folder to watcher: %v", err))
 						}
 					}
 				}
@@ -102,7 +103,7 @@ func (watcher FileWatcher) Start(callback func(string)) {
 				}
 			}
 		case err := <-watcher.Errors:
-			slog.Error(fmt.Sprintf("error in watcher: %v", err))
+			l.Error("error in watcher", "error", err)
 		}
 	}
 }
diff --git a/internal/server/logging.go b/internal/server/logging.go
index 20f0a92..6393099 100644
--- a/internal/server/logging.go
+++ b/internal/server/logging.go
@@ -23,7 +23,7 @@ func NewLoggingResponseWriter(w http.ResponseWriter) *LoggingResponseWriter {
 	return &LoggingResponseWriter{w, http.StatusOK}
 }
 
-func wrapHandlerWithLogging(wrappedHandler http.Handler) http.Handler {
+func wrapHandlerWithLogging(wrappedHandler http.Handler, log *log.Logger) http.Handler {
 	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 		lw := NewLoggingResponseWriter(w)
 		wrappedHandler.ServeHTTP(lw, r)
diff --git a/internal/server/mime.go b/internal/server/mime.go
index 3a1ec8a..340e4c5 100644
--- a/internal/server/mime.go
+++ b/internal/server/mime.go
@@ -10,14 +10,10 @@ var newMIMEs = map[string]string{
 	".xsl": "text/xsl",
 }
 
-func fixupMIMETypes() {
+func fixupMIMETypes(log *log.Logger) {
 	for ext, newType := range newMIMEs {
 		if err := mime.AddExtensionType(ext, newType); err != nil {
 			log.Error("could not update mime type", "ext", ext, "mime", newType)
 		}
 	}
 }
-
-func init() {
-	fixupMIMETypes()
-}
diff --git a/internal/server/server.go b/internal/server/server.go
index dfb0f8c..f01f939 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -49,6 +49,7 @@ type Server struct {
 	redirectServer *http.Server
 	runtimeConfig  *Config
 	config         *cfg.Config
+	log            *log.Logger
 }
 
 func applyDevModeOverrides(config *cfg.Config, runtimeConfig *Config) {
@@ -87,8 +88,8 @@ func serverHeaderHandler(wrappedHandler http.Handler) http.Handler {
 	})
 }
 
-func rebuild(builderConfig *builder.IOConfig, config *cfg.Config) error {
-	r, err := builder.BuildSite(builderConfig, config)
+func rebuild(builderConfig *builder.IOConfig, config *cfg.Config, log *log.Logger) error {
+	r, err := builder.BuildSite(builderConfig, config, log.Named("builder"))
 	if err != nil {
 		return errors.WithMessage(err, "could not build site")
 	}
@@ -97,7 +98,7 @@ func rebuild(builderConfig *builder.IOConfig, config *cfg.Config) error {
 	return nil
 }
 
-func New(runtimeConfig *Config) (*Server, error) {
+func New(runtimeConfig *Config, log *log.Logger) (*Server, error) {
 	builderConfig := &builder.IOConfig{
 		Destination: runtimeConfig.Root,
 		Development: runtimeConfig.Development,
@@ -109,7 +110,7 @@ func New(runtimeConfig *Config) (*Server, error) {
 		if err != nil {
 			return nil, err
 		}
-		_, err = vcs.CloneOrUpdate(vcsConfig)
+		_, err = vcs.CloneOrUpdate(vcsConfig, log.Named("vcs"))
 		if err != nil {
 			return nil, err
 		}
@@ -125,7 +126,7 @@ func New(runtimeConfig *Config) (*Server, error) {
 		runtimeConfig.Root = publicDir
 	}
 
-	config, err := cfg.GetConfig(builderConfig.Source)
+	config, err := cfg.GetConfig(builderConfig.Source, log.Named("config"))
 	if err != nil {
 		return nil, errors.WithMessage(err, "error parsing configuration file")
 	}
@@ -136,16 +137,18 @@ func New(runtimeConfig *Config) (*Server, error) {
 	listenAddress := net.JoinHostPort(runtimeConfig.ListenAddress, strconv.Itoa(runtimeConfig.Port))
 	top := http.NewServeMux()
 
-	err = rebuild(builderConfig, config)
+	err = rebuild(builderConfig, config, log)
 	if err != nil {
 		return nil, err
 	}
 
+	fixupMIMETypes(log)
+
 	if runtimeConfig.Development {
 		liveReload := livereload.New()
 		top.Handle("/_/reload", liveReload)
 		liveReload.Start()
-		fw, err := NewFileWatcher()
+		fw, err := NewFileWatcher(log.Named("watcher"))
 		if err != nil {
 			return nil, errors.WithMessage(err, "could not create file watcher")
 		}
@@ -165,7 +168,7 @@ func New(runtimeConfig *Config) (*Server, error) {
 		}
 		go fw.Start(func(filename string) {
 			log.Info("rebuilding site", "changed_file", filename)
-			err := rebuild(builderConfig, config)
+			err := rebuild(builderConfig, config, log)
 			if err != nil {
 				log.Error("error rebuilding site", "error", err)
 			}
@@ -173,7 +176,7 @@ func New(runtimeConfig *Config) (*Server, error) {
 	}
 
 	loggingMux := http.NewServeMux()
-	mux, err := website.NewMux(config, runtimeConfig.Root)
+	mux, err := website.NewMux(config, runtimeConfig.Root, log.Named("website"))
 	if err != nil {
 		return nil, errors.Wrap(err, "could not create website mux")
 	}
@@ -193,7 +196,7 @@ func New(runtimeConfig *Config) (*Server, error) {
 
 	top.Handle("/",
 		serverHeaderHandler(
-			wrapHandlerWithLogging(loggingMux),
+			wrapHandlerWithLogging(loggingMux, log),
 		),
 	)
 
@@ -218,6 +221,7 @@ func New(runtimeConfig *Config) (*Server, error) {
 			Addr:              listenAddress,
 			Handler:           rMux,
 		},
+		log:           log,
 		config:        config,
 		runtimeConfig: runtimeConfig,
 	}, nil
@@ -240,19 +244,19 @@ func (s *Server) Start() error {
 }
 
 func (s *Server) Stop() chan struct{} {
-	log.Debug("stop called")
+	s.log.Debug("stop called")
 
 	idleConnsClosed := make(chan struct{})
 
 	go func() {
-		log.Debug("shutting down server")
+		s.log.Debug("shutting down server")
 		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
 		defer cancel()
 		err := s.Server.Shutdown(ctx)
-		log.Debug("server shut down")
+		s.log.Debug("server shut down")
 		if err != nil {
 			// Error from closing listeners, or context timeout:
-			log.Warn("HTTP server Shutdown", "error", err)
+			s.log.Warn("HTTP server Shutdown", "error", err)
 		}
 		close(idleConnsClosed)
 	}()
diff --git a/internal/server/tcp.go b/internal/server/tcp.go
index 4efde7d..12fdeb2 100644
--- a/internal/server/tcp.go
+++ b/internal/server/tcp.go
@@ -5,7 +5,7 @@ import (
 )
 
 func (s *Server) serveTCP() error {
-	l, err := listenfd.GetListener(0, s.Addr)
+	l, err := listenfd.GetListener(0, s.Addr, s.log.Named("tcp.listenfd"))
 	if err != nil {
 		return err
 	}
diff --git a/internal/server/tls.go b/internal/server/tls.go
index 6c48f86..655455c 100644
--- a/internal/server/tls.go
+++ b/internal/server/tls.go
@@ -8,7 +8,6 @@ import (
 	"strconv"
 
 	"go.alanpearce.eu/website/internal/listenfd"
-	"go.alanpearce.eu/website/internal/log"
 
 	"github.com/ardanlabs/conf/v3"
 	"github.com/caddyserver/caddy/v2"
@@ -27,10 +26,10 @@ type redisConfig struct {
 
 func (s *Server) serveTLS() (err error) {
 	var issuer *certmagic.ACMEIssuer
+	log := s.log.Named("tls")
 
 	// setting cfg.Logger is too late somehow
-	certmagic.Default.Logger = log.GetLogger().
-		Named("certmagic")
+	certmagic.Default.Logger = log.GetLogger().Named("certmagic")
 	cfg := certmagic.NewDefault()
 	cfg.DefaultServerName = s.config.Domains[0]
 
@@ -97,6 +96,7 @@ func (s *Server) serveTLS() (err error) {
 	ln, err := listenfd.GetListener(
 		1,
 		net.JoinHostPort(s.runtimeConfig.ListenAddress, strconv.Itoa(s.runtimeConfig.Port)),
+		log.Named("listenfd"),
 	)
 	if err != nil {
 		return errors.Wrap(err, "could not bind plain socket")
@@ -127,6 +127,7 @@ func (s *Server) serveTLS() (err error) {
 		0,
 		net.JoinHostPort(s.runtimeConfig.ListenAddress, strconv.Itoa(s.runtimeConfig.TLSPort)),
 		tlsConfig,
+		log.Named("listenfd"),
 	)
 	if err != nil {
 		return errors.Wrap(err, "could not bind tls socket")