From 1573550c7792a0403c2b4fa0959bd349c5f945df Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 21 Apr 2024 21:55:23 +0200 Subject: fix feed linking to localhost 🤦🏻 --- cmd/build/build.go | 46 ++++++++++++++++++++++++++++------------------ internal/config/config.go | 17 ----------------- justfile | 2 +- 3 files changed, 29 insertions(+), 36 deletions(-) diff --git a/cmd/build/build.go b/cmd/build/build.go index 121d5aa..254eff2 100644 --- a/cmd/build/build.go +++ b/cmd/build/build.go @@ -403,12 +403,7 @@ func outputToFile(output io.Reader, filename ...string) error { return nil } -func build(outDir string) error { - config, err := config.GetConfig() - if err != nil { - return err - } - +func build(outDir string, config config.Config) error { privateDir := path.Join(outDir, "private") if err := mkdirp(privateDir); err != nil { return errors.Errorf("could not create private directory: %w", err) @@ -418,7 +413,7 @@ func build(outDir string) error { return errors.Errorf("could not create public directory: %w", err) } - err = cp.Copy("static", publicDir, cp.Options{ + err := cp.Copy("static", publicDir, cp.Options{ PreserveTimes: true, PermissionControl: cp.AddPermission(0755), }) @@ -440,7 +435,7 @@ func build(outDir string) error { return errors.WithMessage(err, "could not create directory for post") } slog.Debug("rendering post", "post", post.Basename) - output, err := renderPost(post, *config) + output, err := renderPost(post, config) if err != nil { return errors.WithMessagef(err, "could not render post %s", post.Input) } @@ -453,7 +448,7 @@ func build(outDir string) error { return errors.WithMessage(err, "could not create directory for tags") } slog.Debug("rendering tags list") - output, err := renderTags(tags, *config) + output, err := renderTags(tags, config) if err != nil { return errors.WithMessage(err, "could not render tags") } @@ -472,7 +467,7 @@ func build(outDir string) error { return errors.WithMessage(err, "could not create directory") } slog.Debug("rendering tags page", "tag", tag) - output, err := renderListPage(tag, *config, matchingPosts) + output, err := renderListPage(tag, config, matchingPosts) if err != nil { return errors.WithMessage(err, "could not render tag page") } @@ -481,7 +476,7 @@ func build(outDir string) error { } slog.Debug("rendering tags feed", "tag", tag) - output, err = renderFeed(fmt.Sprintf("%s - %s", config.Title, tag), *config, matchingPosts, tag) + output, err = renderFeed(fmt.Sprintf("%s - %s", config.Title, tag), config, matchingPosts, tag) if err != nil { return errors.WithMessage(err, "could not render tag feed page") } @@ -491,7 +486,7 @@ func build(outDir string) error { } slog.Debug("rendering list page") - listPage, err := renderListPage("", *config, posts) + listPage, err := renderListPage("", config, posts) if err != nil { return errors.WithMessage(err, "could not render list page") } @@ -500,7 +495,7 @@ func build(outDir string) error { } slog.Debug("rendering feed") - feed, err := renderFeed(config.Title, *config, posts, "feed") + feed, err := renderFeed(config.Title, config, posts, "feed") if err != nil { return errors.WithMessage(err, "could not render feed") } @@ -518,7 +513,7 @@ func build(outDir string) error { } slog.Debug("rendering homepage") - homePage, err := renderHomepage(*config, posts) + homePage, err := renderHomepage(config, posts) if err != nil { return errors.WithMessage(err, "could not render homepage") } @@ -527,7 +522,7 @@ func build(outDir string) error { } slog.Debug("rendering 404 page") - notFound, err := render404(*config) + notFound, err := render404(config) if err != nil { return errors.WithMessage(err, "could not render 404 page") } @@ -541,6 +536,7 @@ func build(outDir string) error { type IOConfig struct { Source string `conf:"default:.,short:s"` Destination string `conf:"default:website,short:d"` + BaseURL config.URL } func main() { @@ -550,7 +546,13 @@ func main() { slog.Debug("starting build process") ioConfig := IOConfig{} - conf.Parse("", &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) + } if ioConfig.Source != "." { err := os.Chdir(ioConfig.Source) @@ -558,12 +560,20 @@ func main() { log.Panic("could not change to source directory") } } - err := os.RemoveAll(ioConfig.Destination) + + config, err := config.GetConfig() + if err != nil { + log.Panic(errors.Errorf("could not get config: %v", err)) + } + + config.BaseURL = ioConfig.BaseURL + + err = os.RemoveAll(ioConfig.Destination) if err != nil { log.Panic(errors.Errorf("could not remove public directory: %v", err)) } - if err := build(ioConfig.Destination); err != nil { + if err := build(ioConfig.Destination, *config); err != nil { switch cause := errors.Cause(err).(type) { case *fs.PathError: slog.Info("pathError") diff --git a/internal/config/config.go b/internal/config/config.go index ebd3cfb..d2eabf0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,7 +1,6 @@ package config import ( - "fmt" "io/fs" "log/slog" "net/url" @@ -57,15 +56,6 @@ func getEnvFallback(key, fallback string) string { } } -func setDevelopmentOverrides(config *Config) error { - overrideURL, err := URL.Parse(config.BaseURL, "http://localhost:"+fmt.Sprint(config.Port)) - if err != nil { - return err - } - config.BaseURL.URL = overrideURL - return nil -} - func GetConfig() (*Config, error) { config := Config{} slog.Debug("reading config.toml") @@ -86,12 +76,5 @@ func GetConfig() (*Config, error) { return nil, err } config.Port = port - config.Production = os.Getenv("ENV") == "production" - if !config.Production { - err = setDevelopmentOverrides(&config) - if err != nil { - return nil, errors.WithMessage(err, "could not override configuration") - } - } return &config, nil } diff --git a/justfile b/justfile index 3ebc592..93688c8 100755 --- a/justfile +++ b/justfile @@ -21,7 +21,7 @@ update-all: watch-flake command: watchexec --restart -w flake.nix -w flake.lock direnv exec . {{ command }} -watch-builder: (watch-flake "watchexec -i cmd/server -i public -r go run ./cmd/build") +watch-builder: (watch-flake "watchexec -i cmd/server -i public -r go run ./cmd/build --base-url http://localhost:3000") nix-build what: nix build .#{{ what }} -- cgit 1.4.1