Skip to content

refactor(examples): extract writeJSON helper in HTTP handlers#30

Merged
appleboy merged 1 commit into
mainfrom
worktree-refactor
May 29, 2026
Merged

refactor(examples): extract writeJSON helper in HTTP handlers#30
appleboy merged 1 commit into
mainfrom
worktree-refactor

Conversation

@appleboy
Copy link
Copy Markdown
Member

Summary

Extracts a small local writeJSON helper in the three Go HTTP examples
(go-jwks, go-jwks-multi, go-webservice) to replace the Content-Type header +
json.NewEncoder(...).Encode(...) boilerplate that was repeated 3–4× per file.
Centralizes JSON response writing so the content-type header can't drift between
handlers. Pure refactor — no behavior change.

AI Authorship

  • AI was used. Details:
    • Tool / model: Claude Opus 4.8 via Claude Code (/simplify)
    • AI-authored files: go-jwks/main.go, go-jwks-multi/main.go, go-webservice/main.go
    • Human line-by-line reviewed: ⚠️ None yet — author has not reviewed the diff line-by-line. Reviewer should read all three files closely.

Change classification

  • Leaf node (local impact) — standalone teaching example apps; nothing depends on them.
  • Core code

Plan reference

No plan — originated from a /simplify cleanup pass over the codebase. The consensus finding across the reuse/altitude/simplification review angles was the repeated JSON-response boilerplate.

Verification

  • Unit tests — N/A (these example apps ship no tests by repo convention)
  • Integration tests — N/A
  • e2e tests — N/A
  • Stress / soak test — N/A
  • Performed: go build ./... and go vet ./... pass cleanly in all three modules.
  • Manual verification for reviewer: run any example (e.g. cd go-jwks && go run main.go) and confirm /health and the /api/* endpoints still return Content-Type: application/json with the same JSON bodies as before.

Verifiability check

  • Inputs and outputs are documented — diff is mechanical; response shapes unchanged.
  • Reviewer can judge correctness without reading every line — writeJSON is a 3-line helper; each call site is a 1:1 substitution.
  • Failures will surface in monitoring — N/A (examples).

Risk & rollback

  • Risk: Very low. Only risk is a missed call site or a changed response shape; both are visible in the diff and caught by go vet/build.
  • Rollback: git revert 63d1bc1 — single self-contained commit.

Reviewer guide

  • Read carefully: the three writeJSON definitions are identical; confirm each handler's substitution preserved its exact map payload and that no handler lost its content-type header.
  • Spot-check OK: the main() / startup code in each file is untouched.

🤖 Generated with Claude Code

- Add a local writeJSON helper to the JWKS and webservice examples
- Replace the repeated content-type header and JSON encode boilerplate in each handler
- Centralize JSON response writing to avoid header-setting drift
Copilot AI review requested due to automatic review settings May 29, 2026 14:44
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors JSON response writing in the Go HTTP examples by extracting a shared local writeJSON helper in each file, reducing repeated response boilerplate without changing response payloads.

Changes:

  • Replaced repeated Content-Type and json.NewEncoder(...).Encode(...) blocks with writeJSON.
  • Updated health handlers to use the same helper.
  • Added identical local writeJSON helpers to the three affected examples.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
go-webservice/main.go Uses writeJSON for profile, data, and health JSON responses.
go-jwks/main.go Uses writeJSON for protected endpoint and health JSON responses.
go-jwks-multi/main.go Uses writeJSON for multi-issuer endpoint and health JSON responses.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@appleboy appleboy merged commit 7173e1e into main May 29, 2026
5 checks passed
@appleboy appleboy deleted the worktree-refactor branch May 29, 2026 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants