From 43d8951296ce2f232ca94f0577e2e726291bf783 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sun, 26 May 2024 21:16:02 +0200 Subject: Replace URL building methods with the URLBuilder --- src/http/assets.go | 8 +++++-- src/http/drafts.go | 4 +++- src/http/feed.go | 4 ++-- src/http/http.go | 7 ++++++ src/http/posts.go | 19 ++++++++-------- src/http/tpl.go | 64 +----------------------------------------------------- 6 files changed, 28 insertions(+), 78 deletions(-) (limited to 'src/http') 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, -- cgit v1.2.3