about summary refs log tree commit diff stats
path: root/internal/server/server.go
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/server.go
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/server.go')
-rw-r--r--internal/server/server.go32
1 files changed, 18 insertions, 14 deletions
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)
 	}()