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/gmi/gmi.go | 11 +++++++++-- src/gmi/tpl.go | 33 ++++----------------------------- 2 files changed, 13 insertions(+), 31 deletions(-) (limited to 'src/gmi') 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 -- cgit v1.2.3