summaryrefslogtreecommitdiff
path: root/srv/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'srv/src/api')
-rw-r--r--srv/src/api/api.go4
-rw-r--r--srv/src/api/render.go16
-rw-r--r--srv/src/api/tpl/base.html7
3 files changed, 20 insertions, 7 deletions
diff --git a/srv/src/api/api.go b/srv/src/api/api.go
index 37ea1fc..7323d29 100644
--- a/srv/src/api/api.go
+++ b/srv/src/api/api.go
@@ -26,6 +26,10 @@ type Params struct {
Logger *mlog.Logger
PowManager pow.Manager
+ // PathPrefix, if given, will be prefixed to all url paths which are
+ // rendered by the API's templating system.
+ PathPrefix string
+
PostStore post.Store
PostAssetStore post.AssetStore
diff --git a/srv/src/api/render.go b/srv/src/api/render.go
index cacdb26..2b6b5a0 100644
--- a/srv/src/api/render.go
+++ b/srv/src/api/render.go
@@ -20,7 +20,7 @@ import (
//go:embed tpl
var tplFS embed.FS
-func mustParseTpl(name string) *template.Template {
+func (a *api) mustParseTpl(name string) *template.Template {
mustRead := func(fileName string) string {
path := filepath.Join("tpl", fileName)
@@ -33,7 +33,15 @@ func mustParseTpl(name string) *template.Template {
return string(b)
}
- tpl := template.Must(template.New("").Parse(mustRead(name)))
+ blogURL := func(path string) string {
+ return filepath.Join(a.params.PathPrefix, "/v2", path)
+ }
+
+ tpl := template.New("").Funcs(template.FuncMap{
+ "BlogURL": blogURL,
+ })
+
+ tpl = template.Must(tpl.Parse(mustRead(name)))
tpl = template.Must(tpl.New("base.html").Parse(mustRead("base.html")))
return tpl
@@ -41,7 +49,7 @@ func mustParseTpl(name string) *template.Template {
func (a *api) renderIndexHandler() http.Handler {
- tpl := mustParseTpl("index.html")
+ tpl := a.mustParseTpl("index.html")
const pageCount = 10
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
@@ -95,7 +103,7 @@ func (a *api) renderIndexHandler() http.Handler {
func (a *api) renderPostHandler() http.Handler {
- tpl := mustParseTpl("post.html")
+ tpl := a.mustParseTpl("post.html")
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
diff --git a/srv/src/api/tpl/base.html b/srv/src/api/tpl/base.html
index bf81032..6031919 100644
--- a/srv/src/api/tpl/base.html
+++ b/srv/src/api/tpl/base.html
@@ -18,7 +18,7 @@
<div class="row">
<div class="seven columns" style="margin-bottom: 3rem;">
<h1 class="title">
- <a href="/">Mediocre Blog</a>
+ <a href="{{ BlogURL "/" }}">Mediocre Blog</a>
</h1>
<div class="light social">
<span>By Brian Picciano</span>
@@ -31,13 +31,14 @@
<div class="five columns light">
<span style="display:block; margin-bottom:0.5rem;">Get notified when new posts are published!</span>
- <a href="/follow.html">
+ <a href="{{ BlogURL "follow.html" }}">
<button class="button-primary">
<i class="far fa-envelope"></i>
Follow
</button>
</a>
- <a href="/feed.xml">
+
+ <a href="{{ BlogURL "feed.xml" }}">
<button class="button">
<i class="fas fa-rss"></i>
RSS