summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2024-05-26 21:16:02 +0200
committerBrian Picciano <mediocregopher@gmail.com>2024-05-26 21:16:02 +0200
commit43d8951296ce2f232ca94f0577e2e726291bf783 (patch)
treea04793b1e15e10040201dfafd1c25c649030f72c
parenta6342a3748beaee1ec3c2dc662af6deec0c3959d (diff)
Replace URL building methods with the URLBuilder
-rw-r--r--src/cmd/load-test-data/test-data.yml20
-rw-r--r--src/gmi/gmi.go11
-rw-r--r--src/gmi/tpl.go33
-rw-r--r--src/http/assets.go8
-rw-r--r--src/http/drafts.go4
-rw-r--r--src/http/feed.go4
-rw-r--r--src/http/http.go7
-rw-r--r--src/http/posts.go19
-rw-r--r--src/http/tpl.go64
-rw-r--r--src/render/methods.go16
10 files changed, 56 insertions, 130 deletions
diff --git a/src/cmd/load-test-data/test-data.yml b/src/cmd/load-test-data/test-data.yml
index 40cfa6e..6987d7d 100644
--- a/src/cmd/load-test-data/test-data.yml
+++ b/src/cmd/load-test-data/test-data.yml
@@ -60,7 +60,7 @@ published_posts:
Here's a real picture of cyberspace.
- {{ Image "galaxy.jpg" }}
+ {{ .Image "galaxy.jpg" }}
This has been a great post.
@@ -119,7 +119,7 @@ published_posts:
Here's a real picture of cyberspace.
- {{ Image "galaxy.jpg" "Definitely not a sound stage" }}
+ {{ .Image "galaxy.jpg" "Definitely not a sound stage" }}
This has been a great post.
@@ -173,10 +173,10 @@ published_posts:
There should be 4 images.
- {{ Image "archive.tgz/1.jpg" }}
- {{ Image "archive.tgz/2.jpg" }}
- {{ Image "archive.tgz/foo/3.jpg" }}
- {{ Image "archive.tgz/foo/4.jpg" }}
+ {{ .Image "archive.tgz/1.jpg" }}
+ {{ .Image "archive.tgz/2.jpg" }}
+ {{ .Image "archive.tgz/foo/3.jpg" }}
+ {{ .Image "archive.tgz/foo/4.jpg" }}
- id: gemtext-archive-test
title: Gemtext Archive Test
@@ -188,10 +188,10 @@ published_posts:
There should be 4 images.
- {{ Image "archive.tgz/1.jpg" "First" }}
- {{ Image "archive.tgz/2.jpg" "Second" }}
- {{ Image "archive.tgz/foo/3.jpg" "Third" }}
- {{ Image "archive.tgz/foo/4.jpg" "Fourth" }}
+ {{ .Image "archive.tgz/1.jpg" "First" }}
+ {{ .Image "archive.tgz/2.jpg" "Second" }}
+ {{ .Image "archive.tgz/foo/3.jpg" "Third" }}
+ {{ .Image "archive.tgz/foo/4.jpg" "Fourth" }}
assets:
galaxy.jpg: ./galaxy.jpg
diff --git a/src/gmi/gmi.go b/src/gmi/gmi.go
index 89c35bc..cc4ef71 100644
--- a/src/gmi/gmi.go
+++ b/src/gmi/gmi.go
@@ -18,6 +18,7 @@ import (
"dev.mediocregopher.com/mediocre-blog.git/src/cfg"
"dev.mediocregopher.com/mediocre-blog.git/src/post"
"dev.mediocregopher.com/mediocre-blog.git/src/post/asset"
+ "dev.mediocregopher.com/mediocre-blog.git/src/render"
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
"git.sr.ht/~adnano/go-gemini"
@@ -84,8 +85,9 @@ type API interface {
}
type api struct {
- params Params
- srv *gemini.Server
+ params Params
+ srv *gemini.Server
+ urlBuilder render.URLBuilder
}
// New initializes and returns a new API instance, including setting up all
@@ -102,6 +104,11 @@ func New(params Params) (API, error) {
a := &api{
params: params,
+ urlBuilder: render.NewURLBuilder(
+ params.PublicURL,
+ params.HTTPPublicURL,
+ params.PublicURL, // geminiURL
+ ),
}
handler, err := a.handler()
diff --git a/src/gmi/tpl.go b/src/gmi/tpl.go
index d4c6b00..fb62a17 100644
--- a/src/gmi/tpl.go
+++ b/src/gmi/tpl.go
@@ -9,9 +9,7 @@ import (
"io"
"io/fs"
"mime"
- "net/url"
"path"
- "path/filepath"
"strings"
"text/template"
@@ -36,26 +34,6 @@ var tplFS embed.FS
func (a *api) tplHandler() (gemini.Handler, error) {
- blogURL := func(base *url.URL, path string, abs bool) string {
-
- // filepath.Join strips trailing slash, but we want to keep it
- trailingSlash := strings.HasSuffix(path, "/")
-
- path = filepath.Join("/", base.Path, path)
-
- if trailingSlash && path != "/" {
- path += "/"
- }
-
- if !abs {
- return path
- }
-
- u := *base
- u.Path = path
- return u.String()
- }
-
preprocessFuncs := post.PreprocessFunctions{
Image: func(args ...string) (string, error) {
var (
@@ -67,10 +45,9 @@ func (a *api) tplHandler() (gemini.Handler, error) {
descr = args[1]
}
- path := filepath.Join("assets", id)
- path = blogURL(a.params.PublicURL, path, false)
-
- return fmt.Sprintf("\n=> %s %s", path, descr), nil
+ return fmt.Sprintf(
+ "\n=> %s %s", a.urlBuilder.Asset(id), descr,
+ ), nil
},
}
@@ -140,9 +117,7 @@ func (a *api) tplHandler() (gemini.Handler, error) {
err := tpl.Execute(buf, render.NewMethods(
ctx,
r.URL,
- a.params.PublicURL,
- a.params.HTTPPublicURL,
- a.params.PublicURL, // geminiURL
+ a.urlBuilder,
a.params.HTTPGeminiGatewayURL,
a.params.PostStore,
nil, // asset.Store, not supported by gemini endpoint
diff --git a/src/http/assets.go b/src/http/assets.go
index 8c5ac7e..8f43074 100644
--- a/src/http/assets.go
+++ b/src/http/assets.go
@@ -86,7 +86,9 @@ func (a *api) postPostAssetHandler() http.Handler {
return
}
- a.executeRedirectTpl(rw, r, a.manageAssetsURL(false))
+ a.executeRedirectTpl(
+ rw, r, a.urlBuilder.Assets().MethodManage().String(),
+ )
})
}
@@ -113,6 +115,8 @@ func (a *api) deletePostAssetHandler() http.Handler {
return
}
- a.executeRedirectTpl(rw, r, a.manageAssetsURL(false))
+ a.executeRedirectTpl(
+ rw, r, a.urlBuilder.Assets().MethodManage().String(),
+ )
})
}
diff --git a/src/http/drafts.go b/src/http/drafts.go
index 8f17eec..a20464b 100644
--- a/src/http/drafts.go
+++ b/src/http/drafts.go
@@ -30,6 +30,8 @@ func (a *api) postDraftPostHandler() http.Handler {
return
}
- a.executeRedirectTpl(rw, r, a.editDraftPostURL(p.ID, false))
+ a.executeRedirectTpl(
+ rw, r, a.urlBuilder.Draft(p.ID).MethodEdit().String(),
+ )
})
}
diff --git a/src/http/feed.go b/src/http/feed.go
index eb72464..676d376 100644
--- a/src/http/feed.go
+++ b/src/http/feed.go
@@ -4,9 +4,9 @@ import (
"fmt"
"net/http"
- "github.com/gorilla/feeds"
"dev.mediocregopher.com/mediocre-blog.git/src/http/apiutil"
"dev.mediocregopher.com/mediocre-blog.git/src/post"
+ "github.com/gorilla/feeds"
)
func (a *api) renderFeedHandler() http.Handler {
@@ -53,7 +53,7 @@ func (a *api) renderFeedHandler() http.Handler {
feed.Updated = post.LastUpdatedAt
}
- postURL := a.postURL(post.ID, true)
+ postURL := a.urlBuilder.Post(post.ID).Absolute().String()
item := &feeds.Item{
Title: post.Title,
diff --git a/src/http/http.go b/src/http/http.go
index 6458416..6e57a77 100644
--- a/src/http/http.go
+++ b/src/http/http.go
@@ -20,6 +20,7 @@ import (
"dev.mediocregopher.com/mediocre-blog.git/src/http/apiutil"
"dev.mediocregopher.com/mediocre-blog.git/src/post"
"dev.mediocregopher.com/mediocre-blog.git/src/post/asset"
+ "dev.mediocregopher.com/mediocre-blog.git/src/render"
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
)
@@ -127,6 +128,7 @@ type api struct {
redirectTpl *template.Template
auther Auther
+ urlBuilder render.URLBuilder
}
// New initializes and returns a new API instance, including setting up all
@@ -147,6 +149,11 @@ func New(params Params) (API, error) {
a := &api{
params: params,
auther: NewAuther(params.AuthUsers, params.AuthRatelimit),
+ urlBuilder: render.NewURLBuilder(
+ params.PublicURL,
+ params.PublicURL, // httpURL
+ params.GeminiPublicURL,
+ ),
}
a.redirectTpl = mustParseTpl(template.New(""), "redirect.html")
diff --git a/src/http/posts.go b/src/http/posts.go
index daaaafc..8488996 100644
--- a/src/http/posts.go
+++ b/src/http/posts.go
@@ -30,13 +30,6 @@ func (a *api) postPreprocessFuncImage(args ...string) (string, error) {
}
tpl := txttpl.New("image.html")
-
- tpl.Funcs(txttpl.FuncMap{
- "AssetURL": func(id string) string {
- return a.assetURL(id, false)
- },
- })
-
tpl = txttpl.Must(tpl.Parse(mustReadTplFile("image.html")))
tplPayload := struct {
@@ -189,7 +182,9 @@ func (a *api) postPostHandler() http.Handler {
return
}
- a.executeRedirectTpl(rw, r, a.editPostURL(p.ID, false))
+ a.executeRedirectTpl(
+ rw, r, a.urlBuilder.Post(p.ID).MethodEdit().String(),
+ )
})
}
@@ -223,9 +218,13 @@ func (a *api) deletePostHandler(isDraft bool) http.Handler {
}
if isDraft {
- a.executeRedirectTpl(rw, r, a.manageDraftPostsURL(false))
+ a.executeRedirectTpl(
+ rw, r, a.urlBuilder.Drafts().MethodManage().String(),
+ )
} else {
- a.executeRedirectTpl(rw, r, a.managePostsURL(false))
+ a.executeRedirectTpl(
+ rw, r, a.urlBuilder.Posts().MethodManage().String(),
+ )
}
})
}
diff --git a/src/http/tpl.go b/src/http/tpl.go
index 57d7cfd..47cea80 100644
--- a/src/http/tpl.go
+++ b/src/http/tpl.go
@@ -9,9 +9,7 @@ import (
"io"
"io/fs"
"net/http"
- "net/url"
"path/filepath"
- "strings"
"dev.mediocregopher.com/mediocre-blog.git/src/http/apiutil"
"dev.mediocregopher.com/mediocre-blog.git/src/post"
@@ -32,64 +30,6 @@ func mustReadTplFile(fileName string) string {
return string(b)
}
-func (a *api) blogURL(base *url.URL, path string, abs bool) string {
- // filepath.Join strips trailing slash, but we want to keep it
- trailingSlash := strings.HasSuffix(path, "/")
-
- path = filepath.Join("/", base.Path, path)
-
- if trailingSlash && path != "/" {
- path += "/"
- }
-
- if !abs {
- return path
- }
-
- u := *base
- u.Path = path
- return u.String()
-}
-
-func (a *api) postURL(id string, abs bool) string {
- path := filepath.Join("posts", id)
- return a.blogURL(a.params.PublicURL, path, abs)
-}
-
-func (a *api) editPostURL(id string, abs bool) string {
- return a.postURL(id, abs) + "?method=edit"
-}
-
-func (a *api) managePostsURL(abs bool) string {
- return a.blogURL(a.params.PublicURL, "posts?method=manage", abs)
-}
-
-func (a *api) manageAssetsURL(abs bool) string {
- return a.blogURL(a.params.PublicURL, "assets?method=manage", abs)
-}
-
-func (a *api) assetURL(id string, abs bool) string {
- path := filepath.Join("assets", id)
- return a.blogURL(a.params.PublicURL, path, false)
-}
-
-func (a *api) draftPostURL(id string, abs bool) string {
- path := filepath.Join("drafts", id)
- return a.blogURL(a.params.PublicURL, path, abs)
-}
-
-func (a *api) editDraftPostURL(id string, abs bool) string {
- return a.draftPostURL(id, abs) + "?method=edit"
-}
-
-func (a *api) manageDraftPostsURL(abs bool) string {
- return a.blogURL(a.params.PublicURL, "drafts", abs) + "?method=manage"
-}
-
-func (a *api) draftsURL(abs bool) string {
- return a.blogURL(a.params.PublicURL, "drafts", abs)
-}
-
func mustParseTpl(tpl *template.Template, name string) *template.Template {
return template.Must(tpl.New(name).Parse(mustReadTplFile(name)))
}
@@ -121,9 +61,7 @@ func (a *api) newTPLData(r *http.Request, payload interface{}) tplData {
Methods: render.NewMethods(
r.Context(),
r.URL,
- a.params.PublicURL,
- a.params.PublicURL, // httpURL
- a.params.GeminiPublicURL,
+ a.urlBuilder,
a.params.GeminiGatewayURL,
a.params.PostStore,
a.params.PostAssetStore,
diff --git a/src/render/methods.go b/src/render/methods.go
index 2b8675b..a671f7e 100644
--- a/src/render/methods.go
+++ b/src/render/methods.go
@@ -57,9 +57,7 @@ type GetPostSeriesNextPreviousRes struct {
type Methods struct {
ctx context.Context
url *url.URL
- publicURL *url.URL
- httpURL *url.URL
- geminiURL *url.URL
+ urlBuilder URLBuilder
geminiGatewayURL *url.URL
postStore post.Store
postAssetStore asset.Store
@@ -74,9 +72,7 @@ type Methods struct {
func NewMethods(
ctx context.Context,
url *url.URL,
- publicURL *url.URL,
- httpURL *url.URL,
- geminiURL *url.URL,
+ urlBuilder URLBuilder,
geminiGatewayURL *url.URL,
postStore post.Store,
postAssetStore asset.Store,
@@ -86,9 +82,7 @@ func NewMethods(
return &Methods{
ctx,
url,
- publicURL,
- httpURL,
- geminiURL,
+ urlBuilder,
geminiGatewayURL,
postStore,
postAssetStore,
@@ -100,7 +94,7 @@ func NewMethods(
}
func (m *Methods) RootURL() URLBuilder {
- return NewURLBuilder(m.publicURL, m.httpURL, m.geminiURL)
+ return m.urlBuilder
}
func (m *Methods) GetTags() ([]string, error) {
@@ -300,7 +294,7 @@ func (m *Methods) GetQueryIntValue(key string, def int) (int, error) {
}
func (m *Methods) GetPath() (string, error) {
- basePath := filepath.Join("/", m.publicURL.Path) // in case it's empty
+ basePath := m.urlBuilder.String()
return filepath.Rel(basePath, m.url.Path)
}