summaryrefslogtreecommitdiff
path: root/src/http
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2024-05-18 18:29:19 +0200
committerBrian Picciano <mediocregopher@gmail.com>2024-05-18 18:29:19 +0200
commit0665d0c65974533fbd313f4e0b062b5103057aeb (patch)
tree5287795eed9767bd959a5139b77ef78b4024216d /src/http
parentffa26298c95451639a6e01db6692d02d50b3d518 (diff)
Replace all URL rendering within templates by a URLConstructor
Diffstat (limited to 'src/http')
-rw-r--r--src/http/http.go5
-rw-r--r--src/http/posts.go6
-rw-r--r--src/http/tpl.go2
-rw-r--r--src/http/tpl/admin.html6
-rw-r--r--src/http/tpl/base.html22
-rw-r--r--src/http/tpl/draft-posts-manage.html14
-rw-r--r--src/http/tpl/follow.html2
-rw-r--r--src/http/tpl/gemini-cta.html6
-rw-r--r--src/http/tpl/image.html4
-rw-r--r--src/http/tpl/index.html6
-rw-r--r--src/http/tpl/post-assets-manage.html8
-rw-r--r--src/http/tpl/post-edit.html16
-rw-r--r--src/http/tpl/post.html4
-rw-r--r--src/http/tpl/posts-manage.html14
-rw-r--r--src/http/tpl/posts.html8
15 files changed, 66 insertions, 57 deletions
diff --git a/src/http/http.go b/src/http/http.go
index 9bfed59..93cc043 100644
--- a/src/http/http.go
+++ b/src/http/http.go
@@ -89,7 +89,10 @@ func (p *Params) SetupCfg(cfg *cfg.Cfg) {
return fmt.Errorf("unmarshaling -http-auth-ratelimit: %w", err)
}
- *publicURLStr = strings.TrimSuffix(*publicURLStr, "/")
+ if !strings.HasSuffix(*publicURLStr, "/") {
+ *publicURLStr += "/"
+ }
+
if p.PublicURL, err = url.Parse(*publicURLStr); err != nil {
return fmt.Errorf("parsing -http-public-url: %w", err)
}
diff --git a/src/http/posts.go b/src/http/posts.go
index 14224f2..42e5b4a 100644
--- a/src/http/posts.go
+++ b/src/http/posts.go
@@ -40,10 +40,16 @@ func (a *api) postPreprocessFuncImage(args ...string) (string, error) {
tpl = txttpl.Must(tpl.Parse(mustReadTplFile("image.html")))
tplPayload := struct {
+ RootURL render.URLBuilder
ID string
Descr string
Resizable bool
}{
+ RootURL: render.NewURLBuilder(
+ a.params.PublicURL,
+ a.params.PublicURL, // httpURL
+ a.params.GeminiPublicURL,
+ ),
ID: id,
Descr: descr,
Resizable: asset.IsImageResizable(id),
diff --git a/src/http/tpl.go b/src/http/tpl.go
index 2711259..c4afbdc 100644
--- a/src/http/tpl.go
+++ b/src/http/tpl.go
@@ -135,6 +135,8 @@ func (a *api) newTPLData(r *http.Request, payload interface{}) tplData {
r.Context(),
r.URL,
a.params.PublicURL,
+ a.params.PublicURL, // httpURL
+ a.params.GeminiPublicURL,
a.params.GeminiGatewayURL,
a.params.PostStore,
a.params.PostAssetStore,
diff --git a/src/http/tpl/admin.html b/src/http/tpl/admin.html
index 0b4b4e3..2dfab0f 100644
--- a/src/http/tpl/admin.html
+++ b/src/http/tpl/admin.html
@@ -7,9 +7,9 @@ mostly left open to inspection, but you will not able to change
anything without providing credentials.
<ul>
- <li><a href="{{ BlogURL "posts?method=manage" }}">Posts</a></li>
- <li><a href="{{ BlogURL "assets?method=manage" }}">Assets</a></li>
- <li><a href="{{ BlogURL "drafts?method=manage" }}">Drafts</a> (private)</li>
+ <li><a href="{{ .RootURL.Posts.MethodManage }}">Posts</a></li>
+ <li><a href="{{ .RootURL.Assets.MethodManage }}">Assets</a></li>
+ <li><a href="{{ .RootURL.Drafts.MethodManage }}">Drafts</a> (private)</li>
</ul>
{{ end }}
diff --git a/src/http/tpl/base.html b/src/http/tpl/base.html
index 47644f7..f0419d5 100644
--- a/src/http/tpl/base.html
+++ b/src/http/tpl/base.html
@@ -3,12 +3,12 @@
<head>
<title>{{ .Title }}</title>
- <link rel="stylesheet" type="text/css" href="{{ StaticURL "new.css" }}" />
- <link rel="stylesheet" type="text/css" href="{{ StaticURL "mediocre.css" }}" />
- <link rel="apple-touch-icon" sizes="180x180" href="{{ StaticURL "favicon/apple-touch-icon.png" }}">
- <link rel="icon" type="image/png" sizes="32x32" href="{{ StaticURL "favicon/favicon-32x32.png" }}">
- <link rel="icon" type="image/png" sizes="16x16" href="{{ StaticURL "favicon/favicon-16x16.png" }}">
- <link rel="manifest" href="{{ StaticURL "favicon/site.webmanifest" }}">
+ <link rel="stylesheet" type="text/css" href="{{ .RootURL.Static "new.css" }}" />
+ <link rel="stylesheet" type="text/css" href="{{ .RootURL.Static "mediocre.css" }}" />
+ <link rel="apple-touch-icon" sizes="180x180" href="{{ .RootURL.Static "favicon/apple-touch-icon.png" }}">
+ <link rel="icon" type="image/png" sizes="32x32" href="{{ .RootURL.Static "favicon/favicon-32x32.png" }}">
+ <link rel="icon" type="image/png" sizes="16x16" href="{{ .RootURL.Static "favicon/favicon-16x16.png" }}">
+ <link rel="manifest" href="{{ .RootURL.Static "favicon/site.webmanifest" }}">
</head>
<body>
@@ -105,17 +105,17 @@
<strong>mediocregopher</strong>'s lil web corner
<br/>
<br/>
- <a href="{{ BlogURL "/" }}">Home</a>
+ <a href="{{ .RootURL }}">Home</a>
&nbsp;//&nbsp;
- <a href="{{ BlogURL "/posts" }}">Posts</a>
+ <a href="{{ .RootURL.Posts }}">Posts</a>
&nbsp;/&nbsp;
- <a href="{{ BlogURL "follow" }}">Follow</a>
+ <a href="{{ .RootURL.Path "follow" }}">Follow</a>
&nbsp;/&nbsp;
- <a href="{{ BlogURL "feed.xml" }}">RSS</a>
+ <a href="{{ .RootURL.Path "feed.xml" }}">RSS</a>
&nbsp;//&nbsp;
<a href="https://dev.mediocregopher.com/mediocre-blog.git">Source</a>
&nbsp;/&nbsp;
- <a href="{{ StaticURL "wtfpl.txt" }}">License</a>
+ <a href="{{ .RootURL.Static "wtfpl.txt" }}">License</a>
</header>
{{ template "body" . }}
diff --git a/src/http/tpl/draft-posts-manage.html b/src/http/tpl/draft-posts-manage.html
index b1b4362..417f716 100644
--- a/src/http/tpl/draft-posts-manage.html
+++ b/src/http/tpl/draft-posts-manage.html
@@ -1,6 +1,6 @@
{{ define "body" }}
- {{ $page := .GetQueryIntValue "p" 0 -}}
+ {{ $page := .GetQueryIntValue "page" 0 -}}
{{ $getPostsRes := .GetDraftPosts $page 20 -}}
<script>
@@ -11,18 +11,18 @@
</script>
<p>
- <a href="{{ BlogURL "admin" }}">Back to Admin</a>
+ <a href="{{ .RootURL.Path "admin" }}">Back to Admin</a>
</p>
<h1>Drafts</h1>
<p>
- <a href="{{ BlogURL "drafts" }}?method=edit">New Draft</a>
+ <a href="{{ .RootURL.Drafts.MethodEdit }}">New Draft</a>
</p>
{{ if gt $page 0 }}
<p>
- <a href="?method=manage&p={{ .Add $page -1 }}">&lt; &lt; Previous Page</a>
+ <a href="{{ .RootURL.Drafts.MethodManage.Page (.Add $page -1) }}">&lt; &lt; Previous Page</a>
</p>
{{ end }}
@@ -32,13 +32,13 @@
<tr>
<td>{{ .Title }}</td>
<td>
- <a href="{{ DraftURL .ID }}?method=edit">
+ <a href="{{ ($.RootURL.Draft .ID).MethodEdit }}">
Edit
</a>
</td>
<td>
<form
- action="{{ DraftURL .ID }}?method=delete"
+ action="{{ ($.RootURL.Draft .ID).MethodDelete }}"
method="POST"
>
<input
@@ -55,7 +55,7 @@
{{ if $getPostsRes.HasMore }}
<p>
- <a href="?method=manage&p={{ .Add $page 1 }}">Next Page &gt; &gt;</a>
+ <a href="{{ .RootURL.Drafts.MethodManage.Page (.Add $page 1) }}">Next Page &gt; &gt;</a>
</p>
{{ end }}
diff --git a/src/http/tpl/follow.html b/src/http/tpl/follow.html
index 1958f95..091af49 100644
--- a/src/http/tpl/follow.html
+++ b/src/http/tpl/follow.html
@@ -6,7 +6,7 @@
</p>
<p>
- <a href="{{ BlogHTTPURL "feed.xml" }}">{{ BlogHTTPURL "feed.xml" }}</a>
+ <a href="{{ .RootURL.Absolute.Path "feed.xml" }}">{{ .RootURL.Absolute.Path "feed.xml" }}</a>
</p>
<p>
diff --git a/src/http/tpl/gemini-cta.html b/src/http/tpl/gemini-cta.html
index 89b8e8b..77c1114 100644
--- a/src/http/tpl/gemini-cta.html
+++ b/src/http/tpl/gemini-cta.html
@@ -2,11 +2,9 @@
<p>
This site can also be accessed via the gemini protocol:
- <a href="{{ BlogGeminiURL "/" | .URLIsSafe }}">
- {{ BlogGeminiURL "/" }}
- </a>
+ <a href="{{ .RootURL.Gemini.HTMLSafe }}">{{ .RootURL.Gemini.HTMLSafe }}</a>
</p>
<p>
- <a href="{{ PostURL "gemspace-tour" }}">What is gemini?</a>
+ <a href="{{ .RootURL.Post "gemspace-tour" }}">What is gemini?</a>
</p>
diff --git a/src/http/tpl/image.html b/src/http/tpl/image.html
index c6c19b3..7778625 100644
--- a/src/http/tpl/image.html
+++ b/src/http/tpl/image.html
@@ -1,7 +1,7 @@
<div style="text-align: center;">
- <a href="{{ AssetURL .ID }}" target="_blank">
+ <a href="{{ .RootURL.Asset .ID }}" target="_blank">
<img
- src="{{ AssetURL .ID }}{{ if .Resizable }}?w=800{{ end }}"
+ src="{{ .RootURL.Asset .ID }}{{ if .Resizable }}?w=800{{ end }}"
alt="{{ .Descr }}"
/>
</a>
diff --git a/src/http/tpl/index.html b/src/http/tpl/index.html
index 7f6399a..2685f4f 100644
--- a/src/http/tpl/index.html
+++ b/src/http/tpl/index.html
@@ -2,9 +2,9 @@
<p>
This here's my little corner of the web, where I publish
- <a href="{{ BlogURL "posts" }}">posts</a>
+ <a href="{{ .RootURL.Posts }}">posts</a>
about projects I'm working on and things that interest me (which you can
- <a href="{{ BlogURL "follow" }}">follow</a>,
+ <a href="{{ .RootURL.Path "follow" }}">follow</a>,
if you like).
</p>
@@ -12,7 +12,7 @@
<p>Feel free to hmu over email or Signal if you'd like to get in touch.</p>
<ul>
<li>Email: <a href="mailto:me@mediocregopher.com">me@mediocregopher.com</a></li>
- <li><a href="{{ StaticURL "me@mediocregopher.com.gpg" }}">GPG Key</a></li>
+ <li><a href="{{ .RootURL.Static "me@mediocregopher.com.gpg" }}">GPG Key</a></li>
<li>Signal:
<a href="https://signal.me/#eu/x5psueq2E3WfFwwEgFDoWeSZx9k4u8vFBaiMSa4Lo0cvluHkb-dIpJp1wfdJsdie">
mediocregopher.01
diff --git a/src/http/tpl/post-assets-manage.html b/src/http/tpl/post-assets-manage.html
index a0b0da0..7e01ff4 100644
--- a/src/http/tpl/post-assets-manage.html
+++ b/src/http/tpl/post-assets-manage.html
@@ -3,7 +3,7 @@
{{ $assetIDs := .GetPostAssetIDs }}
<p>
- <a href="{{ BlogURL "admin" }}">Back to Admin</a>
+ <a href="{{ .RootURL.Path "admin" }}">Back to Admin</a>
</p>
<h1>Assets</h1>
@@ -15,7 +15,7 @@
overwritten.
</p>
-<form action="{{ BlogURL "assets/" }}" method="POST" enctype="multipart/form-data">
+<form action="{{ .RootURL.Assets }}" method="POST" enctype="multipart/form-data">
<div class="row">
<div class="four columns">
<input type="text" placeholder="Unique ID" name="id" />
@@ -37,10 +37,10 @@
{{ range $assetIDs }}
<tr>
- <td><a href="{{ AssetURL . }}">{{ . }}</a></td>
+ <td><a href="{{ $.RootURL.Asset . }}">{{ . }}</a></td>
<td>
<form
- action="{{ AssetURL . }}?method=delete"
+ action="{{ ($.RootURL.Asset .).MethodDelete }}"
method="POST"
style="margin-bottom: 0;"
>
diff --git a/src/http/tpl/post-edit.html b/src/http/tpl/post-edit.html
index 28dcd0e..1c40a3a 100644
--- a/src/http/tpl/post-edit.html
+++ b/src/http/tpl/post-edit.html
@@ -6,17 +6,17 @@
<p>
{{ if .Payload.IsDraft }}
- <a href="{{ BlogURL "drafts?method=manage" }}">
+ <a href="{{ .RootURL.Drafts.MethodManage }}">
Back to Drafts
</a>
{{ else }}
- <a href="{{ BlogURL "posts?method=manage" }}">
+ <a href="{{ .RootURL.Posts.MethodManage }}">
Back to Posts
</a>
{{ end }}
</p>
-<form method="POST" action="{{ BlogURL "posts/" }}">
+<form method="POST" action="{{ .RootURL.Posts }}">
<table>
@@ -36,7 +36,7 @@
{{ $post.ID }}
<input name="id" type="hidden" value="{{ $post.ID }}" />
{{ else }}
- <a href="{{ PostURL $post.ID }}">{{ $post.ID }}</a>
+ <a href="{{ .RootURL.Post $post.ID }}">{{ $post.ID }}</a>
<input name="id" type="hidden" value="{{ $post.ID }}" />
{{ end }}
</td>
@@ -135,12 +135,12 @@
<input
type="submit"
value="Preview"
- formaction="{{ BlogURL "posts/" }}{{ $post.ID }}?method=preview"
+ formaction="{{ (.RootURL.Post $post.ID).MethodPreview }}"
formtarget="_blank"
/>
{{ if .Payload.IsDraft }}
- <input type="submit" value="Save" formaction="{{ BlogURL "drafts/" }}" />
+ <input type="submit" value="Save" formaction="{{ .RootURL.Drafts }}" />
<script>
@@ -154,12 +154,12 @@
<input
type="submit"
value="Publish"
- formaction="{{ BlogURL "posts/" }}"
+ formaction="{{ .RootURL.Posts }}"
onclick="confirmPublish(event)"
/>
{{ else }}
- <input type="submit" value="Update" formaction="{{ BlogURL "posts/" }}" />
+ <input type="submit" value="Update" formaction="{{ .RootURL.Posts }}" />
{{ end }}
</p>
diff --git a/src/http/tpl/post.html b/src/http/tpl/post.html
index 46a24fc..1bda8c9 100644
--- a/src/http/tpl/post.html
+++ b/src/http/tpl/post.html
@@ -27,7 +27,7 @@
This post is part of a series.<br/>
{{ if $seriesNextPrev.Next }}
- Next: <a href="{{ PostURL $seriesNextPrev.Next.ID }}">{{ $seriesNextPrev.Next.Title }}</a>
+ Next: <a href="{{ .RootURL.Post $seriesNextPrev.Next.ID }}">{{ $seriesNextPrev.Next.Title }}</a>
{{ end }}
{{ if and $seriesNextPrev.Next $seriesNextPrev.Previous }}
@@ -35,7 +35,7 @@
{{ end }}
{{ if $seriesNextPrev.Previous }}
- Previously: <a href="{{ PostURL $seriesNextPrev.Previous.ID }}">{{ $seriesNextPrev.Previous.Title }}</a>
+ Previously: <a href="{{ .RootURL.Post $seriesNextPrev.Previous.ID }}">{{ $seriesNextPrev.Previous.Title }}</a>
<br/>
{{ end }}
diff --git a/src/http/tpl/posts-manage.html b/src/http/tpl/posts-manage.html
index f9adbc1..ad9c9d7 100644
--- a/src/http/tpl/posts-manage.html
+++ b/src/http/tpl/posts-manage.html
@@ -1,6 +1,6 @@
{{ define "body" }}
- {{ $page := .GetQueryIntValue "p" 0 -}}
+ {{ $page := .GetQueryIntValue "page" 0 -}}
{{ $getPostsRes := .GetPosts $page 20 -}}
<script>
@@ -11,14 +11,14 @@
</script>
<p>
- <a href="{{ BlogURL "admin" }}">Back to Admin</a>
+ <a href="{{ .RootURL.Path "admin" }}">Back to Admin</a>
</p>
<h1>Posts</h1>
{{ if gt $page 0 }}
<p>
- <a href="?method=manage&p={{ .Add $page -1 }}">&lt; &lt; Previous Page</a>
+ <a href="{{ .RootURL.Posts.MethodManage.Page (.Add $page -1) }}">&lt; &lt; Previous Page</a>
</p>
{{ end }}
@@ -33,15 +33,15 @@
{{ range $getPostsRes.Posts }}
<tr>
<td>{{ .PublishedAt.Local.Format "2006-01-02 15:04:05 MST" }}</td>
- <td><a href="{{ PostURL .ID }}">{{ .Title }}</a></td>
+ <td><a href="{{ $.RootURL.Post .ID }}">{{ .Title }}</a></td>
<td>
- <a href="{{ PostURL .ID }}?method=edit">
+ <a href="{{ ($.RootURL.Post .ID).MethodEdit }}">
Edit
</a>
</td>
<td>
<form
- action="{{ PostURL .ID }}?method=delete"
+ action="{{ ($.RootURL.Post .ID).MethodDelete }}"
method="POST"
>
<input
@@ -58,7 +58,7 @@
{{ if $getPostsRes.HasMore }}
<p>
- <a href="?method=manage&p={{ .Add $page 1 }}">Next Page &gt; &gt;</a>
+ <a href="{{ .RootURL.Posts.MethodManage.Page (.Add $page 1) }}">Next Page &gt; &gt;</a>
</p>
{{ end }}
diff --git a/src/http/tpl/posts.html b/src/http/tpl/posts.html
index 2fa336d..0df166a 100644
--- a/src/http/tpl/posts.html
+++ b/src/http/tpl/posts.html
@@ -1,11 +1,11 @@
{{ define "body" }}
- {{ $page := .GetQueryIntValue "p" 0 -}}
+ {{ $page := .GetQueryIntValue "page" 0 -}}
{{ $getPostsRes := .GetPosts $page 20 -}}
{{ if gt $page 0 }}
<p>
- <a href="?p={{ .Add $page -1 }}">&lt; &lt; Previous Page</a>
+ <a href="{{ .RootURL.Posts.Page (.Add $page -1) }}">&lt; &lt; Previous Page</a>
</p>
{{ else }}
<p>
@@ -17,7 +17,7 @@
<ul>
{{ range $getPostsRes.Posts }}
<li>
- <strong><a href="{{ PostURL .ID }}">
+ <strong><a href="{{ $.RootURL.Post .ID }}">
{{ .PublishedAt.Format "2006-01-02" }} - {{ .Title }}
</a></strong>
{{ if .Description }}
@@ -29,7 +29,7 @@
{{ if $getPostsRes.HasMore }}
<p>
- <a href="?p={{ .Add $page 1 }}">Next Page &gt; &gt;</a>
+ <a href="{{ .RootURL.Posts.Page (.Add $page 1) }}">Next Page &gt; &gt;</a>
</p>
{{ end }}