From 93a8843e2e3391459fd333aef9e5c7617608c2b3 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 18 May 2024 15:55:59 +0200 Subject: Remove most rendering related querying from go http handlers --- src/gmi/tpl.go | 2 + src/http/assets.go | 20 +---- src/http/drafts.go | 40 +-------- src/http/posts.go | 167 ++--------------------------------- src/http/tpl.go | 2 + src/http/tpl/draft-posts-manage.html | 26 ++++-- src/http/tpl/post-assets-manage.html | 6 +- src/http/tpl/post-edit.html | 34 +++---- src/http/tpl/posts-manage.html | 14 +-- src/http/tpl/posts.html | 15 ++-- src/render/methods.go | 51 +++++++++-- 11 files changed, 121 insertions(+), 256 deletions(-) diff --git a/src/gmi/tpl.go b/src/gmi/tpl.go index 8ffa6bc..f6c1754 100644 --- a/src/gmi/tpl.go +++ b/src/gmi/tpl.go @@ -177,6 +177,8 @@ func (a *api) tplHandler() (gemini.Handler, error) { a.params.PublicURL, a.params.HTTPGeminiGatewayURL, a.params.PostStore, + nil, // asset.Store, not supported by gemini endpoint + nil, // post.DraftStore, not supported by gemini endpoint preprocessFuncs, )) diff --git a/src/http/assets.go b/src/http/assets.go index 5a47152..8c5ac7e 100644 --- a/src/http/assets.go +++ b/src/http/assets.go @@ -14,27 +14,9 @@ import ( ) func (a *api) managePostAssetsHandler() http.Handler { - tpl := a.mustParseBasedTpl("post-assets-manage.html") - return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - - ids, err := a.params.PostAssetStore.List() - - if err != nil { - apiutil.InternalServerError( - rw, r, fmt.Errorf("getting list of asset ids: %w", err), - ) - return - } - - tplPayload := struct { - IDs []string - }{ - IDs: ids, - } - - a.executeTemplate(rw, r, tpl, tplPayload) + a.executeTemplate(rw, r, tpl, nil) }) } diff --git a/src/http/drafts.go b/src/http/drafts.go index b0550ce..8f17eec 100644 --- a/src/http/drafts.go +++ b/src/http/drafts.go @@ -5,50 +5,12 @@ import ( "net/http" "dev.mediocregopher.com/mediocre-blog.git/src/http/apiutil" - "dev.mediocregopher.com/mediocre-blog.git/src/post" ) func (a *api) manageDraftPostsHandler() http.Handler { - tpl := a.mustParseBasedTpl("draft-posts-manage.html") - const pageCount = 20 - return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - - page, err := apiutil.StrToInt(r.FormValue("p"), 0) - if err != nil { - apiutil.BadRequest( - rw, r, fmt.Errorf("invalid page number: %w", err), - ) - return - } - - posts, hasMore, err := a.params.PostDraftStore.Get(page, pageCount) - if err != nil { - apiutil.InternalServerError( - rw, r, fmt.Errorf("fetching page %d of posts: %w", page, err), - ) - return - } - - tplPayload := struct { - Posts []post.Post - PrevPage, NextPage int - }{ - Posts: posts, - PrevPage: -1, - NextPage: -1, - } - - if page > 0 { - tplPayload.PrevPage = page - 1 - } - - if hasMore { - tplPayload.NextPage = page + 1 - } - - a.executeTemplate(rw, r, tpl, tplPayload) + a.executeTemplate(rw, r, tpl, nil) }) } diff --git a/src/http/posts.go b/src/http/posts.go index 939b811..14224f2 100644 --- a/src/http/posts.go +++ b/src/http/posts.go @@ -5,7 +5,6 @@ import ( "context" "errors" "fmt" - "html/template" "net/http" "path/filepath" "strings" @@ -58,12 +57,6 @@ func (a *api) postPreprocessFuncImage(args ...string) (string, error) { return buf.String(), nil } -type postTplPayload struct { - post.StoredPost - SeriesPrevious, SeriesNext *post.StoredPost - Body template.HTML -} - func (a *api) postPreprocessFuncs() post.PreprocessFunctions { return post.PreprocessFunctions{ BlogURL: func(path string) string { @@ -90,76 +83,17 @@ func (a *api) postPreprocessFuncs() post.PreprocessFunctions { } func (a *api) getPostsHandler() http.Handler { - - tpl := a.mustParseBasedTpl("posts.html") - getPostHandler := a.getPostHandler() - const pageCount = 20 + var ( + tpl = a.mustParseBasedTpl("posts.html") + getPostHandler = a.getPostHandler() + ) return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - - id := filepath.Base(r.URL.Path) - - if id != "/" { + if id := filepath.Base(r.URL.Path); id != "/" { getPostHandler.ServeHTTP(rw, r) return } - - page, err := apiutil.StrToInt(r.FormValue("p"), 0) - if err != nil { - apiutil.BadRequest( - rw, r, fmt.Errorf("invalid page number: %w", err), - ) - return - } - - tag := r.FormValue("tag") - - var ( - posts []post.StoredPost - hasMore bool - ) - - if tag == "" { - posts, hasMore, err = a.params.PostStore.Get(page, pageCount) - } else { - posts, err = a.params.PostStore.GetByTag(tag) - } - - if err != nil { - apiutil.InternalServerError( - rw, r, fmt.Errorf("fetching page %d of posts: %w", page, err), - ) - return - } - - tags, err := a.params.PostStore.GetTags() - if err != nil { - apiutil.InternalServerError( - rw, r, fmt.Errorf("fething tags: %w", err), - ) - return - } - - tplPayload := struct { - Posts []post.StoredPost - PrevPage, NextPage int - Tags []string - }{ - Posts: posts, - PrevPage: -1, - NextPage: -1, - Tags: tags, - } - - if page > 0 { - tplPayload.PrevPage = page - 1 - } - - if hasMore { - tplPayload.NextPage = page + 1 - } - - a.executeTemplate(rw, r, tpl, tplPayload) + a.executeTemplate(rw, r, tpl, nil) }) } @@ -173,105 +107,22 @@ func (a *api) getPostHandler() http.Handler { } func (a *api) managePostsHandler() http.Handler { - tpl := a.mustParseBasedTpl("posts-manage.html") - const pageCount = 20 - return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - - page, err := apiutil.StrToInt(r.FormValue("p"), 0) - if err != nil { - apiutil.BadRequest( - rw, r, fmt.Errorf("invalid page number: %w", err), - ) - return - } - - posts, hasMore, err := a.params.PostStore.Get(page, pageCount) - if err != nil { - apiutil.InternalServerError( - rw, r, fmt.Errorf("fetching page %d of posts: %w", page, err), - ) - return - } - - tplPayload := struct { - Posts []post.StoredPost - PrevPage, NextPage int - }{ - Posts: posts, - PrevPage: -1, - NextPage: -1, - } - - if page > 0 { - tplPayload.PrevPage = page - 1 - } - - if hasMore { - tplPayload.NextPage = page + 1 - } - - a.executeTemplate(rw, r, tpl, tplPayload) + a.executeTemplate(rw, r, tpl, nil) }) } func (a *api) editPostHandler(isDraft bool) http.Handler { - tpl := a.mustParseBasedTpl("post-edit.html") - return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - - id := filepath.Base(r.URL.Path) - - if id == "/" && !isDraft { - http.Error(rw, "Post id required", 400) - return - } - - var ( - storedPost post.StoredPost - err error - ) - - if id != "/" { - - if isDraft { - storedPost.Post, err = a.params.PostDraftStore.GetByID(id) - } else { - storedPost, err = a.params.PostStore.GetByID(id) - } - - if errors.Is(err, post.ErrPostNotFound) { - http.Error(rw, "Post not found", 404) - return - } else if err != nil { - apiutil.InternalServerError( - rw, r, fmt.Errorf("fetching post with id %q: %w", id, err), - ) - return - } - } - - tags, err := a.params.PostStore.GetTags() - if err != nil { - apiutil.InternalServerError(rw, r, fmt.Errorf("fetching tags: %w", err)) - return - } - - tplPayload := struct { - Post post.StoredPost - Tags []string + a.executeTemplate(rw, r, tpl, struct { IsDraft bool Formats []post.Format }{ - Post: storedPost, - Tags: tags, IsDraft: isDraft, Formats: post.Formats, - } - - a.executeTemplate(rw, r, tpl, tplPayload) + }) }) } diff --git a/src/http/tpl.go b/src/http/tpl.go index c623f2e..afd4c8e 100644 --- a/src/http/tpl.go +++ b/src/http/tpl.go @@ -151,6 +151,8 @@ func (a *api) newTPLData(r *http.Request, payload interface{}) tplData { a.params.PublicURL, a.params.GeminiGatewayURL, a.params.PostStore, + a.params.PostAssetStore, + a.params.PostDraftStore, a.postPreprocessFuncs(), ), Payload: payload, diff --git a/src/http/tpl/draft-posts-manage.html b/src/http/tpl/draft-posts-manage.html index 5454f24..b1b4362 100644 --- a/src/http/tpl/draft-posts-manage.html +++ b/src/http/tpl/draft-posts-manage.html @@ -1,5 +1,15 @@ {{ define "body" }} + {{ $page := .GetQueryIntValue "p" 0 -}} + {{ $getPostsRes := .GetDraftPosts $page 20 -}} + + +

Back to Admin

@@ -10,15 +20,15 @@ New Draft

- {{ if ge .Payload.PrevPage 0 }} + {{ if gt $page 0 }}

- < < Previous Page + < < Previous Page

{{ end }} - {{ range .Payload.Posts }} + {{ range $getPostsRes.Posts }} @@ -39,9 +53,9 @@
{{ .Title }} @@ -31,7 +41,11 @@ action="{{ DraftURL .ID }}?method=delete" method="POST" > - +
- {{ if ge .Payload.NextPage 0 }} + {{ if $getPostsRes.HasMore }}

- Next Page > > + Next Page > >

{{ end }} diff --git a/src/http/tpl/post-assets-manage.html b/src/http/tpl/post-assets-manage.html index f21717a..a0b0da0 100644 --- a/src/http/tpl/post-assets-manage.html +++ b/src/http/tpl/post-assets-manage.html @@ -1,5 +1,7 @@ {{ define "body" }} +{{ $assetIDs := .GetPostAssetIDs }} +

Back to Admin

@@ -27,13 +29,13 @@ -{{ if gt (len .Payload.IDs) 0 }} +{{ if $assetIDs }}

Existing Assets

- {{ range .Payload.IDs }} + {{ range $assetIDs }} @@ -45,15 +49,15 @@ name="tags" type="text" required - value="{{- range $i, $tag := .Payload.Post.Tags -}} + value="{{- range $i, $tag := $post.Tags -}} {{- if ne $i 0 }} {{ end }}{{ $tag -}} {{- end -}} "/> - {{ if gt (len .Payload.Tags) 0 }} + {{ if $tags }} Existing tags: - {{ range $i, $tag := .Payload.Tags }} + {{ range $i, $tag := $tags }} {{ if ne $i 0 }} {{ end }}{{ $tag }} {{ end }} @@ -67,7 +71,7 @@ + value="{{ $post.Series }}" /> @@ -78,7 +82,7 @@ name="title" type="text" required - value="{{ .Payload.Post.Title }}" /> + value="{{ $post.Title }}" /> @@ -88,7 +92,7 @@ + value="{{ $post.Description }}" /> @@ -98,7 +102,7 @@ diff --git a/src/http/tpl/posts-manage.html b/src/http/tpl/posts-manage.html index 96b30ca..f9adbc1 100644 --- a/src/http/tpl/posts-manage.html +++ b/src/http/tpl/posts-manage.html @@ -1,4 +1,8 @@ {{ define "body" }} + + {{ $page := .GetQueryIntValue "p" 0 -}} + {{ $getPostsRes := .GetPosts $page 20 -}} +
{{ . }} diff --git a/src/http/tpl/post-edit.html b/src/http/tpl/post-edit.html index c66b60a..28dcd0e 100644 --- a/src/http/tpl/post-edit.html +++ b/src/http/tpl/post-edit.html @@ -1,5 +1,9 @@ {{ define "body" }} +{{ $tags := .GetTags }} + +{{ $post := (or (and .Payload.IsDraft .GetThisDraftPost) .GetThisPost) }} +

{{ if .Payload.IsDraft }} @@ -21,19 +25,19 @@ Unique ID

- {{ if eq .Payload.Post.ID "" }} + {{ if eq $post.ID "" }} + value="{{ $post.ID }}" /> {{ else if .Payload.IsDraft }} - {{ .Payload.Post.ID }} - + {{ $post.ID }} + {{ else }} - {{ .Payload.Post.ID }} - + {{ $post.ID }} + {{ end }}