* implement functions to append/get metadata and set/get status code * сhanged behavior to return nil instead of empty metadata for getResponseMetadata() * сhanged work with HTTP headers to use direct array assignment instead of for-range * fix linters * fix meter handler * fix uninitialized response metadata for incoming context * removed a useless test * metrics handler has been fixed to work with compressed data
30 lines
606 B
Go
30 lines
606 B
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
)
|
|
|
|
type (
|
|
rspStatusCodeKey struct{}
|
|
rspStatusCodeVal struct {
|
|
code int
|
|
}
|
|
)
|
|
|
|
// SetResponseStatusCode sets the status code in the context.
|
|
func SetResponseStatusCode(ctx context.Context, code int) {
|
|
if rsp, ok := ctx.Value(rspStatusCodeKey{}).(*rspStatusCodeVal); ok {
|
|
rsp.code = code
|
|
}
|
|
}
|
|
|
|
// GetResponseStatusCode retrieves the response status code from the context.
|
|
func GetResponseStatusCode(ctx context.Context) int {
|
|
code := http.StatusOK
|
|
if rsp, ok := ctx.Value(rspStatusCodeKey{}).(*rspStatusCodeVal); ok {
|
|
code = rsp.code
|
|
}
|
|
return code
|
|
}
|