Standardize URL generation across the blog

This commit is contained in:
Brian Picciano 2022-05-21 11:34:03 -06:00
parent e269b111a1
commit 01424c7dab
4 changed files with 48 additions and 31 deletions

View File

@ -172,7 +172,7 @@ func (a *api) postPostAssetHandler() http.Handler {
return
}
a.executeRedirectTpl(rw, r, "assets/")
a.executeRedirectTpl(rw, r, a.assetsURL(false))
})
}
@ -199,6 +199,6 @@ func (a *api) deletePostAssetHandler() http.Handler {
return
}
a.executeRedirectTpl(rw, r, "assets/")
a.executeRedirectTpl(rw, r, a.assetsURL(false))
})
}

View File

@ -25,7 +25,7 @@ type postTplPayload struct {
func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload, error) {
bodyTpl, err := a.parseTpl(storedPost.Body)
bodyTpl, err := a.parseTpl(storedPost.ID+"-body.html", storedPost.Body)
if err != nil {
return postTplPayload{}, fmt.Errorf("parsing post body as template: %w", err)
}
@ -271,7 +271,7 @@ func (a *api) postPostHandler() http.Handler {
if first {
a.params.Logger.Info(r.Context(), "publishing blog post to mailing list")
urlStr := a.params.PublicURL.String() + filepath.Join("/posts", p.ID)
urlStr := a.postURL(p.ID, true)
if err := a.params.MailingList.Publish(p.Title, urlStr); err != nil {
apiutil.InternalServerError(
@ -281,9 +281,7 @@ func (a *api) postPostHandler() http.Handler {
}
}
redirectPath := fmt.Sprintf("posts/%s?edit", p.ID)
a.executeRedirectTpl(rw, r, redirectPath)
a.executeRedirectTpl(rw, r, a.postURL(p.ID, false)+"?edit")
})
}
@ -310,7 +308,7 @@ func (a *api) deletePostHandler() http.Handler {
return
}
a.executeRedirectTpl(rw, r, "posts/")
a.executeRedirectTpl(rw, r, a.postsURL(false))
})
}

View File

@ -6,6 +6,7 @@ import (
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"path/filepath"
"strings"
@ -28,37 +29,54 @@ func mustReadTplFile(fileName string) string {
return string(b)
}
func (a *api) parseTpl(tplBody string) (*template.Template, error) {
func (a *api) blogURL(path string, abs bool) string {
// filepath.Join strips trailing slash, but we want to keep it
trailingSlash := strings.HasSuffix(path, "/")
blogURL := func(path string) string {
res := filepath.Join("/", path)
// filepath.Join strips trailing slash, but we want to keep it
trailingSlash := strings.HasSuffix(path, "/")
path = filepath.Join("/", path)
if trailingSlash && path != "/" {
path += "/"
}
return path
if trailingSlash && res != "/" {
res += "/"
}
if abs {
res = a.params.PublicURL.String() + res
}
return res
}
func (a *api) postURL(id string, abs bool) string {
path := filepath.Join("posts", id)
return a.blogURL(path, abs)
}
func (a *api) postsURL(abs bool) string {
return a.blogURL("posts", abs)
}
func (a *api) assetsURL(abs bool) string {
return a.blogURL("assets", abs)
}
func (a *api) parseTpl(name, tplBody string) (*template.Template, error) {
tpl := template.New("root")
tpl = tpl.Funcs(template.FuncMap{
"BlogURL": blogURL,
"BlogURL": func(path string) string {
return a.blogURL(path, false)
},
"StaticURL": func(path string) string {
path = filepath.Join("static", path)
return blogURL(path)
return a.blogURL(path, false)
},
"AssetURL": func(id string) string {
path := filepath.Join("assets", id)
return blogURL(path)
return a.blogURL(path, false)
},
"PostURL": func(id string) string {
path := filepath.Join("posts", id)
return blogURL(path)
return a.postURL(id, false)
},
"DateTimeFormat": func(t time.Time) string {
return t.Format("2006-01-02")
@ -89,7 +107,7 @@ func (a *api) parseTpl(tplBody string) (*template.Template, error) {
var err error
if tpl, err = tpl.New("").Parse(tplBody); err != nil {
if tpl, err = tpl.New(name).Parse(tplBody); err != nil {
return nil, err
}
@ -97,7 +115,7 @@ func (a *api) parseTpl(tplBody string) (*template.Template, error) {
}
func (a *api) mustParseTpl(name string) *template.Template {
return template.Must(a.parseTpl(mustReadTplFile(name)))
return template.Must(a.parseTpl(name, mustReadTplFile(name)))
}
func (a *api) mustParseBasedTpl(name string) *template.Template {
@ -140,12 +158,13 @@ func executeTemplate(
}
func (a *api) executeRedirectTpl(
rw http.ResponseWriter, r *http.Request, path string,
rw http.ResponseWriter, r *http.Request, url string,
) {
log.Printf("here url:%q", url)
executeTemplate(rw, r, a.redirectTpl, struct {
Path string
URL string
}{
Path: path,
URL: url,
})
}

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0; url='{{ BlogURL .Payload.Path }}'" />
<meta http-equiv="refresh" content="0; url='{{ .Payload.URL }}'" />
</head>
<body>
<p>Redirecting...</p>