refactor(examples): extract writeJSON helper in HTTP handlers#30
Merged
Conversation
- 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
There was a problem hiding this comment.
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-Typeandjson.NewEncoder(...).Encode(...)blocks withwriteJSON. - Updated health handlers to use the same helper.
- Added identical local
writeJSONhelpers 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Extracts a small local
writeJSONhelper in the three Go HTTP examples(go-jwks, go-jwks-multi, go-webservice) to replace the
Content-Typeheader +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
/simplify)Change classification
Plan reference
No plan — originated from a
/simplifycleanup pass over the codebase. The consensus finding across the reuse/altitude/simplification review angles was the repeated JSON-response boilerplate.Verification
go build ./...andgo vet ./...pass cleanly in all three modules.cd go-jwks && go run main.go) and confirm/healthand the/api/*endpoints still returnContent-Type: application/jsonwith the same JSON bodies as before.Verifiability check
writeJSONis a 3-line helper; each call site is a 1:1 substitution.Risk & rollback
go vet/build.git revert 63d1bc1— single self-contained commit.Reviewer guide
writeJSONdefinitions are identical; confirm each handler's substitution preserved its exact map payload and that no handler lost its content-type header.main()/ startup code in each file is untouched.🤖 Generated with Claude Code