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/http/posts.go | 167 +++--------------------------------------------------- 1 file changed, 9 insertions(+), 158 deletions(-) (limited to 'src/http/posts.go') 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) + }) }) } -- cgit v1.2.3