Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: format healthcheck responses#7723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
coadler merged 1 commit intomainfromcolin/indent-debug
May 30, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletionscoderd/debug.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -37,7 +37,7 @@ func (api *API) debugDeploymentHealth(rw http.ResponseWriter, r *http.Request) {
// Get cached report if it exists.
if report := api.healthCheckCache.Load(); report != nil {
if time.Since(report.Time) < api.HealthcheckRefresh {
httpapi.Write(ctx, rw, http.StatusOK, report)
httpapi.WriteIndent(ctx, rw, http.StatusOK, report)
return
}
}
Expand All@@ -61,7 +61,7 @@ func (api *API) debugDeploymentHealth(rw http.ResponseWriter, r *http.Request) {
})
return
case res := <-resChan:
httpapi.Write(ctx, rw, http.StatusOK, res.Val)
httpapi.WriteIndent(ctx, rw, http.StatusOK, res.Val)
return
}
}
Expand Down
30 changes: 23 additions & 7 deletionscoderd/httpapi/httpapi.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -136,24 +136,40 @@ func RouteNotFound(rw http.ResponseWriter) {
// marshaling, such as the number of elements in an array, which could help us
// spot routes that need to be paginated.
func Write(ctx context.Context, rw http.ResponseWriter, status int, response interface{}) {
// Pretty up JSON when testing.
if flag.Lookup("test.v") != nil {
WriteIndent(ctx, rw, status, response)
return
}

_, span := tracing.StartSpan(ctx)
defer span.End()

buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
rw.WriteHeader(status)

enc := json.NewEncoder(rw)
enc.SetEscapeHTML(true)
// Pretty up JSON when testing.
if flag.Lookup("test.v") != nil {
enc.SetIndent("", "\t")
}

err := enc.Encode(response)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
}

func WriteIndent(ctx context.Context, rw http.ResponseWriter, status int, response interface{}) {
_, span := tracing.StartSpan(ctx)
defer span.End()

rw.Header().Set("Content-Type", "application/json; charset=utf-8")
rw.WriteHeader(status)
_, err = rw.Write(buf.Bytes())

enc := json.NewEncoder(rw)
enc.SetEscapeHTML(true)
enc.SetIndent("", "\t")

err := enc.Encode(response)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp