grpc

package
v1.1.68 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2022 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GinLogFormatter = func(param gin.LogFormatterParams) string {
	var statusColor, methodColor, resetColor string
	if param.IsOutputColor() {
		statusColor = param.StatusCodeColor()
		methodColor = param.MethodColor()
		resetColor = param.ResetColor()
	}

	if param.Latency > time.Minute {

		param.Latency = param.Latency - param.Latency%time.Second
	}
	span := trace.SpanFromContext(param.Request.Context())

	var traceId string
	if span.SpanContext().HasTraceID() {
		traceId = span.SpanContext().TraceID().String()
	}

	return fmt.Sprintf("[gRPC-Gateway] %v |%s %3d %s| %13v | %15s |%s %-7s %s %#v\n%s",
		traceId,
		statusColor, param.StatusCode, resetColor,
		param.Latency,
		param.ClientIP,
		methodColor, param.Method, resetColor,
		param.Path,
		param.ErrorMessage,
	)
}

Functions

func ListenAndServe

func ListenAndServe(addr string, handler http.Handler, opts ...GatewayOption) error

ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

The handler is typically nil, in which case the DefaultServeMux is used.

ListenAndServe always returns a non-nil error.

func ListenAndServeTLS

func ListenAndServeTLS(addr, certFile, keyFile string, handler http.Handler, opts ...GatewayOption) error

ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

func MessageProducerWithForward added in v1.1.37

func MessageProducerWithForward(ctx context.Context, format string, level logrus.Level, code codes.Code, err error, fields logrus.Fields)

MessageProducerWithForward fill "X-Forwarded-For" and "X-Forwarded-Host" to record http callers

func Serve

func Serve(l net.Listener, handler http.Handler, opts ...GatewayOption) error

Serve accepts incoming HTTP connections on the listener l, creating a new service goroutine for each. The service goroutines read requests and then call handler to reply to them.

The handler is typically nil, in which case the DefaultServeMux is used.

HTTP/2 support is only enabled if the Listener returns *tls.Conn connections and they were configured with "h2" in the TLS Config.NextProtos.

Serve always returns a non-nil error.

func ServeTLS

func ServeTLS(l net.Listener, handler http.Handler, certFile, keyFile string, opts ...GatewayOption) error

ServeTLS accepts incoming HTTPS connections on the listener l, creating a new service goroutine for each. The service goroutines read requests and then call handler to reply to them.

The handler is typically nil, in which case the DefaultServeMux is used.

Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

ServeTLS always returns a non-nil error.

Types

type EmptyGatewayOption

type EmptyGatewayOption struct{}

EmptyGatewayOption does not alter the configuration. It can be embedded in another structure to build custom options.

This API is EXPERIMENTAL.

type ForwardResponseMessageHandler

type ForwardResponseMessageHandler interface {
	ForwardResponseMessage(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, req *http.Request, resp proto.Message, opts ...func(context.Context, http.ResponseWriter, proto.Message) error)
}

type ForwardResponseMessageHandlerFunc

type ForwardResponseMessageHandlerFunc func(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, req *http.Request, resp proto.Message, opts ...func(context.Context, http.ResponseWriter, proto.Message) error)

func (ForwardResponseMessageHandlerFunc) ForwardResponseMessage

type GRPCHandler

type GRPCHandler interface {
	Register(srv *grpc.Server)
}

type GRPCHandlerFunc

type GRPCHandlerFunc func(srv *grpc.Server)

func (GRPCHandlerFunc) Register

func (f GRPCHandlerFunc) Register(srv *grpc.Server)

type Gateway

type Gateway struct {
	http.Server

	Handler http.Handler
	// contains filtered or unexported fields
}

func NewGateway

func NewGateway(addr string, opts ...GatewayOption) *Gateway

func NewGatewayTLS

func NewGatewayTLS(addr string, tlsConfig *tls.Config, opts ...GatewayOption) *Gateway

NewGatewayTLS TLSConfig optionally provides a TLS configuration for use by ServeTLS and ListenAndServeTLS. Note that this value is cloned by ServeTLS and ListenAndServeTLS, so it's not possible to modify the configuration with methods like tls.Config.SetSessionTicketKeys. To use SetSessionTicketKeys, use Server.Serve with a TLS Listener instead.

func (*Gateway) ApplyOptions

func (gateway *Gateway) ApplyOptions(options ...GatewayOption) *Gateway

func (*Gateway) ListenAndServe

func (gateway *Gateway) ListenAndServe() error

ListenAndServe listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

If srv.Addr is blank, ":http" is used.

ListenAndServe always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.

func (*Gateway) ListenAndServeTLS

func (gateway *Gateway) ListenAndServeTLS(certFile, keyFile string) error

ListenAndServeTLS listens on the TCP network address srv.Addr and then calls ServeTLS to handle requests on incoming TLS connections. Accepted connections are configured to enable TCP keep-alives.

Filenames containing a certificate and matching private key for the server must be provided if neither the Server's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

If srv.Addr is blank, ":https" is used.

ListenAndServeTLS always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.

func (*Gateway) RegisterGRPCFunc

