summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--srv/src/http/assets.go4
-rw-r--r--srv/src/http/posts.go10
-rw-r--r--srv/src/http/tpl.go59
-rw-r--r--srv/src/http/tpl/redirect.html2
4 files changed, 46 insertions, 29 deletions
diff --git a/srv/src/http/assets.go b/srv/src/http/assets.go
index aacef96..2cbe7a2 100644
--- a/srv/src/http/assets.go
+++ b/srv/src/http/assets.go
@@ -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))
})
}
diff --git a/srv/src/http/posts.go b/srv/src/http/posts.go
index c05f3de..2f96c6d 100644
--- a/srv/src/http/posts.go
+++ b/srv/src/http/posts.go
@@ -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))
})
}
diff --git a/srv/src/http/tpl.go b/srv/src/http/tpl.go
index dcd1551..2e22370 100644
--- a/srv/src/http/tpl.go
+++ b/srv/src/http/tpl.go
@@ -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, "/")
+ if trailingSlash && res != "/" {
+ res += "/"
+ }
- path = filepath.Join("/", path)
+ if abs {
+ res = a.params.PublicURL.String() + res
+ }
- if trailingSlash && path != "/" {
- path += "/"
- }
+ return res
+}
- return path
- }
+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,
})
}
diff --git a/srv/src/http/tpl/redirect.html b/srv/src/http/tpl/redirect.html
index ed12a2e..a50b324 100644
--- a/srv/src/http/tpl/redirect.html
+++ b/srv/src/http/tpl/redirect.html
@@ -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>