Simplify URL generation

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-11-23 00:14:23 +01:00
parent 662f340546
commit b98c71497c
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D

23
api.go
View file

@ -26,7 +26,7 @@ type (
} }
) )
func buildFullURL(u *url.URL) string { func buildFullURL(u *url.URL, _ error) string {
return strings.Join([]string{ return strings.Join([]string{
strings.TrimRight(cfg.BaseURL, "/"), strings.TrimRight(cfg.BaseURL, "/"),
strings.TrimLeft(u.String(), "/"), strings.TrimLeft(u.String(), "/"),
@ -155,28 +155,23 @@ func handleLogFeed(w http.ResponseWriter, r *http.Request) {
return return
} }
var (
feedTitle = "Latestver Update Log"
feedURL, _ = router.Get("catalog").URL()
)
if vars["name"] != "" {
feedTitle = fmt.Sprintf("Latestver Update Log of %s:%s", vars["name"], vars["tag"])
feedURL, _ = router.Get("catalog-entry").URL("name", vars["name"], "tag", vars["tag"])
}
feed := &feeds.Feed{ feed := &feeds.Feed{
Description: "Generated by go-latestver: https://github.com/Luzifer/go-latestver", Description: "Generated by go-latestver: https://github.com/Luzifer/go-latestver",
Link: &feeds.Link{Href: buildFullURL(feedURL)}, Link: &feeds.Link{Href: buildFullURL(router.Get("catalog").URL())},
Title: feedTitle, Title: "Latestver Update Log",
}
if vars["name"] != "" {
feed.Title = fmt.Sprintf("Latestver Update Log of %s:%s", vars["name"], vars["tag"])
feed.Link.Href = buildFullURL(router.Get("catalog-entry").URL("name", vars["name"], "tag", vars["tag"]))
} }
for _, le := range logs { for _, le := range logs {
catalogEntryURL, _ := router.Get("catalog-entry").URL("name", le.CatalogName, "tag", le.CatalogTag)
feed.Add(&feeds.Item{ feed.Add(&feeds.Item{
Created: le.Timestamp.UTC(), Created: le.Timestamp.UTC(),
Description: fmt.Sprintf("%s:%s updated to version %s from %s", le.CatalogName, le.CatalogTag, le.VersionTo, le.VersionFrom), Description: fmt.Sprintf("%s:%s updated to version %s from %s", le.CatalogName, le.CatalogTag, le.VersionTo, le.VersionFrom),
Id: fmt.Sprintf("%s:%s-%s", le.CatalogName, le.CatalogTag, le.Timestamp.UTC().Format(time.RFC3339)), Id: fmt.Sprintf("%s:%s-%s", le.CatalogName, le.CatalogTag, le.Timestamp.UTC().Format(time.RFC3339)),
Link: &feeds.Link{Href: buildFullURL(catalogEntryURL)}, Link: &feeds.Link{Href: buildFullURL(router.Get("catalog-entry").URL("name", le.CatalogName, "tag", le.CatalogTag))},
Title: fmt.Sprintf("%s:%s %s", le.CatalogName, le.CatalogTag, le.VersionTo), Title: fmt.Sprintf("%s:%s %s", le.CatalogName, le.CatalogTag, le.VersionTo),
}) })
} }