func (gateway *Gateway) RegisterGRPCFunc(handler func(srv *grpc.Server))

RegisterGRPCFunc registers grpc handler of the gateway

func (*Gateway) RegisterGRPCHandler

func (gateway *Gateway) RegisterGRPCHandler(handler GRPCHandler)

RegisterGRPCHandler registers grpc handler of the gateway

func (*Gateway) RegisterHTTPFunc

func (gateway *Gateway) RegisterHTTPFunc(ctx context.Context, handler func(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error) error

RegisterHTTPFunc registers http handler of the gateway

func (*Gateway) RegisterHTTPHandler

func (gateway *Gateway) RegisterHTTPHandler(ctx context.Context, handler HTTPHandler) error

RegisterHTTPHandler registers http handler of the gateway

func (*Gateway) Serve

func (gateway *Gateway) Serve(l net.Listener) error

func (*Gateway) ServeTLS

func (gateway *Gateway) ServeTLS(l net.Listener, certFile, keyFile string) error

ServeTLS accepts incoming connections on the Listener l, creating a new service goroutine for each. The service goroutines perform TLS setup and then read requests, calling srv.Handler to reply to them.

Files containing a certificate and matching private key for the server must be provided if neither the Server's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

ServeTLS always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.

type GatewayOption

type GatewayOption interface {
	// contains filtered or unexported methods
}

A GatewayOption sets options.

func WithDefault added in v0.0.99

func WithDefault() []GatewayOption

func WithDefaultMarsherOption

func WithDefaultMarsherOption() []GatewayOption

func WithGrpcDialOption

func WithGrpcDialOption(opts ...grpc.DialOption) GatewayOption

func WithGrpcReflectionService added in v0.0.99

func WithGrpcReflectionService(autoRegistered bool) GatewayOption

func WithGrpcServeMuxOption

func WithGrpcServeMuxOption(opts ...runtime.ServeMuxOption) GatewayOption

func WithGrpcServerOption

func WithGrpcServerOption(opts ...grpc.ServerOption) GatewayOption

func WithGrpcStreamClientChain added in v1.0.159

func WithGrpcStreamClientChain(interceptors ...grpc.StreamClientInterceptor) GatewayOption

func WithGrpcStreamServerChain

func WithGrpcStreamServerChain(interceptors ...grpc.StreamServerInterceptor) GatewayOption

func WithGrpcUnaryClientChain added in v1.0.159

func WithGrpcUnaryClientChain(interceptors ...grpc.UnaryClientInterceptor) GatewayOption

func WithGrpcUnaryServerChain

func WithGrpcUnaryServerChain(interceptors ...grpc.UnaryServerInterceptor) GatewayOption

func WithHTTPErrorHandler

func WithHTTPErrorHandler(fn HTTPErrorHandler) GatewayOption

WithHTTPErrorHandler replies to the request with the error. You can set a custom function to this variable to customize error format.

func WithHttpAfterCompletion

func WithHttpAfterCompletion(
	afterCompletion func(w http.ResponseWriter, r *http.Request, err interface{})) GatewayOption

func WithHttpHandlerInterceptor

func WithHttpHandlerInterceptor(opts ...http_.HandlerInterceptorChainOption) GatewayOption

func WithHttpPostHandler

func WithHttpPostHandler(
	postHandle func(w http.ResponseWriter, r *http.Request)) GatewayOption

func WithHttpPreHandler

func WithHttpPreHandler(preHandle func(w http.ResponseWriter, r *http.Request) error) GatewayOption

func WithHttpRewriter deprecated

func WithHttpRewriter(rewriter func(w http.ResponseWriter, r *http.Request) error) GatewayOption

Deprecated: Use WithHttpPreHandler instead.

func WithHttpWrapper

func WithHttpWrapper(wrapper func(http.Handler) http.Handler) GatewayOption

WithHttpWrapper is a decorator or middleware of http.Handler

func WithLogrusLogger

func WithLogrusLogger(logger *logrus.Logger) GatewayOption

func WithLogrusLoggerConfig added in v1.1.65

func WithLogrusLoggerConfig(logger *logrus.Logger, conf gin.LoggerConfig) GatewayOption

func WithMarshalerOption

func WithMarshalerOption(mime string, marshaler runtime.Marshaler) GatewayOption

type GatewayOptionFunc

type GatewayOptionFunc func(*Gateway)

GatewayOptionFunc wraps a function that modifies Gateway into an implementation of the GatewayOption interface.

type HTTPErrorHandler

type HTTPErrorHandler interface {
	HandleHTTPError(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error)
}

type HTTPErrorHandlerFunc

type HTTPErrorHandlerFunc func(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error)

func (HTTPErrorHandlerFunc) HandleHTTPError

func (f HTTPErrorHandlerFunc) HandleHTTPError(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error)

type HTTPHandler

type HTTPHandler interface {
	Register(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error
}

type HTTPHandlerFunc

type HTTPHandlerFunc func(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error

func (HTTPHandlerFunc) Register

func (f HTTPHandlerFunc) Register(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error

Jump to

Keyboard shortcuts

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