about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-04-17 19:52:20 +0200
committerAlan Pearce2024-04-17 20:53:30 +0200
commit37dabe7476a9ed35a95d42ae32e3064fccbbea2d (patch)
treeb621a511105ae9d0551bff371d789ce28b8c6a43
parent9769d979701c039cce997d8b9c13a6f5f0f764e4 (diff)
downloadwebsite-37dabe7476a9ed35a95d42ae32e3064fccbbea2d.tar.lz
website-37dabe7476a9ed35a95d42ae32e3064fccbbea2d.tar.zst
website-37dabe7476a9ed35a95d42ae32e3064fccbbea2d.zip
config: convert base_url to a URL type
-rw-r--r--cmd/build/build.go4
-rw-r--r--internal/config/config.go12
2 files changed, 13 insertions, 3 deletions
diff --git a/cmd/build/build.go b/cmd/build/build.go
index ecc40a8..a8ce710 100644
--- a/cmd/build/build.go
+++ b/cmd/build/build.go
@@ -334,7 +334,7 @@ func renderFeed(title string, config Config, posts []Post, specific string) (io.
 	doc, err := xmlquery.Parse(reader)
 	feed := doc.SelectElement("feed")
 	feed.SelectElement("title").FirstChild.Data = title
-	feed.SelectElement("link").SetAttr("href", config.BaseURL)
+	feed.SelectElement("link").SetAttr("href", config.BaseURL.String())
 	feed.SelectElement("id").FirstChild.Data = MakeTagURI(config, specific)
 	datetime, err := posts[0].Date.UTC().MarshalText()
 	feed.SelectElement("updated").FirstChild.Data = string(datetime)
@@ -342,7 +342,7 @@ func renderFeed(title string, config Config, posts []Post, specific string) (io.
 	xmlquery.RemoveFromTree(tpl)
 
 	for _, post := range posts {
-		fullURL, err := url.JoinPath(config.BaseURL, post.URL)
+		fullURL, err := url.JoinPath(config.BaseURL.String(), post.URL)
 		if err != nil {
 			return nil, err
 		}
diff --git a/internal/config/config.go b/internal/config/config.go
index 20fe21e..cab5f90 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -3,6 +3,7 @@ package config
 import (
 	"io/fs"
 	"log/slog"
+	"net/url"
 
 	"github.com/BurntSushi/toml"
 	"github.com/pkg/errors"
@@ -18,9 +19,18 @@ type MenuItem struct {
 	URL  string `toml:"url"`
 }
 
+type URL struct {
+	*url.URL
+}
+
+func (u *URL) UnmarshalText(text []byte) (err error) {
+	u.URL, err = url.Parse(string(text))
+	return err
+}
+
 type Config struct {
 	DefaultLanguage        string `toml:"default_language"`
-	BaseURL                string `toml:"base_url"`
+	BaseURL                URL    `toml:"base_url"`
 	RedirectOtherHostnames bool   `toml:"redirect_other_hostnames"`
 	Title                  string
 	Email                  string