config: convert base_url to a URL type
Alan Pearce alan@alanpearce.eu
Wed, 17 Apr 2024 19:52:20 +0200
2 files changed, 13 insertions(+), 3 deletions(-)
M cmd/build/build.go → cmd/build/build.go
@@ -334,7 +334,7 @@ defer reader.Close() 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 @@ tpl := feed.SelectElement("entry") 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 }
M internal/config/config.go → internal/config/config.go
@@ -3,6 +3,7 @@ import ( "io/fs" "log/slog" + "net/url" "github.com/BurntSushi/toml" "github.com/pkg/errors" @@ -18,9 +19,18 @@ Name string 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