From 6fbd05d37428c82f02c2801af4f6c051458d58c4 Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Tue, 30 May 2023 19:45:30 +0000 Subject: [PATCH] feat: format healthcheck responses --- coderd/debug.go | 4 ++-- coderd/httpapi/httpapi.go | 30 +++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/coderd/debug.go b/coderd/debug.go index ef9a0018210d1..8fef634b5aacf 100644 --- a/coderd/debug.go +++ b/coderd/debug.go @@ -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 } } @@ -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 } } diff --git a/coderd/httpapi/httpapi.go b/coderd/httpapi/httpapi.go index 135b2d4d184dd..658c0cc39294b 100644 --- a/coderd/httpapi/httpapi.go +++ b/coderd/httpapi/httpapi.go @@ -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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy