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 return
} }
a.executeRedirectTpl(rw, r, "assets/") a.executeRedirectTpl(rw, r, a.assetsURL(false))
}) })
} }
@ -199,6 +199,6 @@ func (a *api) deletePostAssetHandler() http.Handler {
return 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) { 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 { if err != nil {
return postTplPayload{}, fmt.Errorf("parsing post body as template: %w", err) return postTplPayload{}, fmt.Errorf("parsing post body as template: %w", err)
} }
@ -271,7 +271,7 @@ func (a *api) postPostHandler() http.Handler {
if first { if first {
a.params.Logger.Info(r.Context(), "publishing blog post to mailing list") 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 { if err := a.params.MailingList.Publish(p.Title, urlStr); err != nil {
apiutil.InternalServerError( apiutil.InternalServerError(
@ -281,9 +281,7 @@ func (a *api) postPostHandler() http.Handler {
} }
} }
redirectPath := fmt.Sprintf("posts/%s?edit", p.ID) a.executeRedirectTpl(rw, r, a.postURL(p.ID, false)+"?edit")
a.executeRedirectTpl(rw, r, redirectPath)
}) })
} }
@ -310,7 +308,7 @@ func (a *api) deletePostHandler() http.Handler {
return return
} }
a.executeRedirectTpl(rw, r, "posts/") a.executeRedirectTpl(rw, r, a.postsURL(false))
}) })
} }

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"io/fs" "io/fs"
"log"
"net/http" "net/http"
"path/filepath" "path/filepath"
"strings" "strings"
@ -28,37 +29,54 @@ func mustReadTplFile(fileName string) string {
return string(b) 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 if trailingSlash && res != "/" {
trailingSlash := strings.HasSuffix(path, "/") res += "/"
path = filepath.Join("/", path)
if trailingSlash && path != "/" {
path += "/"
}
return path
} }
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 := template.New("root")
tpl = tpl.Funcs(template.FuncMap{ tpl = tpl.Funcs(template.FuncMap{
"BlogURL": blogURL, "BlogURL": func(path string) string {
return a.blogURL(path, false)
},
"StaticURL": func(path string) string { "StaticURL": func(path string) string {
path = filepath.Join("static", path) path = filepath.Join("static", path)
return blogURL(path) return a.blogURL(path, false)
}, },
"AssetURL": func(id string) string { "AssetURL": func(id string) string {
path := filepath.Join("assets", id) path := filepath.Join("assets", id)
return blogURL(path) return a.blogURL(path, false)
}, },
"PostURL": func(id string) string { "PostURL": func(id string) string {
path := filepath.Join("posts", id) return a.postURL(id, false)
return blogURL(path)
}, },
"DateTimeFormat": func(t time.Time) string { "DateTimeFormat": func(t time.Time) string {
return t.Format("2006-01-02") return t.Format("2006-01-02")
@ -89,7 +107,7 @@ func (a *api) parseTpl(tplBody string) (*template.Template, error) {
var err 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 return nil, err
} }
@ -97,7 +115,7 @@ func (a *api) parseTpl(tplBody string) (*template.Template, error) {
} }
func (a *api) mustParseTpl(name string) *template.Template { 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 { func (a *api) mustParseBasedTpl(name string) *template.Template {
@ -140,12 +158,13 @@ func executeTemplate(
} }
func (a *api) executeRedirectTpl( 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 { executeTemplate(rw, r, a.redirectTpl, struct {
Path string URL string
}{ }{
Path: path, URL: url,
}) })
} }

View File

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