Documentation ¶
Index ¶
- Constants
- Variables
- func AtlasDefaultHeaderMatcher() func(string) (string, bool)
- func ChainHeaderMatcher(matchers ...runtime.HeaderMatcherFunc) runtime.HeaderMatcherFunc
- func ClientUnaryInterceptor(parentCtx context.Context, method string, req, reply interface{}, ...) error
- func Code(cname string) codes.Code
- func CodeName(c codes.Code) string
- func ExtendedDefaultHeaderMatcher(headerNames ...string) func(string) (string, bool)
- func GeoIPHeaderMatcher() runtime.HeaderMatcherFunc
- func GetCollectionOp(res, op interface{}) error
- func GetFieldSelection(req proto.Message) (fieldName string, fs *query.FieldSelection, err error)
- func GetFiltering(req proto.Message) (fieldName string, f *query.Filtering, err error)
- func GetGeoHeaders() []string
- func GetPageInfo(resp proto.Message) (fieldName string, pg *query.PageInfo, err error)
- func GetPagination(req proto.Message) (fieldName string, p *query.Pagination, err error)
- func GetSearching(req proto.Message) (fieldName string, s *query.Searching, err error)
- func GetSorting(req proto.Message) (fieldName string, s *query.Sorting, err error)
- func GetXB3Headers() []string
- func HTTPStatus(ctx context.Context, st *status.Status) (int, string)
- func HTTPStatusFromCode(code codes.Code) int
- func HTTPStatusWithMethod(ctx context.Context, method string, st *status.Status) (int, string)
- func Header(ctx context.Context, key string) (string, bool)
- func HeaderN(ctx context.Context, key string, n int) (val []string, found bool)
- func IncludeStatusDetails(withDetails bool)
- func MetadataAnnotator(ctx context.Context, req *http.Request) metadata.MD
- func NewGateway(options ...Option) (*http.ServeMux, error)
- func NewPresenceAnnotator(methods ...string) func(context.Context, *http.Request) metadata.MD
- func NewProtoMessageErrorHandler(out runtime.HeaderMatcherFunc) runtime.ErrorHandlerFunc
- func NewResponseError(ctx context.Context, msg string, kvpairs ...interface{}) error
- func NewResponseErrorWithCode(ctx context.Context, c codes.Code, msg string, kvpairs ...interface{}) error
- func PrefixOutgoingHeaderMatcher(key string) (string, bool)
- func PresenceClientInterceptor(options ...presenceInterceptorOption) grpc.UnaryClientInterceptor
- func QueryFilterWith(extraFields []string) *utilities.DoubleArray
- func RequestIDHeaderMatcher() runtime.HeaderMatcherFunc
- func SetCollectionOps(req, op interface{}) error
- func SetCreated(ctx context.Context, msg string) error
- func SetDeleted(ctx context.Context, msg string) error
- func SetPageInfo(ctx context.Context, p *query.PageInfo) error
- func SetRunning(ctx context.Context, message, resource string) error
- func SetStatus(ctx context.Context, st *status.Status) error
- func SetUpdated(ctx context.Context, msg string) error
- func TracingHeaderMatcher() runtime.HeaderMatcherFunc
- func UnaryServerInterceptor() grpc.UnaryServerInterceptor
- func WithCodedSuccess(ctx context.Context, c codes.Code, msg string, args ...interface{}) error
- func WithError(ctx context.Context, err error)
- func WithOverrideFieldMask(d *presenceInterceptorOptionsDecorator)
- func WithSuccess(ctx context.Context, msg MessageWithFields)
- type ForwardResponseMessageFunc
- type ForwardResponseStreamFunc
- type MessageWithFields
- type Option
- type ProtoErrorHandler
- type ProtoStreamErrorHandlerFunc
- type ResponseForwarder
- type RestErrs
Constants ¶
const ( // These custom codes defined here to conform REST API Syntax // It is supposed that you do not send them over the wire as part of gRPC Status, // because they will be treated as Unknown by gRPC library. // You should use them to send successfull status of your RPC method // using SetStatus function from this package. Created codes.Code = 10000 + iota // 10000 is an offset from standard codes Updated Deleted LongRunning PartialContent )
const ( // DefaultServerAddress is the standard gRPC server address that a REST // gateway will connect to. DefaultServerAddress = ":9090" )
const XForwardedFor = "X-Forwarded-For"
Variables ¶
var ( // ProtoMessageErrorHandler uses PrefixOutgoingHeaderMatcher. // To use ProtoErrorHandler with custom outgoing header matcher call NewProtoMessageErrorHandler. ProtoMessageErrorHandler = NewProtoMessageErrorHandler(PrefixOutgoingHeaderMatcher) // ProtoStreamErrorHandler uses PrefixOutgoingHeaderMatcher. // To use ProtoErrorHandler with custom outgoing header matcher call NewProtoStreamErrorHandler. ProtoStreamErrorHandler = NewProtoStreamErrorHandler(PrefixOutgoingHeaderMatcher) )
var ( // ForwardResponseMessage is default implementation of ForwardResponseMessageFunc ForwardResponseMessage = NewForwardResponseMessage(PrefixOutgoingHeaderMatcher, ProtoMessageErrorHandler, ProtoStreamErrorHandler) // ForwardResponseStream is default implementation of ForwardResponseStreamFunc ForwardResponseStream = NewForwardResponseStream(PrefixOutgoingHeaderMatcher, ProtoMessageErrorHandler, ProtoStreamErrorHandler) )
var DefaultQueryFilter = utilities.NewDoubleArray(defaultFilterFields)
DefaultQueryFilter can be set to override the filter_{service}_{rpc}_{num} field in generated .pb.gw.go files to prevent parse errors in the gateway and potentially reduce log noise due to unrecognized fields
var OldStatusCreatedOnUpdate = false
OldStatesCreatedOnUpdate if true will return http.StatusCreated from HTTPStatusFromCode function if gRPC code is equal to Updated. This variable should only be set in an init() function by code that vendors this library.
var StatusFromMethod = true
StatusFromMethod if true will cause the HTTP code returned to be set depending on the HTTP method, ex. a 201 for POST as a "create" operation
Functions ¶
func AtlasDefaultHeaderMatcher ¶
AtlasDefaultHeaderMatcher func used to add all headers used by atlas-app-toolkit This function also passes through all the headers that runtime.DefaultHeaderMatcher handles. AtlasDefaultHeaderMatcher can be used as a Incoming/Outgoing header matcher.
func ChainHeaderMatcher ¶
func ChainHeaderMatcher(matchers ...runtime.HeaderMatcherFunc) runtime.HeaderMatcherFunc
ChainHeaderMatcher func is used to build chain on header matcher funcitons this function can be used as incoming or outgoing header matcher keep in mind that gRPC metadata treat as case insensitive strings
func ClientUnaryInterceptor ¶
func ClientUnaryInterceptor(parentCtx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error
ClientUnaryInterceptor parse collection operators and stores in corresponding message fields
func Code ¶
Code returns an instance of gRPC code by its string name. The `cname` must be in upper case and one of the code names defined in REST API Syntax. If code name is invalid or unknow the codes.Unknown will be returned.
func CodeName ¶
CodeName returns stringname of gRPC code, function handles as standard codes from "google.golang.org/grpc/codes" as well as custom ones defined in this package. The codes.Unimplemented is named "NOT_IMPLEMENTED" in accordance with REST API Syntax Specification.
func ExtendedDefaultHeaderMatcher ¶
ExtendedDefaultHeaderMatcher func is used to add custom headers to be matched from incoming http requests, If this returns true the header will be added to grpc context. This function also passes through all the headers that runtime.DefaultHeaderMatcher handles.
func GeoIPHeaderMatcher ¶
func GeoIPHeaderMatcher() runtime.HeaderMatcherFunc
GeoIPHeaderMatcher X-Geo-* headers are set of geo metadata from MaxMind DB injected on ingress nginx
func GetCollectionOp ¶
func GetCollectionOp(res, op interface{}) error
func GetFieldSelection ¶
func GetFiltering ¶
func GetPageInfo ¶
func GetPagination ¶
func GetSearching ¶ added in v2.2.0
func GetSorting ¶
func HTTPStatus ¶
Status returns REST representation of gRPC status. If status.Status is not nil it will be converted in accordance with REST API Syntax otherwise context will be used to extract `grpcgateway-status-code` from gRPC metadata. If `grpcgateway-status-code` is not set it is assumed that it is OK.
func HTTPStatusFromCode ¶
HTTPStatusFromCode converts a gRPC error code into the corresponding HTTP response status.
func HTTPStatusWithMethod ¶ added in v2.2.0
HTTPStatusWithMethod returns REST representation of gRPC status. If status.Status is not nil it will be converted in accordance with REST API Syntax otherwise context will be used to extract `grpcgateway-status-code` from gRPC metadata. If `grpcgateway-status-code` is not set it falls back on the method string provided, using default expectations for POST, PUT/PATCH, and DELETE verbs
func Header ¶
Header returns first value for a given key if it exists in gRPC metadata from incoming or outcoming context, otherwise returns (nil, false)
Calls HeaderN(ctx, key, 1)
Provided key is converted to lowercase (see grpc/metadata.New). If key is not found the prefix "grpcgateway-" is added to the key and key is being searched once again.
func HeaderN ¶
HeaderN returns first n values for a given key if it exists in gRPC metadata from incoming or outcoming context, otherwise returns (nil, false)
If n < 0 all values for a given key will be returned If n > 0 at least n values will be returned, or (nil, false) If n == 0 result is (nil, false)
Provided key is converted to lowercase (see grpc/metadata.New). If key is not found the prefix "grpcgateway-" is added to the key and key is being searched once again.
func IncludeStatusDetails ¶
func IncludeStatusDetails(withDetails bool)
IncludeStatusDetails enables/disables output of status & code fields in all http json translated in the gateway with this package's ForwardResponseMessage
func MetadataAnnotator ¶
MetadataAnnotator is a function for passing metadata to a gRPC context It must be mainly used as ServeMuxOption for gRPC Gateway 'ServeMux' See: 'WithMetadata' option.
MetadataAnnotator stores request URL in gRPC metadata from incoming HTTP кequest
func NewGateway ¶
NewGateway creates a gRPC REST gateway with HTTP handlers that have been generated by the gRPC gateway protoc plugin
func NewPresenceAnnotator ¶
NewPresenceAnnotator will parse the JSON input and then add the paths to the metadata to be pulled from context later
func NewProtoMessageErrorHandler ¶
func NewProtoMessageErrorHandler(out runtime.HeaderMatcherFunc) runtime.ErrorHandlerFunc
NewProtoMessageErrorHandler returns runtime.ProtoErrorHandlerFunc
func NewResponseError ¶
NewResponseError sets the error in the context with extra fields, to override the standard message-only error
func NewResponseErrorWithCode ¶
func NewResponseErrorWithCode(ctx context.Context, c codes.Code, msg string, kvpairs ...interface{}) error
NewResponseErrorWithCode sets the return code and returns an error with extra fields in MD to be extracted in the gateway response writer
func PrefixOutgoingHeaderMatcher ¶
PrefixOutgoingHeaderMatcher discards all grpc header metadata.
func PresenceClientInterceptor ¶
func PresenceClientInterceptor(options ...presenceInterceptorOption) grpc.UnaryClientInterceptor
PresenceClientInterceptor gets the interceptor for populating a fieldmask in a proto message from the fields given in the metadata/context
func QueryFilterWith ¶
func QueryFilterWith(extraFields []string) *utilities.DoubleArray
QueryFilterWith will add extra fields to the standard fields in the default filter.
func RequestIDHeaderMatcher ¶
func RequestIDHeaderMatcher() runtime.HeaderMatcherFunc
RequestIDHeaderMatcher request id header contains unique identifier for request
func SetCollectionOps ¶
func SetCollectionOps(req, op interface{}) error
func SetCreated ¶
SetCreated is a shortcut for SetStatus(ctx, status.New(Created, msg))
func SetDeleted ¶
SetDeleted is a shortcut for SetStatus(ctx, status.New(Deleted, msg))
func SetPageInfo ¶
SetPagination sets page info to outgoing gRPC context. Deprecated: Please add `infoblox.api.PageInfo` as part of gRPC message and do not rely on outgoing gRPC context.
func SetRunning ¶
SetRunning is a shortcut for SetStatus(ctx, status.New(LongRunning, url))
func SetStatus ¶
SetStatus sets gRPC status as gRPC metadata Status.Code will be set with metadata key `grpcgateway-status-code` and with value as string name of the code. Status.Message will be set with metadata key `grpcgateway-status-message` and with corresponding value.
func SetUpdated ¶
SetUpdated is a shortcut for SetStatus(ctx, status.New(Updated, msg))
func TracingHeaderMatcher ¶
func TracingHeaderMatcher() runtime.HeaderMatcherFunc
TracingHeaderMatcher tracing headers
func UnaryServerInterceptor ¶
func UnaryServerInterceptor() grpc.UnaryServerInterceptor
UnaryServerInterceptor returns grpc.UnaryServerInterceptor that should be used as a middleware if an user's request message defines any of collection operators.
Returned middleware populates collection operators from gRPC metadata if they defined in a request message.
func WithCodedSuccess ¶
WithCodedSuccess wraps a SetStatus and WithSuccess call into one, just to make things a little more "elegant"
func WithError ¶
WithError will save an error message into the grpc trailer metadata, if it is an error that implements MessageWithFields, it also saves the fields. This error will then be inserted into the return JSON if the ResponseForwarder is used
func WithOverrideFieldMask ¶
func WithOverrideFieldMask(d *presenceInterceptorOptionsDecorator)
WithOverrideFieldMask represent an option to override field mask generated by grpc-gateway
func WithSuccess ¶
func WithSuccess(ctx context.Context, msg MessageWithFields)
WithSuccess will save a MessageWithFields into the grpc trailer metadata. This success message will then be inserted into the return JSON if the ResponseForwarder is used
Types ¶
type ForwardResponseMessageFunc ¶
type ForwardResponseMessageFunc func(context.Context, *runtime.ServeMux, runtime.Marshaler, http.ResponseWriter, *http.Request, protoreflect.ProtoMessage, ...func(context.Context, http.ResponseWriter, protoreflect.ProtoMessage) error)
ForwardResponseMessageFunc forwards gRPC response to HTTP client inaccordance with REST API Syntax
func NewForwardResponseMessage ¶
func NewForwardResponseMessage(out runtime.HeaderMatcherFunc, meh runtime.ErrorHandlerFunc, seh ProtoStreamErrorHandlerFunc) ForwardResponseMessageFunc
NewForwardResponseMessage returns ForwardResponseMessageFunc
type ForwardResponseStreamFunc ¶
type ForwardResponseStreamFunc func(context.Context, *runtime.ServeMux, runtime.Marshaler, http.ResponseWriter, *http.Request, func() (protoreflect.ProtoMessage, error), ...func(context.Context, http.ResponseWriter, protoreflect.ProtoMessage) error)
ForwardResponseStreamFunc forwards gRPC stream response to HTTP client inaccordance with REST API Syntax
func NewForwardResponseStream ¶
func NewForwardResponseStream(out runtime.HeaderMatcherFunc, meh runtime.ErrorHandlerFunc, seh ProtoStreamErrorHandlerFunc) ForwardResponseStreamFunc
NewForwardResponseStream returns ForwardResponseStreamFunc
type MessageWithFields ¶
func NewWithFields ¶
func NewWithFields(message string, kvpairs ...interface{}) MessageWithFields
NewWithFields returns a new MessageWithFields that requires a message string, and then treats the following arguments as alternating keys and values a non-string key will immediately return the result so far, ignoring later values. The values can be any type
type Option ¶
type Option func(*gateway)
Option is a functional option that modifies the REST gateway on initialization
func WithDialOptions ¶
func WithDialOptions(options ...grpc.DialOption) Option
WithDialOptions assigns a list of gRPC dial options to the REST gateway
func WithEndpointRegistration ¶
WithEndpointRegistration takes a group of HTTP handlers that have been generated by the gRPC gateway protoc plugin and registers them to the REST gateway with some prefix (e.g. www.website.com/prefix/endpoint)
func WithGatewayOptions ¶
func WithGatewayOptions(opt ...runtime.ServeMuxOption) Option
WithGatewayOptions allows for additional gateway ServeMuxOptions beyond the default ProtoMessageErrorHandler and MetadataAnnotator from this package
func WithServerAddress ¶
WithServerAddress determines what address the gateway will connect to. By default, the gateway will connect to 0.0.0.0:9090
type ProtoErrorHandler ¶
type ProtoErrorHandler struct {
OutgoingHeaderMatcher runtime.HeaderMatcherFunc
}
ProtoErrorHandler implements runtime.ProtoErrorHandlerFunc in method MessageHandler and ProtoStreamErrorHandlerFunc in method StreamHandler in accordance with REST API Syntax Specification. See RestError for the JSON format of an error
func (*ProtoErrorHandler) MessageHandler ¶
func (h *ProtoErrorHandler) MessageHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, rw http.ResponseWriter, req *http.Request, err error)
MessageHandler implements runtime.ProtoErrorHandlerFunc in accordance with REST API Syntax Specification. See RestError for the JSON format of an error
func (*ProtoErrorHandler) StreamHandler ¶
func (h *ProtoErrorHandler) StreamHandler(ctx context.Context, headerWritten bool, mux *runtime.ServeMux, marshaler runtime.Marshaler, rw http.ResponseWriter, req *http.Request, err error)
StreamHandler implements ProtoStreamErrorHandlerFunc in accordance with REST API Syntax Specification. See RestError for the JSON format of an error
type ProtoStreamErrorHandlerFunc ¶
type ProtoStreamErrorHandlerFunc func(context.Context, bool, *runtime.ServeMux, runtime.Marshaler, http.ResponseWriter, *http.Request, error)
ProtoStreamErrorHandlerFunc handles the error as a gRPC error generated via status package and replies to the testRequest. Addition bool argument indicates whether method (http.ResponseWriter.WriteHeader) was called or not.
func NewProtoStreamErrorHandler ¶
func NewProtoStreamErrorHandler(out runtime.HeaderMatcherFunc) ProtoStreamErrorHandlerFunc
NewProtoStreamErrorHandler returns ProtoStreamErrorHandlerFunc
type ResponseForwarder ¶
type ResponseForwarder struct { OutgoingHeaderMatcher runtime.HeaderMatcherFunc MessageErrHandler runtime.ErrorHandlerFunc StreamErrHandler ProtoStreamErrorHandlerFunc }
ResponseForwarder implements ForwardResponseMessageFunc in method ForwardMessage and ForwardResponseStreamFunc in method ForwardStream in accordance with REST API Syntax Specification. See: https://github.com/infobloxopen/atlas-app-toolkit#responses for format of JSON response.
func (*ResponseForwarder) ForwardMessage ¶
func (fw *ResponseForwarder) ForwardMessage(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, rw http.ResponseWriter, req *http.Request, resp protoreflect.ProtoMessage, opts ...func(context.Context, http.ResponseWriter, protoreflect.ProtoMessage) error)
ForwardMessage implements runtime.ForwardResponseMessageFunc
func (*ResponseForwarder) ForwardStream ¶
func (fw *ResponseForwarder) ForwardStream(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, rw http.ResponseWriter, req *http.Request, recv func() (protoreflect.ProtoMessage, error), opts ...func(context.Context, http.ResponseWriter, protoreflect.ProtoMessage) error)
ForwardStream implements runtime.ForwardResponseStreamFunc. RestStatus comes first in the chuncked result.