Documentation
¶
Index ¶
- Constants
- func DefaultErrorEncoder(_ context.Context, err error, w http.ResponseWriter)
- func DefaultHttpErrorEncoder(_ context.Context, err error, w http.ResponseWriter)
- func DefaultHttpResponseEncoder(ctx context.Context, w http.ResponseWriter, response interface{}) error
- func EncodeJSONResponse(_ context.Context, w http.ResponseWriter, response interface{}) error
- func Nop(context.Context, interface{}) (interface{}, error)
- func NopRequestDecoder(ctx context.Context, r *http.Request) (interface{}, error)
- func PopulateRequestContext(ctx context.Context, r *http.Request) context.Context
- type ClientResponseFunc
- type CodedError
- type DecodeRequestFunc
- type EncodeResponseFunc
- type Endpoint
- type ErrorEncoder
- type ErrorHandler
- type ErrorHandlerFunc
- type Failer
- type Headerer
- type HttpCoder
- type LogErrorHandler
- type Middleware
- type RequestFunc
- type Server
- type ServerFinalizerFunc
- type ServerOption
- func ServerAfter(after ...ServerResponseFunc) ServerOption
- func ServerBefore(before ...RequestFunc) ServerOption
- func ServerErrorEncoder(ee ErrorEncoder) ServerOption
- func ServerErrorHandler(errorHandler ErrorHandler) ServerOption
- func ServerErrorLogger(logger log.Logger) ServerOption
- func ServerFinalizer(f ...ServerFinalizerFunc) ServerOption
- type ServerResponseFunc
- type StatusCoder
Constants ¶
const ( // ContextKeyRequestMethod is populated in the context by // PopulateRequestContext. Its value is r.Method. ContextKeyRequestMethod contextKey = iota // ContextKeyRequestURI is populated in the context by // PopulateRequestContext. Its value is r.RequestURI. ContextKeyRequestURI // ContextKeyRequestPath is populated in the context by // PopulateRequestContext. Its value is r.URL.Path. ContextKeyRequestPath // ContextKeyRequestProto is populated in the context by // PopulateRequestContext. Its value is r.Proto. ContextKeyRequestProto // ContextKeyRequestHost is populated in the context by // PopulateRequestContext. Its value is r.Host. ContextKeyRequestHost // ContextKeyRequestRemoteAddr is populated in the context by // PopulateRequestContext. Its value is r.RemoteAddr. ContextKeyRequestRemoteAddr // ContextKeyRequestXForwardedFor is populated in the context by // PopulateRequestContext. Its value is r.Header.Get("X-Forwarded-For"). ContextKeyRequestXForwardedFor // ContextKeyRequestXForwardedProto is populated in the context by // PopulateRequestContext. Its value is r.Header.Get("X-Forwarded-Proto"). ContextKeyRequestXForwardedProto // ContextKeyRequestAuthorization is populated in the context by // PopulateRequestContext. Its value is r.Header.Get("Authorization"). ContextKeyRequestAuthorization // ContextKeyRequestReferer is populated in the context by // PopulateRequestContext. Its value is r.Header.Get("Referer"). ContextKeyRequestReferer // ContextKeyRequestUserAgent is populated in the context by // PopulateRequestContext. Its value is r.Header.Get("User-Agent"). ContextKeyRequestUserAgent // ContextKeyRequestXRequestID is populated in the context by // PopulateRequestContext. Its value is r.Header.Get("X-Request-Id"). ContextKeyRequestXRequestID // ContextKeyRequestAccept is populated in the context by // PopulateRequestContext. Its value is r.Header.Get("Accept"). ContextKeyRequestAccept // ContextKeyResponseHeaders is populated in the context whenever a // ServerFinalizerFunc is specified. Its value is of type http.Header, and // is captured only once the entire response has been written. ContextKeyResponseHeaders // ContextKeyResponseSize is populated in the context whenever a // ServerFinalizerFunc is specified. Its value is of type int64. ContextKeyResponseSize )
Variables ¶
This section is empty.
Functions ¶
func DefaultErrorEncoder ¶ added in v1.0.0
func DefaultErrorEncoder(_ context.Context, err error, w http.ResponseWriter)
DefaultErrorEncoder writes the error to the ResponseWriter, by default a content type of text/plain, a body of the plain text of the error, and a status code of 500. If the error implements Headerer, the provided headers will be applied to the response. If the error implements json.Marshaler, and the marshaling succeeds, a content type of application/json and the JSON encoded form of the error will be used. If the error implements StatusCoder, the provided StatusCode will be used instead of 500.
func DefaultHttpErrorEncoder ¶
func DefaultHttpErrorEncoder(_ context.Context, err error, w http.ResponseWriter)
DefaultHttpErrorEncoder
Computes the default http error response. When implementing custom gkBoot.HttpEncoder, ensure to implement your own error encoder handler.
func DefaultHttpResponseEncoder ¶
func DefaultHttpResponseEncoder(ctx context.Context, w http.ResponseWriter, response interface{}) error
DefaultHttpResponseEncoder
Computes the http response encoding, for different formats, you must attach your own gkBoot.HttpEncoder to your gkBoot.Service for each one defined
func EncodeJSONResponse ¶ added in v1.0.0
func EncodeJSONResponse(_ context.Context, w http.ResponseWriter, response interface{}) error
EncodeJSONResponse is a EncodeResponseFunc that serializes the response as a JSON object to the ResponseWriter. Many JSON-over-HTTP services can use it as a sensible default. If the response implements Headerer, the provided headers will be applied to the response. If the response implements StatusCoder, the provided StatusCode will be used instead of 200.
func Nop ¶ added in v1.0.0
Nop is an endpoint that does nothing and returns a nil error. Useful for tests.
func NopRequestDecoder ¶ added in v1.0.0
NopRequestDecoder is a DecodeRequestFunc that can be used for requests that do not need to be decoded, and simply returns nil, nil.
func PopulateRequestContext ¶ added in v1.0.0
PopulateRequestContext is a RequestFunc that populates several values into the context from the HTTP request. Those values may be extracted using the corresponding ContextKey type in this package.
Types ¶
type ClientResponseFunc ¶ added in v1.0.0
ClientResponseFunc may take information from an HTTP request and make the response available for consumption. ClientResponseFuncs are only executed in clients, after a request has been made, but prior to it being decoded.
type CodedError ¶
type CodedError interface {
StatusCode() int
}
CodedError
Indicates an error capable of storing a status code. Used with default (en/de)-coders
type DecodeRequestFunc ¶
DecodeRequestFunc any decoder that takes on this format, goal is to translate http.Request to API request object
type EncodeResponseFunc ¶
type EncodeResponseFunc func(context.Context, http.ResponseWriter, interface{}) error
EncodeResponseFunc any encoder that takes on this format, goal is to translate API response object to the http.ResponseWriter
type Endpoint ¶ added in v1.0.0
Endpoint is the fundamental building block of servers and clients. It represents a single RPC method.
type ErrorEncoder ¶ added in v1.0.0
type ErrorEncoder func(ctx context.Context, err error, w http.ResponseWriter)
ErrorEncoder is responsible for encoding an error to the ResponseWriter. Users are encouraged to use custom ErrorEncoders to encode HTTP errors to their clients, and will likely want to pass and check for their own error types. See the example shipping/handling service.
type ErrorHandler ¶ added in v1.0.0
ErrorHandler receives a transport error to be processed for diagnostic purposes. Usually this means logging the error.
type ErrorHandlerFunc ¶ added in v1.0.0
The ErrorHandlerFunc type is an adapter to allow the use of ordinary function as ErrorHandler. If f is a function with the appropriate signature, ErrorHandlerFunc(f) is a ErrorHandler that calls f.
type Failer ¶
type Failer interface {
Failed() error
}
Failer may be implemented by Go kit response types that contain business logic error details. If Failed returns a non-nil error, the Go kit transport layer may interpret this as a business logic error, and may encode it differently than a regular, successful response.
It's not necessary for your response types to implement Failer, but it may help for more sophisticated use cases. The addsvc example shows how Failer should be used by a complete application.
type Headerer ¶ added in v1.0.0
Headerer is checked by DefaultErrorEncoder. If an error value implements Headerer, the provided headers will be applied to the response writer, after the Content-Type is set.
type HttpCoder ¶
type HttpCoder interface {
StatusCode() int
}
HttpCoder is any response that returns a status code
type LogErrorHandler ¶ added in v1.0.0
type LogErrorHandler struct {
// contains filtered or unexported fields
}
LogErrorHandler is a transport error handler implementation which logs an error.
func NewLogErrorHandler ¶ added in v1.0.0
func NewLogErrorHandler(logger log.Logger) *LogErrorHandler
type Middleware ¶ added in v1.0.0
Middleware is a chainable behavior modifier for endpoints.
type RequestFunc ¶ added in v1.0.0
RequestFunc may take information from an HTTP request and put it into a request context. In Servers, RequestFuncs are executed prior to invoking the endpoint. In Clients, RequestFuncs are executed after creating the request but prior to invoking the HTTP client.
func SetRequestHeader ¶ added in v1.0.0
func SetRequestHeader(key, val string) RequestFunc
SetRequestHeader returns a RequestFunc that sets the given header.
type Server ¶ added in v1.0.0
type Server struct {
// contains filtered or unexported fields
}
Server wraps an endpoint and implements http.Handler.
func NewServer ¶ added in v1.0.0
func NewServer( e Endpoint, dec DecodeRequestFunc, enc EncodeResponseFunc, options ...ServerOption, ) *Server
NewServer constructs a new server, which implements http.Handler and wraps the provided endpoint.
type ServerFinalizerFunc ¶ added in v1.0.0
ServerFinalizerFunc can be used to perform work at the end of an HTTP request, after the response has been written to the client. The principal intended use is for request logging. In addition to the response code provided in the function signature, additional response parameters are provided in the context under keys with the ContextKeyResponse prefix.
type ServerOption ¶ added in v1.0.0
type ServerOption func(*Server)
ServerOption sets an optional parameter for servers.
func ServerAfter ¶ added in v1.0.0
func ServerAfter(after ...ServerResponseFunc) ServerOption
ServerAfter functions are executed on the HTTP response writer after the endpoint is invoked, but before anything is written to the client.
func ServerBefore ¶ added in v1.0.0
func ServerBefore(before ...RequestFunc) ServerOption
ServerBefore functions are executed on the HTTP request object before the request is decoded.
func ServerErrorEncoder ¶ added in v1.0.0
func ServerErrorEncoder(ee ErrorEncoder) ServerOption
ServerErrorEncoder is used to encode errors to the http.ResponseWriter whenever they're encountered in the processing of a request. Clients can use this to provide custom error formatting and response codes. By default, errors will be written with the DefaultErrorEncoder.
func ServerErrorHandler ¶ added in v1.0.0
func ServerErrorHandler(errorHandler ErrorHandler) ServerOption
ServerErrorHandler is used to handle non-terminal errors. By default, non-terminal errors are ignored. This is intended as a diagnostic measure. Finer-grained control of error handling, including logging in more detail, should be performed in a custom ServerErrorEncoder or ServerFinalizer, both of which have access to the context.
func ServerErrorLogger ¶ added in v1.0.0
func ServerErrorLogger(logger log.Logger) ServerOption
ServerErrorLogger is used to log non-terminal errors. By default, no errors are logged. This is intended as a diagnostic measure. Finer-grained control of error handling, including logging in more detail, should be performed in a custom ServerErrorEncoder or ServerFinalizer, both of which have access to the context. Deprecated: Use ServerErrorHandler instead.
func ServerFinalizer ¶ added in v1.0.0
func ServerFinalizer(f ...ServerFinalizerFunc) ServerOption
ServerFinalizer is executed at the end of every HTTP request. By default, no finalizer is registered.
type ServerResponseFunc ¶ added in v1.0.0
ServerResponseFunc may take information from a request context and use it to manipulate a ResponseWriter. ServerResponseFuncs are only executed in servers, after invoking the endpoint but prior to writing a response.
func SetContentType ¶ added in v1.0.0
func SetContentType(contentType string) ServerResponseFunc
SetContentType returns a ServerResponseFunc that sets the Content-Type header to the provided value.
func SetResponseHeader ¶ added in v1.0.0
func SetResponseHeader(key, val string) ServerResponseFunc
SetResponseHeader returns a ServerResponseFunc that sets the given header.
type StatusCoder ¶ added in v1.0.0
type StatusCoder interface {
StatusCode() int
}
StatusCoder is checked by DefaultErrorEncoder. If an error value implements StatusCoder, the StatusCode will be used when encoding the error. By default, StatusInternalServerError (500) is used.