From 879cc25421c66a959ab801f0937b4264a461fc7e Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Thu, 27 Jun 2024 13:34:21 +0200 Subject: use pointer to config in builder & templates --- internal/atom/atom.go | 2 +- internal/builder/builder.go | 17 ++++++++++------- internal/builder/template.go | 4 ++-- internal/server/server.go | 2 +- internal/sitemap/sitemap.go | 4 ++-- internal/website/mux.go | 2 +- templates/error.templ | 2 +- templates/homepage.templ | 2 +- templates/list.templ | 4 ++-- templates/page.templ | 2 +- templates/post.templ | 2 +- templates/tags.templ | 2 +- 12 files changed, 24 insertions(+), 21 deletions(-) diff --git a/internal/atom/atom.go b/internal/atom/atom.go index a5965a4..19ad0d1 100644 --- a/internal/atom/atom.go +++ b/internal/atom/atom.go @@ -7,7 +7,7 @@ import ( "website/internal/config" ) -func MakeTagURI(config config.Config, specific string) string { +func MakeTagURI(config *config.Config, specific string) string { return "tag:" + config.OriginalDomain + "," + config.DomainStartDate + ":" + specific } diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 4436151..177fa2f 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -86,7 +86,7 @@ func writerToFile(writer io.WriterTo, pathParts ...string) error { return nil } -func build(outDir string, config config.Config) (*Result, error) { +func build(outDir string, config *config.Config) (*Result, error) { log.Debug("output", "dir", outDir) r := &Result{ Hashes: make([]string, 0), @@ -252,16 +252,19 @@ func build(outDir string, config config.Config) (*Result, error) { return r, nil } -func BuildSite(ioConfig IOConfig) (*Result, error) { - config, err := config.GetConfig() - if err != nil { - return nil, errors.WithMessage(err, "could not get config") +func BuildSite(ioConfig IOConfig, cfg *config.Config) (*Result, error) { + if cfg == nil { + var err error + cfg, err = config.GetConfig() + if err != nil { + return nil, errors.WithMessage(err, "could not get config") + } } - config.InjectLiveReload = ioConfig.Development + cfg.InjectLiveReload = ioConfig.Development compressFiles = !ioConfig.Development templates.Setup() loadCSS() - return build(ioConfig.Destination, *config) + return build(ioConfig.Destination, cfg) } diff --git a/internal/builder/template.go b/internal/builder/template.go index da45fb7..d46844d 100644 --- a/internal/builder/template.go +++ b/internal/builder/template.go @@ -66,7 +66,7 @@ func (q *QueryDocument) Find(selector string) *QuerySelection { return &QuerySelection{q.Document.Find(selector)} } -func renderRobotsTXT(config config.Config) (io.Reader, error) { +func renderRobotsTXT(config *config.Config) (io.Reader, error) { r, w := io.Pipe() tpl, err := template.ParseFiles("templates/robots.tmpl") if err != nil { @@ -87,7 +87,7 @@ func renderRobotsTXT(config config.Config) (io.Reader, error) { func renderFeed( title string, - config config.Config, + config *config.Config, posts []content.Post, specific string, ) (io.Reader, error) { diff --git a/internal/server/server.go b/internal/server/server.go index 00cc022..469485f 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -108,7 +108,7 @@ func serverHeaderHandler(wrappedHandler http.Handler) http.Handler { } func rebuild(builderConfig builder.IOConfig, config *cfg.Config) error { - r, err := builder.BuildSite(builderConfig) + r, err := builder.BuildSite(builderConfig, config) if err != nil { return errors.WithMessage(err, "could not build site") } diff --git a/internal/sitemap/sitemap.go b/internal/sitemap/sitemap.go index a38e277..a45ebd9 100644 --- a/internal/sitemap/sitemap.go +++ b/internal/sitemap/sitemap.go @@ -13,9 +13,9 @@ type Sitemap struct { Sitemap *sitemap.Sitemap } -func New(cfg config.Config) *Sitemap { +func New(cfg *config.Config) *Sitemap { return &Sitemap{ - config: &cfg, + config: cfg, Sitemap: sitemap.New(), } } diff --git a/internal/website/mux.go b/internal/website/mux.go index c286903..c125561 100644 --- a/internal/website/mux.go +++ b/internal/website/mux.go @@ -50,7 +50,7 @@ func (fn WrappedWebHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err := fn.handler(w, r); err != nil { if strings.Contains(r.Header.Get("Accept"), "text/html") { w.WriteHeader(err.Code) - err := templates.Error(*fn.config, r.URL.Path, err).Render(r.Context(), w) + err := templates.Error(fn.config, r.URL.Path, err).Render(r.Context(), w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } diff --git a/templates/error.templ b/templates/error.templ index 2da7bef..c336ea9 100644 --- a/templates/error.templ +++ b/templates/error.templ @@ -6,7 +6,7 @@ import ( "strconv" ) -templ Error(config config.Config, path string, err *http.Error) { +templ Error(config *config.Config, path string, err *http.Error) { @Page(config, PageSettings{ Title: "Error", Path: path, diff --git a/templates/homepage.templ b/templates/homepage.templ index 0afbb2f..611a8ff 100644 --- a/templates/homepage.templ +++ b/templates/homepage.templ @@ -5,7 +5,7 @@ import ( "website/internal/content" ) -templ Homepage(config config.Config, posts []content.Post, content string) { +templ Homepage(config *config.Config, posts []content.Post, content string) { @Page(config, PageSettings{ Title: config.Title, TitleAttrs: templ.Attributes{ diff --git a/templates/list.templ b/templates/list.templ index 602c15c..94ccf5a 100644 --- a/templates/list.templ +++ b/templates/list.templ @@ -5,7 +5,7 @@ import ( "website/internal/content" ) -templ TagPage(config config.Config, tag string, posts []content.Post, path string) { +templ TagPage(config *config.Config, tag string, posts []content.Post, path string) { @Page(config, PageSettings{ Title: tag, Path: path, @@ -24,7 +24,7 @@ templ TagPage(config config.Config, tag string, posts []content.Post, path strin } } -templ ListPage(config config.Config, posts []content.Post, path string) { +templ ListPage(config *config.Config, posts []content.Post, path string) { @Page(config, PageSettings{ Title: config.Title, TitleAttrs: templ.Attributes{ diff --git a/templates/page.templ b/templates/page.templ index 7869369..1dbd345 100644 --- a/templates/page.templ +++ b/templates/page.templ @@ -43,7 +43,7 @@ templ menuItem(item config.MenuItem) { >{ item.Name } } -templ Page(site config.Config, page PageSettings) { +templ Page(site *config.Config, page PageSettings) {
diff --git a/templates/post.templ b/templates/post.templ index 7b82584..c432e57 100644 --- a/templates/post.templ +++ b/templates/post.templ @@ -19,7 +19,7 @@ templ postDate(d time.Time) { } -templ PostPage(config config.Config, post content.Post) { +templ PostPage(config *config.Config, post content.Post) { @Page(config, PageSettings{ Title: post.Title, TitleAttrs: templ.Attributes{ diff --git a/templates/tags.templ b/templates/tags.templ index 7218ca1..5e9878a 100644 --- a/templates/tags.templ +++ b/templates/tags.templ @@ -6,7 +6,7 @@ templ tagLink(tag string, attrs templ.Attributes) { #{ tag } } -templ TagsPage(config config.Config, title string, tags []string, path string) { +templ TagsPage(config *config.Config, title string, tags []string, path string) { @Page(config, PageSettings{ Title: title, Path: path, -- cgit 1.4.1