http

package
v0.0.0-...-6367170 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2023 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ContentTypeAll is the '*/*' type
	ContentTypeAll = "*/*"
	// ContentTypeForm is the form url encoded
	ContentTypeForm = "application/x-www-form-urlencoded"
	// ContentTypeGRPC represents 'application/grpc' content-type
	ContentTypeGRPCProto = "application/grpc"
	// ContentTypeGRPC represents 'application/grpc' content-type
	ContentTypeGRPCJSON = "application/grpc+json"
	// ContentTypeJSON represents 'application/json' content-type
	ContentTypeJSON = "application/json"
)

Variables

View Source
var (
	// WithFormMarshaler allows marshaling application/x-www-form-urlencoded requests
	WithFormMarshaler = runtime.WithMarshalerOption(ContentTypeForm, &runtime.HTTPBodyMarshaler{
		Marshaler: &FormMarshaler{},
	})

	// WithJSONMarshaler allows marshaling application/json requests
	WithJSONMarshaler = runtime.WithMarshalerOption(ContentTypeJSON, &runtime.HTTPBodyMarshaler{
		Marshaler: &runtime.JSONPb{
			MarshalOptions: protojson.MarshalOptions{
				UseProtoNames: true,
			},
			UnmarshalOptions: protojson.UnmarshalOptions{
				DiscardUnknown: true,
			},
		},
	})
)
View Source
var (
	// HeaderContentType represents a header Content-Type key
	HeaderContentType = textproto.CanonicalMIMEHeaderKey("Content-Type")

	// HeaderAccept represents a header Accept key
	HeaderAccept = textproto.CanonicalMIMEHeaderKey("Accept")
)
View Source
var (
	// WithForwardResponseOption returns a ServeMuxOption representing the forwardResponseOption.
	//
	// forwardResponseOption is an option that will be called on the relevant context.Context,
	// http.ResponseWriter, and proto.Message before every forwarded response.
	//
	// The message may be nil in the case where just a header is being sent.
	WithForwardResponseOption = runtime.WithForwardResponseOption

	// WithIncomingHeaderMatcher returns a ServeMuxOption representing a headerMatcher for incoming request to gateway.
	//
	// This matcher will be called with each header in http.Request. If matcher returns true, that header will be
	// passed to gRPC context. To transform the header before passing to gRPC context, matcher should return modified header.
	WithIncomingHeaderMatcher = runtime.WithIncomingHeaderMatcher

	// WithOutgoingHeaderMatcher returns a ServeMuxOption representing a headerMatcher for outgoing response from gateway.
	//
	// This matcher will be called with each header in response header metadata. If matcher returns true, that header will be
	// passed to http response returned from gateway. To transform the header before passing to response,
	// matcher should return modified header.
	WithOutgoingHeaderMatcher = runtime.WithOutgoingHeaderMatcher

	// WithMetadata returns a ServeMuxOption for passing metadata to a gRPC context.
	//
	// This can be used by services that need to read from http.Request and modify gRPC context. A common use case
	// is reading token from cookie and adding it in gRPC context.
	WithMetadata = runtime.WithMetadata

	// WithErrorHandler returns a ServeMuxOption for passing metadata to a gRPC context.
	//
	// This can be used to handle an error as general proto message defined by gRPC.
	// The response including body and status is not backward compatible with the default error handler.
	// When this option is used, HTTPError and OtherErrorHandler are overwritten on initialization.
	WithErrorHandler = runtime.WithErrorHandler

	// WithDisablePathLengthFallback returns a ServeMuxOption for disable path length fallback.
	WithDisablePathLengthFallback = runtime.WithDisablePathLengthFallback

	// WithStreamErrorHandler returns a ServeMuxOption that will use the given custom stream
	// error handler, which allows for customizing the error trailer for server-streaming
	// calls.
	//
	// For stream errors that occur before any response has been written, the mux's
	// ProtoErrorHandler will be invoked. However, once data has been written, the errors must
	// be handled differently: they must be included in the response body. The response body's
	// final message will include the error details returned by the stream error handler.
	WithStreamErrorHandler = runtime.WithStreamErrorHandler

	// WithMarshaler returns a ServeMuxOption which associates inbound and outbound
	// Marshalers to a MIME type in mux.
	WithMarshaler = runtime.WithMarshalerOption
)
View Source
var (
	// AllIncomingHeaders allows the service to handle all incoming request
	AllIncomingHeaders = runtime.WithIncomingHeaderMatcher(preserve)

	// AllOutgoingHeaders allows the service to handle all incoming request
	AllOutgoingHeaders = runtime.WithOutgoingHeaderMatcher(preserve)

	// WithForwardResponse represents a forward response
	WithForwardResponse = runtime.WithForwardResponseOption(response)
)

