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

Commit43e0968

Browse files
authored
feat: format healthcheck responses (#7723)
1 parent73d795f commit43e0968

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

‎coderd/debug.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (api *API) debugDeploymentHealth(rw http.ResponseWriter, r *http.Request) {
3737
// Get cached report if it exists.
3838
ifreport:=api.healthCheckCache.Load();report!=nil {
3939
iftime.Since(report.Time)<api.HealthcheckRefresh {
40-
httpapi.Write(ctx,rw,http.StatusOK,report)
40+
httpapi.WriteIndent(ctx,rw,http.StatusOK,report)
4141
return
4242
}
4343
}
@@ -61,7 +61,7 @@ func (api *API) debugDeploymentHealth(rw http.ResponseWriter, r *http.Request) {
6161
})
6262
return
6363
caseres:=<-resChan:
64-
httpapi.Write(ctx,rw,http.StatusOK,res.Val)
64+
httpapi.WriteIndent(ctx,rw,http.StatusOK,res.Val)
6565
return
6666
}
6767
}

‎coderd/httpapi/httpapi.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,40 @@ func RouteNotFound(rw http.ResponseWriter) {
136136
// marshaling, such as the number of elements in an array, which could help us
137137
// spot routes that need to be paginated.
138138
funcWrite(ctx context.Context,rw http.ResponseWriter,statusint,responseinterface{}) {
139+
// Pretty up JSON when testing.
140+
ifflag.Lookup("test.v")!=nil {
141+
WriteIndent(ctx,rw,status,response)
142+
return
143+
}
144+
139145
_,span:=tracing.StartSpan(ctx)
140146
deferspan.End()
141147

142-
buf:=&bytes.Buffer{}
143-
enc:=json.NewEncoder(buf)
148+
rw.Header().Set("Content-Type","application/json; charset=utf-8")
149+
rw.WriteHeader(status)
150+
151+
enc:=json.NewEncoder(rw)
144152
enc.SetEscapeHTML(true)
145-
// Pretty up JSON when testing.
146-
ifflag.Lookup("test.v")!=nil {
147-
enc.SetIndent("","\t")
148-
}
153+
149154
err:=enc.Encode(response)
150155
iferr!=nil {
151156
http.Error(rw,err.Error(),http.StatusInternalServerError)
152157
return
153158
}
159+
}
160+
161+
funcWriteIndent(ctx context.Context,rw http.ResponseWriter,statusint,responseinterface{}) {
162+
_,span:=tracing.StartSpan(ctx)
163+
deferspan.End()
164+
154165
rw.Header().Set("Content-Type","application/json; charset=utf-8")
155166
rw.WriteHeader(status)
156-
_,err=rw.Write(buf.Bytes())
167+
168+
enc:=json.NewEncoder(rw)
169+
enc.SetEscapeHTML(true)
170+
enc.SetIndent("","\t")
171+
172+
err:=enc.Encode(response)
157173
iferr!=nil {
158174
http.Error(rw,err.Error(),http.StatusInternalServerError)
159175
return

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp