MCPcopy Index your code
hub / github.com/coder/coder / Write

Function Write

coderd/httpapi/httpapi.go:191–212  ·  view source on GitHub ↗

Write outputs a standardized format to an HTTP response body. ctx is used for tracing and can be nil for tracing to be disabled. Tracing this function is helpful because JSON marshaling can sometimes take a non-insignificant amount of time, and could help us catch outliers. Additionally, we can enri

(ctx context.Context, rw http.ResponseWriter, status int, response interface{})

Source from the content-addressed store, hash-verified

189// marshaling, such as the number of elements in an array, which could help us
190// spot routes that need to be paginated.
191func Write(ctx context.Context, rw http.ResponseWriter, status int, response interface{}) {
192 // Pretty up JSON when testing.
193 if flag.Lookup("test.v") != nil {
194 WriteIndent(ctx, rw, status, response)
195 return
196 }
197
198 _, span := tracing.StartSpan(ctx)
199 defer span.End()
200
201 SetAuthzCheckRecorderHeader(ctx, rw)
202
203 rw.Header().Set("Content-Type", "application/json; charset=utf-8")
204 rw.WriteHeader(status)
205
206 enc := json.NewEncoder(rw)
207 enc.SetEscapeHTML(true)
208
209 // We can't really do much about these errors, it's probably due to a
210 // dropped connection.
211 _ = enc.Encode(response)
212}
213
214func WriteIndent(ctx context.Context, rw http.ResponseWriter, status int, response interface{}) {
215 _, span := tracing.StartSpan(ctx)

Callers 15

postUserSecretMethod · 0.92
getUserSecretsMethod · 0.92
getUserSecretMethod · 0.92
patchUserSecretMethod · 0.92
deleteUserSecretMethod · 0.92
TestExternalAuthByIDFunction · 0.92
TestExternalAuthDeviceFunction · 0.92
provisionerDaemonsMethod · 0.92
NewFunction · 0.92
externalAuthByIDMethod · 0.92

Calls 7

StartSpanFunction · 0.92
WriteIndentFunction · 0.85
EncodeMethod · 0.80
SetMethod · 0.65
HeaderMethod · 0.45
WriteHeaderMethod · 0.45

Tested by 15

TestExternalAuthByIDFunction · 0.74
TestExternalAuthDeviceFunction · 0.74
TestAPIKeyFunction · 0.74
TestWriteFunction · 0.74
TestToolsFunction · 0.74