Functions

func Accept

func Accept(next http.Handler) http.Handler

Accept prepare the Accept header for underlying requests

func ContentType

func ContentType(next http.Handler) http.Handler

ContentType prepare the Content-Type header for underlying requests

func Match

func Match(body io.Reader) bool

Match matches the request

func Metadata

func Metadata(next http.Handler) http.Handler

Metadata middleware injects some useful headers

func PrepareMediaType

func PrepareMediaType(name string, r *http.Request)

PrepareMediaType prepares a media type header

func SetMediaType

func SetMediaType(name string, r *http.Request)

SetMediaType sets the media type

Types

type ErrorHandlerFunc

type ErrorHandlerFunc = runtime.ErrorHandlerFunc

ErrorHandlerFunc handles the error as a gRPC error generated via status package and replies to the request.

type FormDecoder

type FormDecoder struct {
	Reader io.Reader
}

FormDecoder represents a form decoder

func (*FormDecoder) Decode

func (d *FormDecoder) Decode(v interface{}) error

Decode decodes the request

type FormMarshaler

type FormMarshaler struct{}

FormMarshaler defines a conversion between byte sequence and gRPC payloads / fields.

func (*FormMarshaler) ContentType

func (m *FormMarshaler) ContentType(_ interface{}) string

ContentType returns the Content-Type which this marshaler is responsible for.

func (*FormMarshaler) Marshal

func (m *FormMarshaler) Marshal(v interface{}) ([]byte, error)

Marshal marshals "v" into byte sequence.

func (*FormMarshaler) NewDecoder

func (m *FormMarshaler) NewDecoder(r io.Reader) runtime.Decoder

NewDecoder returns a Decoder which reads byte sequence from "r".

func (*FormMarshaler) NewEncoder

func (m *FormMarshaler) NewEncoder(w io.Writer) runtime.Encoder

NewEncoder returns an Encoder which writes bytes sequence into "w".

func (*FormMarshaler) Unmarshal

func (m *FormMarshaler) Unmarshal(data []byte, v interface{}) error

Unmarshal unmarshals "data" into "v". "v" must be a pointer value.

type HeaderMatcherFunc

type HeaderMatcherFunc = runtime.HeaderMatcherFunc

HeaderMatcherFunc checks whether a header key should be forwarded to/from gRPC context.

type Proxy

type Proxy struct {
	// contains filtered or unexported fields
}

Proxy represents a http proxy to the server

func NewProxy

func NewProxy(opts ...ServeMuxOption) *Proxy

NewProxy creates a new http proxy

func (*Proxy) OnError

func (proxy *Proxy) OnError(fn runtime.ErrorHandlerFunc)

OnError handles response errors

func (*Proxy) Router

func (proxy *Proxy) Router() chi.Router

Router returns the underlying router

func (*Proxy) Serve

func (proxy *Proxy) Serve(mux cmux.CMux) error

Serve serves the mux

func (*Proxy) Shutdown

func (proxy *Proxy) Shutdown(ctx context.Context) error

Shutdown shutdowns the server

func (*Proxy) Use

func (proxy *Proxy) Use(fn ProxyHandlerFunc)

Use registers the proxy handler

func (*Proxy) UseDialOption

func (proxy *Proxy) UseDialOption(opt grpc.DialOption)

UseDialOption uses specific dial option

func (*Proxy) WithErrorHandler

func (proxy *Proxy) WithErrorHandler() runtime.ServeMuxOption

WithErrorHandler creates a error handler proxy

type ProxyHandlerFunc

type ProxyHandlerFunc = func(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error

ProxyHandlerFunc handles the proxy call

type ServeMuxOption

type ServeMuxOption = runtime.ServeMuxOption

ServeMuxOption is an option that can be given to a ServeMux on construction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL