Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 deletions coderd/debug.go
Original file line number Diff line number Diff 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 deletions coderd/httpapi/httpapi.go
Original file line number Diff line number Diff 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