diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2024-05-26 22:06:44 +0200 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2024-05-26 22:06:44 +0200 |
commit | faa296075f5ea2d8e01004b46b036997f9529d99 (patch) | |
tree | 5d5e2cb693302dd7239aa02a183d2b0efff242c2 /src/http/posts.go | |
parent | 006c0028106425abb3f718b2e86349dee5b7a2ea (diff) |
Clean out Get/SetRequestLogger from apiutil
Diffstat (limited to 'src/http/posts.go')
-rw-r--r-- | src/http/posts.go | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/src/http/posts.go b/src/http/posts.go index 1bc65c8..5a295a7 100644 --- a/src/http/posts.go +++ b/src/http/posts.go @@ -165,12 +165,14 @@ func (a *api) publishPost(ctx context.Context, p post.Post) error { func (a *api) postPostHandler() http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - - ctx := r.Context() + var ( + ctx = r.Context() + logger = a.params.Logger + ) p, err := postFromPostReq(r) if err != nil { - apiutil.BadRequest(rw, r, err) + apiutil.BadRequest(ctx, logger, rw, "%w", err) return } @@ -178,7 +180,7 @@ func (a *api) postPostHandler() http.Handler { if err := a.publishPost(ctx, p); err != nil { apiutil.InternalServerError( - rw, r, fmt.Errorf("publishing post with id %q: %w", p.ID, err), + ctx, logger, rw, "publishing post with id %q: %w", p.ID, err, ) return } @@ -192,11 +194,14 @@ func (a *api) postPostHandler() http.Handler { func (a *api) deletePostHandler(isDraft bool) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - - id := filepath.Base(r.URL.Path) + var ( + ctx = r.Context() + logger = a.params.Logger + id = filepath.Base(r.URL.Path) + ) if id == "/" { - apiutil.BadRequest(rw, r, errors.New("id is required")) + apiutil.BadRequest(ctx, logger, rw, "id is required") return } @@ -213,7 +218,7 @@ func (a *api) deletePostHandler(isDraft bool) http.Handler { return } else if err != nil { apiutil.InternalServerError( - rw, r, fmt.Errorf("deleting post with id %q: %w", id, err), + ctx, logger, rw, "deleting post with id %q: %w", id, err, ) return } @@ -235,10 +240,14 @@ func (a *api) previewPostHandler() http.Handler { tpl := a.mustParseBasedTpl("post.html") return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + var ( + ctx = r.Context() + logger = a.params.Logger + ) p, err := postFromPostReq(r) if err != nil { - apiutil.BadRequest(rw, r, err) + apiutil.BadRequest(ctx, logger, rw, "%w", err) return } @@ -247,7 +256,7 @@ func (a *api) previewPostHandler() http.Handler { PublishedAt: time.Now(), } - r = r.WithContext(render.WithPost(r.Context(), storedPost)) + r = r.WithContext(render.WithPost(ctx, storedPost)) a.executeTemplate(rw, r, tpl, nil) }) } |