diff options
-rw-r--r-- | cmd/build/main.go | 24 | ||||
-rw-r--r-- | cmd/cspgenerator/cspgenerator.go | 4 | ||||
-rw-r--r-- | cmd/dev/main.go | 86 | ||||
-rw-r--r-- | cmd/server/main.go | 22 | ||||
-rw-r--r-- | go.mod | 11 | ||||
-rw-r--r-- | go.sum | 48 | ||||
-rw-r--r-- | internal/builder/builder.go | 26 | ||||
-rw-r--r-- | internal/builder/posts.go | 6 | ||||
-rw-r--r-- | internal/builder/template.go | 6 | ||||
-rw-r--r-- | internal/config/config.go | 4 | ||||
-rw-r--r-- | internal/log/log.go | 48 | ||||
-rw-r--r-- | internal/server/filemap.go | 8 | ||||
-rw-r--r-- | internal/server/server.go | 19 |
13 files changed, 197 insertions, 115 deletions
diff --git a/cmd/build/main.go b/cmd/build/main.go index 069f9bd..0b1cc46 100644 --- a/cmd/build/main.go +++ b/cmd/build/main.go @@ -3,11 +3,10 @@ package main import ( "fmt" "io/fs" - "log" - "log/slog" "os" "website/internal/builder" + "website/internal/log" "github.com/BurntSushi/toml" "github.com/ardanlabs/conf/v3" @@ -15,22 +14,17 @@ import ( ) func main() { - if os.Getenv("DEBUG") != "" { - slog.SetLogLoggerLevel(slog.LevelDebug) - } - log.SetFlags(log.LstdFlags | log.Lmsgprefix) - log.SetPrefix("build: ") - slog.Debug("starting build process") - ioConfig := builder.IOConfig{} if help, err := conf.Parse("", &ioConfig); err != nil { if errors.Is(err, conf.ErrHelpWanted) { fmt.Println(help) os.Exit(1) } - log.Panicf("parsing I/O configuration: %v", err) + panic("error parsing configuration: " + err.Error()) } + log.Configure(!ioConfig.Development) + log.Debug("starting build process") if ioConfig.Source != "." { err := os.Chdir(ioConfig.Source) if err != nil { @@ -41,15 +35,11 @@ func main() { if err := builder.BuildSite(ioConfig); err != nil { switch cause := errors.Cause(err).(type) { case *fs.PathError: - slog.Info("pathError") - slog.Error(fmt.Sprintf("%s", err)) + log.Error("path error", "error", err) case toml.ParseError: - slog.Info("parseError") - slog.Error(fmt.Sprintf("%s", err)) + log.Info("parse error", "error", err) default: - slog.Info("other") - slog.Error(fmt.Sprintf("cause:%+v", errors.Cause(cause))) - slog.Error(fmt.Sprintf("%+v", cause)) + log.Info("other error", "error", err, "cause", errors.Cause(cause)) } os.Exit(1) } diff --git a/cmd/cspgenerator/cspgenerator.go b/cmd/cspgenerator/cspgenerator.go index f79a591..89d2718 100644 --- a/cmd/cspgenerator/cspgenerator.go +++ b/cmd/cspgenerator/cspgenerator.go @@ -1,13 +1,13 @@ package main import ( - "log" "website/internal/config" + "website/internal/log" ) func main() { err := config.GenerateCSP() if err != nil { - log.Fatal(err) + log.Fatal("error generating csp", "error", err) } } diff --git a/cmd/dev/main.go b/cmd/dev/main.go index aa3102f..459cfaf 100644 --- a/cmd/dev/main.go +++ b/cmd/dev/main.go @@ -5,8 +5,6 @@ import ( "fmt" "io" "io/fs" - "log" - "log/slog" "net/http" "net/http/httputil" @@ -15,12 +13,12 @@ import ( "os/signal" "path" "path/filepath" - "strings" "sync" "syscall" "time" "website/internal/config" + "website/internal/log" "github.com/antage/eventsource" "github.com/ardanlabs/conf/v3" @@ -41,14 +39,14 @@ func RunCommandPiped( command string, args ...string, ) (cmd *exec.Cmd, err error) { - slog.Debug(fmt.Sprintf("running command %s %s", command, strings.Join(args, " "))) + log.Debug("running command", "command", command, "args", args) cmd = exec.CommandContext(ctx, command, args...) cmd.Env = append(os.Environ(), "DEBUG=") cmd.Cancel = func() error { - slog.Debug("signalling child") + log.Debug("signalling child") err := cmd.Process.Signal(os.Interrupt) if err != nil { - slog.Error(fmt.Sprintf("signal error: %v", err)) + log.Error("signal error:", "error", err) } return err } @@ -80,12 +78,12 @@ func NewFileWatcher(pollTime time.Duration) (*FileWatcher, error) { } func (watcher FileWatcher) WatchAllFiles(from string) error { - slog.Debug(fmt.Sprintf("watching files under %s", from)) + log.Debug("watching files under", "from", from) err := filepath.Walk(from, func(path string, info fs.FileInfo, err error) error { if err != nil { return err } - // slog.Debug(fmt.Sprintf("adding file %s to watcher", path)) + // log.Debug(fmt.Sprintf("adding file %s to watcher", path)) if err = watcher.Add(path); err != nil { return err } @@ -107,7 +105,11 @@ func build(ctx context.Context, config DevConfig) error { } err = cmd.Run() - slog.Debug(fmt.Sprintf("build command exited with code %d", cmd.ProcessState.ExitCode())) + log.Debug( + "build command exited", + "status", + cmd.ProcessState.ExitCode(), + ) if err != nil { return errors.WithMessage(err, "error running build command") } @@ -148,7 +150,7 @@ func server(ctx context.Context, devConfig DevConfig) error { for { select { case <-ctx.Done(): - slog.Debug("server context done") + log.Debug("server context done") err := cmd.Process.Signal(os.Interrupt) if err != nil { return err @@ -161,10 +163,8 @@ func server(ctx context.Context, devConfig DevConfig) error { } func main() { - if os.Getenv("DEBUG") != "" { - slog.SetLogLoggerLevel(slog.LevelDebug) - } var wg sync.WaitGroup + log.Configure(false) devConfig := DevConfig{} help, err := conf.Parse("", &devConfig) @@ -173,23 +173,23 @@ func main() { fmt.Println(help) os.Exit(1) } - log.Panicf("parsing dev configuration: %v", err) + log.Panic("parsing dev configuration", "error", err) } - slog.Debug(fmt.Sprintf("using folder %s for build output", devConfig.TempDir)) + log.Debug("running with in /tmp", "dir", devConfig.TempDir) ctx, cancel := context.WithCancel(context.Background()) defer cancel() - slog.Debug("setting interrupt handler") + log.Debug("setting interrupt handler") c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { sig := <-c - slog.Info(fmt.Sprintf("shutting down on signal %d", sig)) + log.Info("shutting down on signal", "sig", sig) cancel() sig = <-c - slog.Info(fmt.Sprintf("got second signal, dying %d", sig)) + log.Info("got second signal, dying", "sig", sig) os.Exit(1) }() @@ -205,9 +205,9 @@ func main() { go func() { defer wg.Done() defer devCancel() - slog.Debug("waiting for first server launch") + log.Debug("waiting for first server launch") <-serverChan - slog.Debug("got first server launch event") + log.Debug("got first server launch event") http.Handle("/", &httputil.ReverseProxy{ Rewrite: func(req *httputil.ProxyRequest) { @@ -220,7 +220,7 @@ func main() { go func() { err := srv.ListenAndServe() if err != nil && err != http.ErrServerClosed { - slog.Error(err.Error()) + log.Error(err.Error()) cancel() } done <- true @@ -228,29 +228,29 @@ func main() { go func() { for ready := range serverChan { if ready { - slog.Debug("sending reload message") + log.Debug("sending reload message") eventsource.SendEventMessage("reload", "", "") } else { - slog.Debug("server not ready") + log.Debug("server not ready") } } }() - slog.Info(fmt.Sprintf("dev server listening on %s", devConfig.BaseURL.Host)) + log.Info("dev server listening on", "host", devConfig.BaseURL.String()) <-done - slog.Debug("dev server closed") + log.Debug("dev server closed") }() fw, err := NewFileWatcher(500 * time.Millisecond) if err != nil { - log.Fatalf("error creating file watcher: %v", err) + log.Fatal("error creating file watcher", "error", err) } err = fw.WatchAllFiles("content") if err != nil { - log.Fatalf("could not watch files in content directory: %v", err) + log.Fatal("could not watch files in content directory", "error", err) } err = fw.WatchAllFiles("templates") if err != nil { - log.Fatalf("could not watch files in templates directory: %v", err) + log.Fatal("could not watch files in templates directory", "error", err) } var exitCode int @@ -258,14 +258,14 @@ func main() { loop: for { serverCtx, stopServer := context.WithCancel(ctx) - slog.Debug("starting build") + log.Debug("starting build") err := build(ctx, devConfig) if err != nil { - slog.Error(fmt.Sprintf("build error: %v", err)) + log.Error("build error:", "error", err) // don't set up the server until there's a FS change event } else { - slog.Debug("setting up server") + log.Debug("setting up server") wg.Add(1) go func() { defer wg.Done() @@ -276,43 +276,43 @@ loop: select { case <-ctx.Done(): - slog.Debug("main context cancelled") - slog.Debug("calling server shutdown") + log.Debug("main context cancelled") + log.Debug("calling server shutdown") err := srv.Shutdown(devCtx) if err != nil { - slog.Debug("shutdown error", "error", err) + log.Debug("shutdown error", "error", err) } exitCode = 1 break loop case event := <-fw.Events: - slog.Debug(fmt.Sprintf("event received: %v", event)) + log.Debug("event received:", "event", event) stopServer() serverChan <- false - slog.Debug("waiting for server shutdown") + log.Debug("waiting for server shutdown") <-serverErr - slog.Debug("server shutdown completed") + log.Debug("server shutdown completed") continue case err = <-serverErr: if err != nil && err != context.Canceled { var exerr *exec.ExitError - slog.Error(fmt.Sprintf("server reported error: %v", err)) + log.Error("server reported error:", "error", err) if errors.As(err, &exerr) { - slog.Debug("server exit error") + log.Debug("server exit error") } else { - slog.Debug("server other error") + log.Debug("server other error") } break } - slog.Debug("no error or server context cancelled") + log.Debug("no error or server context cancelled") continue } - slog.Debug("waiting on server") + log.Debug("waiting on server") exitCode = 0 break } - slog.Debug("waiting for wg before shutting down") + log.Debug("waiting for wg before shutting down") wg.Wait() os.Exit(exitCode) } diff --git a/cmd/server/main.go b/cmd/server/main.go index bae215a..464c438 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -2,12 +2,11 @@ package main import ( "fmt" - "log" - "log/slog" "os" "os/signal" "sync" + "website/internal/log" "website/internal/server" "github.com/ardanlabs/conf/v3" @@ -20,12 +19,6 @@ var ( ) func main() { - if os.Getenv("DEBUG") != "" { - slog.SetLogLoggerLevel(slog.LevelDebug) - } - log.SetFlags(log.LstdFlags | log.Lmsgprefix) - log.SetPrefix("server: ") - runtimeConfig := server.Config{} help, err := conf.Parse("", &runtimeConfig) if err != nil { @@ -33,23 +26,24 @@ func main() { fmt.Println(help) os.Exit(1) } - log.Panicf("parsing runtime configuration: %v", err) + panic("parsing runtime configuration" + err.Error()) } + log.Configure(runtimeConfig.Production) c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt) sv, err := server.New(&runtimeConfig) if err != nil { - log.Fatalf("error setting up server: %v", err) + log.Fatal("error setting up server", "error", err) } wg := &sync.WaitGroup{} wg.Add(1) go func() { defer wg.Done() sig := <-c - log.Printf("signal captured: %v", sig) + log.Info("signal captured", "sig", sig) <-sv.Stop() - slog.Debug("server stopped") + log.Debug("server stopped") }() sErr := make(chan error) @@ -59,13 +53,13 @@ func main() { sErr <- sv.Start() }() if !runtimeConfig.InDevServer { - log.Printf("server listening on %s", sv.Addr) + log.Info("server listening", "address", sv.Addr) } err = <-sErr if err != nil { // Error starting or closing listener: - log.Fatalf("error: %v", err) + log.Fatal("error", "error", err) } wg.Wait() } diff --git a/go.mod b/go.mod index 4887a56..a369312 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/fatih/structtag v1.2.0 github.com/getsentry/sentry-go v0.27.0 github.com/gohugoio/hugo v0.125.7 + github.com/nkmr-jp/zl v1.4.3 github.com/otiai10/copy v1.14.0 github.com/pkg/errors v0.9.1 github.com/shengyanli1982/law v0.1.15 @@ -26,24 +27,34 @@ require ( replace github.com/a-h/htmlformat => github.com/alanpearce/htmlformat v0.0.0-20240425000139-1244374b2562 require ( + github.com/Code-Hex/dd v1.1.0 // indirect github.com/andybalholm/cascadia v1.3.2 // indirect github.com/bep/godartsass v1.2.0 // indirect github.com/bep/godartsass/v2 v2.0.0 // indirect github.com/bep/golibsass v1.1.1 // indirect github.com/cli/safeexec v1.0.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gobwas/glob v0.2.3 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/logrusorgru/aurora/v4 v4.0.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mitchellh/hashstructure v1.1.0 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/samber/lo v1.39.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect + github.com/sykesm/zap-logfmt v0.0.4 // indirect github.com/tdewolff/parse/v2 v2.7.14 // indirect + github.com/thessem/zap-prettyconsole v0.4.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.27.0 // indirect + golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect google.golang.org/protobuf v1.34.1 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index c72aa1c..81c549f 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/BurntSushi/locker v0.0.0-20171006230638-a6e239ea1c69/go.mod h1:L1AbZd github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/Code-Hex/dd v1.1.0 h1:VEtTThnS9l7WhpKUIpdcWaf0B8Vp0LeeSEsxA1DZseI= +github.com/Code-Hex/dd v1.1.0/go.mod h1:VaMyo/YjTJ3d4qm/bgtrUkT2w+aYwJ07Y7eCWyrJr1w= github.com/PuerkitoBio/goquery v1.9.2 h1:4/wZksC3KgkQw7SQgkKotmKljk0M6V8TUvA8Wb4yPeE= github.com/PuerkitoBio/goquery v1.9.2/go.mod h1:GHPCaP0ODyyxqcNoFGYlAprUFH81NuRPd0GX3Zu2Mvk= github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4= @@ -127,6 +129,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/hairyhenderson/go-codeowners v0.4.0 h1:Wx/tRXb07sCyHeC8mXfio710Iu35uAy5KYiBdLHdv4Q= github.com/hairyhenderson/go-codeowners v0.4.0/go.mod h1:iJgZeCt+W/GzXo5uchFCqvVHZY2T4TAIpvuVlKVkLxc= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= @@ -137,6 +140,7 @@ github.com/jdkato/prose v1.2.1 h1:Fp3UnJmLVISmlc57BgKUzdjr0lOtjqTZicL3PaYy6cU= github.com/jdkato/prose v1.2.1/go.mod h1:AiRHgVagnEx2JbQRQowVBKjG0bcs/vtkGCH1dYAL1rA= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= @@ -148,6 +152,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kyokomi/emoji/v2 v2.2.12 h1:sSVA5nH9ebR3Zji1o31wu3yOwD1zKXQA2z0zUyeit60= github.com/kyokomi/emoji/v2 v2.2.12/go.mod h1:JUcn42DTdsXJo1SWanHh4HKDEyPaR5CqkmoirZZP9qE= +github.com/logrusorgru/aurora/v4 v4.0.0 h1:sRjfPpun/63iADiSvGGjgA1cAYegEWMPCJdUpJYn9JA= +github.com/logrusorgru/aurora/v4 v4.0.0/go.mod h1:lP0iIa2nrnT/qoFXcOZSrZQpJ1o6n2CUf/hyHi2Q4ZQ= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/makeworld-the-better-one/dither/v2 v2.4.0 h1:Az/dYXiTcwcRSe59Hzw4RI1rSnAZns+1msaCXetrMFE= @@ -170,6 +176,8 @@ github.com/muesli/smartcrop v0.3.0 h1:JTlSkmxWg/oQ1TcLDoypuirdE8Y/jzNirQeLkxpA6O github.com/muesli/smartcrop v0.3.0/go.mod h1:i2fCI/UorTfgEpPPLWiFBv4pye+YAG78RwcQLUkocpI= github.com/niklasfasching/go-org v1.7.0 h1:vyMdcMWWTe/XmANk19F4k8XGBYg0GQ/gJGMimOjGMek= github.com/niklasfasching/go-org v1.7.0/go.mod h1:WuVm4d45oePiE0eX25GqTDQIt/qPW1T9DGkRscqLW5o= +github.com/nkmr-jp/zl v1.4.3 h1:fzjeG+OdNWW5O9Yn3VrzXWTeiSqDFbSzJ77H8yCYNa4= +github.com/nkmr-jp/zl v1.4.3/go.mod h1:3XXgnW5Lj+6xfcowgLWn1JwUhDc+MSDIQuQLrjeS8oU= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU= @@ -184,16 +192,20 @@ github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw= github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc= github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk= +github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA= +github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/shengyanli1982/law v0.1.15 h1:puSn0Saa+0ptjszspycfWHPSu0D3kBcU3oEeW83MRIc= github.com/shengyanli1982/law v0.1.15/go.mod h1:20k9YnOTwilUB4X5Z4S7TIX5Ek1Ok4xfx8V8ZxIWlyM= github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= @@ -204,32 +216,51 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/sykesm/zap-logfmt v0.0.4 h1:U2WzRvmIWG1wDLCFY3sz8UeEmsdHQjHFNlIdmroVFaI= +github.com/sykesm/zap-logfmt v0.0.4/go.mod h1:AuBd9xQjAe3URrWT1BBDk2v2onAZHkZkWRMiYZXiZWA= github.com/tdewolff/minify/v2 v2.20.20 h1:vhULb+VsW2twkplgsawAoUY957efb+EdiZ7zu5fUhhk= github.com/tdewolff/minify/v2 v2.20.20/go.mod h1:GYaLXFpIIwsX99apQHXfGdISUdlA98wmaoWxjT9C37k= github.com/tdewolff/parse/v2 v2.7.14 h1:100KJ+QAO3PpMb3uUjzEU/NpmCdbBYz6KPmCIAfWpR8= github.com/tdewolff/parse/v2 v2.7.14/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA= github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52 h1:gAQliwn+zJrkjAHVcBEYW/RFvd2St4yYimisvozAYlA= github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE= +github.com/thessem/zap-prettyconsole v0.4.0 h1:905GshsxOSVz44hm0mEr00Dk/gjf1Aoq3tkW1o2kKuw= +github.com/thessem/zap-prettyconsole v0.4.0/go.mod h1:6bRZpKuje/vxEMEAlF22qPHkcO8kfJSrL/0NALxuZoQ= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.7.1 h1:3bajkSilaCbjdKVsKdZjZCLBNPL9pYzrCakKaf4U49U= github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= github.com/yuin/goldmark-emoji v1.0.2 h1:c/RgTShNgHTtc6xdz2KKI74jJr6rWi7FPgnP9GAsO5s= github.com/yuin/goldmark-emoji v1.0.2/go.mod h1:RhP/RWpexdp+KHs7ghKnifRoIs/Bq4nDS7tRbCkOwKY= +go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= +go.uber.org/zap v1.12.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE= -golang.org/x/exp v0.0.0-20221031165847-c99f073a8326/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM= +golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc= golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8= golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= @@ -238,6 +269,7 @@ golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= @@ -256,6 +288,7 @@ golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -281,11 +314,14 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY= -golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg= +golang.org/x/tools v0.21.0 h1:qc0xYgIbsSDt9EyWz05J5wfa7LOVW0YTLOXrqdLAWIw= +golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= @@ -311,6 +347,9 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= @@ -319,3 +358,4 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= diff --git a/internal/builder/builder.go b/internal/builder/builder.go index cb3b670..a8205a3 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -3,13 +3,13 @@ package builder import ( "fmt" "io" - "log/slog" "net/url" "os" "path" "slices" "website/internal/config" + "website/internal/log" cp "github.com/otiai10/copy" "github.com/pkg/errors" @@ -27,7 +27,7 @@ func mkdirp(dirs ...string) error { } func outputToFile(output io.Reader, filename ...string) error { - slog.Debug(fmt.Sprintf("outputting file %s", path.Join(filename...))) + log.Debug("outputting file", "filename", path.Join(filename...)) file, err := os.OpenFile(path.Join(filename...), os.O_WRONLY|os.O_CREATE, 0644) if err != nil { return errors.WithMessage(err, "could not open output file") @@ -41,7 +41,7 @@ func outputToFile(output io.Reader, filename ...string) error { } func build(outDir string, config config.Config) error { - slog.Debug(fmt.Sprintf("output directory %s", outDir)) + log.Debug("output", "dir", outDir) privateDir := path.Join(outDir, "private") if err := mkdirp(privateDir); err != nil { return errors.WithMessage(err, "could not create private directory") @@ -62,7 +62,7 @@ func build(outDir string, config config.Config) error { if err := mkdirp(publicDir, "post"); err != nil { return errors.WithMessage(err, "could not create post output directory") } - slog.Debug("reading posts") + log.Debug("reading posts") posts, tags, err := readPosts("content", "post", publicDir) if err != nil { return err @@ -72,7 +72,7 @@ func build(outDir string, config config.Config) error { if err := mkdirp(publicDir, "post", post.Basename); err != nil { return errors.WithMessage(err, "could not create directory for post") } - slog.Debug("rendering post", "post", post.Basename) + log.Debug("rendering post", "post", post.Basename) output, err := renderPost(post, config) if err != nil { return errors.WithMessagef(err, "could not render post %s", post.Input) @@ -85,7 +85,7 @@ func build(outDir string, config config.Config) error { if err := mkdirp(publicDir, "tags"); err != nil { return errors.WithMessage(err, "could not create directory for tags") } - slog.Debug("rendering tags list") + log.Debug("rendering tags list") output, err := renderTags(tags, config, "/tags") if err != nil { return errors.WithMessage(err, "could not render tags") @@ -104,7 +104,7 @@ func build(outDir string, config config.Config) error { if err := mkdirp(publicDir, "tags", tag); err != nil { return errors.WithMessage(err, "could not create directory") } - slog.Debug("rendering tags page", "tag", tag) + log.Debug("rendering tags page", "tag", tag) output, err := renderListPage(tag, config, matchingPosts, "/tags/"+tag) if err != nil { return errors.WithMessage(err, "could not render tag page") @@ -113,7 +113,7 @@ func build(outDir string, config config.Config) error { return err } - slog.Debug("rendering tags feed", "tag", tag) + log.Debug("rendering tags feed", "tag", tag) output, err = renderFeed( fmt.Sprintf("%s - %s", config.Title, tag), config, @@ -128,7 +128,7 @@ func build(outDir string, config config.Config) error { } } - slog.Debug("rendering list page") + log.Debug("rendering list page") listPage, err := renderListPage("", config, posts, "/post") if err != nil { return errors.WithMessage(err, "could not render list page") @@ -137,7 +137,7 @@ func build(outDir string, config config.Config) error { return err } - slog.Debug("rendering feed") + log.Debug("rendering feed") feed, err := renderFeed(config.Title, config, posts, "feed") if err != nil { return errors.WithMessage(err, "could not render feed") @@ -146,7 +146,7 @@ func build(outDir string, config config.Config) error { return err } - slog.Debug("rendering feed styles") + log.Debug("rendering feed styles") feedStyles, err := renderFeedStyles() if err != nil { return errors.WithMessage(err, "could not render feed styles") @@ -155,7 +155,7 @@ func build(outDir string, config config.Config) error { return err } - slog.Debug("rendering homepage") + log.Debug("rendering homepage") homePage, err := renderHomepage(config, posts, "/") if err != nil { return errors.WithMessage(err, "could not render homepage") @@ -164,7 +164,7 @@ func build(outDir string, config config.Config) error { return err } - slog.Debug("rendering 404 page") + log.Debug("rendering 404 page") notFound, err := render404(config, "/404.html") if err != nil { return errors.WithMessage(err, "could not render 404 page") diff --git a/internal/builder/posts.go b/internal/builder/posts.go index a4526e4..954e259 100644 --- a/internal/builder/posts.go +++ b/internal/builder/posts.go @@ -2,13 +2,13 @@ package builder import ( "bytes" - "log/slog" "os" "path" "path/filepath" "slices" "strings" "time" + "website/internal/log" "github.com/adrg/frontmatter" mapset "github.com/deckarep/golang-set/v2" @@ -91,7 +91,7 @@ func readPosts(root string, inputDir string, outputDir string) ([]Post, Tags, er if !f.IsDir() && path.Ext(pathFromRoot) == ".md" { output := outputReplacer.Replace(pathFromRoot) url := urlReplacer.Replace(pathFromRoot) - slog.Debug("reading post", "post", pathFromRoot) + log.Debug("reading post", "post", pathFromRoot) matter, content, err := getPost(pathFromRoot) if err != nil { return nil, nil, err @@ -101,7 +101,7 @@ func readPosts(root string, inputDir string, outputDir string) ([]Post, Tags, er tags.Add(strings.ToLower(tag)) } - slog.Debug("rendering markdown in post", "post", pathFromRoot) + log.Debug("rendering markdown in post", "post", pathFromRoot) html, err := renderMarkdown(content) if err != nil { return nil, nil, err diff --git a/internal/builder/template.go b/internal/builder/template.go index 5bd990e..c63e1c2 100644 --- a/internal/builder/template.go +++ b/internal/builder/template.go @@ -4,7 +4,6 @@ import ( "encoding/xml" "fmt" "io" - "log/slog" "net/url" "os" "strings" @@ -12,6 +11,7 @@ import ( "time" "website/internal/atom" "website/internal/config" + "website/internal/log" "github.com/PuerkitoBio/goquery" "github.com/a-h/htmlformat" @@ -391,12 +391,12 @@ func renderHTML(doc *goquery.Document) io.Reader { go func() { _, err := w.Write([]byte("<!doctype html>\n")) if err != nil { - slog.Error("error writing doctype") + log.Error("error writing doctype", "error", err) w.CloseWithError(err) } err = htmlformat.Nodes(w, []*html.Node{doc.Children().Get(0)}) if err != nil { - slog.Error("error rendering html", "error", err) + log.Error("error rendering html", "error", err) w.CloseWithError(err) return } diff --git a/internal/config/config.go b/internal/config/config.go index 47422e7..063f549 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,8 +2,8 @@ package config import ( "io/fs" - "log/slog" "net/url" + "website/internal/log" "github.com/BurntSushi/toml" "github.com/pkg/errors" @@ -47,7 +47,7 @@ type Config struct { func GetConfig() (*Config, error) { config := Config{} - slog.Debug("reading config.toml") + log.Debug("reading config.toml") _, err := toml.DecodeFile("config.toml", &config) if err != nil { var pathError *fs.PathError diff --git a/internal/log/log.go b/internal/log/log.go new file mode 100644 index 0000000..e16d7bb --- /dev/null +++ b/internal/log/log.go @@ -0,0 +1,48 @@ +package log + +import ( + "os" + + zaplogfmt "github.com/sykesm/zap-logfmt" + prettyconsole "github.com/thessem/zap-prettyconsole" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +var logger *zap.SugaredLogger + +func DPanic(msg string, rest ...any) { + logger.DPanicw(msg, rest...) +} +func Debug(msg string, rest ...any) { + logger.Debugw(msg, rest...) +} +func Info(msg string, rest ...any) { + logger.Infow(msg, rest...) +} +func Warn(msg string, rest ...any) { + logger.Warnw(msg, rest...) +} +func Error(msg string, rest ...any) { + logger.Errorw(msg, rest...) +} +func Panic(msg string, rest ...any) { + logger.Panicw(msg, rest...) +} +func Fatal(msg string, rest ...any) { + logger.Fatalw(msg, rest...) +} + +func Configure(isProduction bool) { + var l *zap.Logger + if isProduction { + cfg := zap.NewProductionEncoderConfig() + cfg.TimeKey = "" + l = zap.New(zapcore.NewCore(zaplogfmt.NewEncoder(cfg), os.Stderr, zapcore.InfoLevel)) + } else { + cfg := prettyconsole.NewConfig() + cfg.EncoderConfig.TimeKey = "" + l = zap.Must(cfg.Build()) + } + logger = l.WithOptions(zap.AddCallerSkip(1)).Sugar() +} diff --git a/internal/server/filemap.go b/internal/server/filemap.go index 466db49..6130e65 100644 --- a/internal/server/filemap.go +++ b/internal/server/filemap.go @@ -5,12 +5,12 @@ import ( "hash/fnv" "io" "io/fs" - "log" - "log/slog" "os" "path/filepath" "strings" + "website/internal/log" + "github.com/pkg/errors" ) @@ -36,7 +36,7 @@ func hashFile(filename string) (string, error) { func registerFile(urlpath string, filepath string) error { if files[urlpath] != (File{}) { - log.Printf("registerFile called with duplicate file, urlPath: %s", urlpath) + log.Info("registerFile called with duplicate file", "url_path", urlpath) return nil } hash, err := hashFile(filepath) @@ -61,7 +61,7 @@ func registerContentFiles(root string) error { } urlPath, _ := strings.CutSuffix(relPath, "index.html") if !f.IsDir() { - slog.Debug("registering file", "urlpath", "/"+urlPath) + log.Debug("registering file", "urlpath", "/"+urlPath) return registerFile("/"+urlPath, filePath) } return nil diff --git a/internal/server/server.go b/internal/server/server.go index cbee989..62e32d5 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -4,8 +4,6 @@ import ( "context" "fmt" "io" - "log" - "log/slog" "mime" "net" "net/http" @@ -16,6 +14,7 @@ import ( "time" cfg "website/internal/config" + "website/internal/log" "github.com/getsentry/sentry-go" sentryhttp "github.com/getsentry/sentry-go/http" @@ -89,7 +88,7 @@ func (fn webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer func() { if fail := recover(); fail != nil { w.WriteHeader(http.StatusInternalServerError) - slog.Error("runtime panic!", "error", fail) + log.Error("runtime panic!", "error", fail) } }() w.Header().Set("Server", fmt.Sprintf("website (%s)", ShortSHA)) @@ -111,7 +110,7 @@ var newMIMEs = map[string]string{ func fixupMIMETypes() { for ext, newType := range newMIMEs { if err := mime.AddExtensionType(ext, newType); err != nil { - slog.Error("could not update mime type", "ext", ext, "mime", newType) + log.Error("could not update mime type", "ext", ext, "mime", newType) } } } @@ -134,7 +133,7 @@ func New(runtimeConfig *Config) (*Server, error) { } prefix := path.Join(runtimeConfig.Root, "public") - slog.Debug("registering content files", "prefix", prefix) + log.Debug("registering content files", "prefix", prefix) err = registerContentFiles(prefix) if err != nil { return nil, errors.WithMessagef(err, "registering content files") @@ -161,7 +160,7 @@ func New(runtimeConfig *Config) (*Server, error) { top := http.NewServeMux() mux := http.NewServeMux() - slog.Debug("binding main handler to", "host", runtimeConfig.BaseURL.Hostname()+"/") + log.Debug("binding main handler to", "host", runtimeConfig.BaseURL.Hostname()+"/") mux.Handle(runtimeConfig.BaseURL.Hostname()+"/", webHandler(serveFile)) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { @@ -205,19 +204,19 @@ func (s *Server) Start() error { } func (s *Server) Stop() chan struct{} { - slog.Debug("stop called") + log.Debug("stop called") idleConnsClosed := make(chan struct{}) go func() { - slog.Debug("shutting down server") + log.Debug("shutting down server") ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() err := s.Server.Shutdown(ctx) - slog.Debug("server shut down") + log.Debug("server shut down") if err != nil { // Error from closing listeners, or context timeout: - log.Printf("HTTP server Shutdown: %v", err) + log.Warn("HTTP server Shutdown", "error", err) } close(idleConnsClosed) }() |