gateway

package
v0.0.0-...-585f190 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2024 License: Apache-2.0 Imports: 59 Imported by: 0

Documentation

Index

Constants

View Source
const (
	XRequestID  = "x-request-id"
	XUID        = "x-uid"
	XAccessKey  = "x-access-key"
	XHttpMethod = "x-http-method"
	XRemoteAddr = "x-http-forwarded-for"
	XProtocol   = "x-http-protocol"
	XHttpURI    = "x-http-uri"
	XIdentity   = "x-identity"
)
View Source
const ClientStreamContentType = "application/begonia-client-stream"
View Source
const GatewayXParams = "x-gateway-params"

Variables

Functions

func ConvertDynamicMessageToHttpBody

func ConvertDynamicMessageToHttpBody(dynamicMessage *dynamicpb.Message) (*httpbody.HttpBody, error)
func (m *RawBinaryUnmarshaler) NewDecoder(r io.Reader) runtime.Decoder {
	return &BinaryDecoder{"Data", r}
}

func HandleErrorWithLogger

func HandleErrorWithLogger(logger logger.Logger) runtime.ErrorHandlerFunc

func HttpResponseBodyModify

func HttpResponseBodyModify(ctx context.Context, w http.ResponseWriter, msg proto.Message) error

func IncomingHeadersToMetadata

func IncomingHeadersToMetadata(ctx context.Context, req *http.Request) metadata.MD

func JSONCamelCase

func JSONCamelCase(s string) string

func NewGrpcConnPool

func NewGrpcConnPool(addr string, poolOpt ...loadbalance.PoolOptionsBuildOption) loadbalance.Pool

NewGrpcConnPool 创建一个grpc连接池

func NewGrpcEndpoint

func NewGrpcEndpoint(addr string, pool loadbalance.Pool) loadbalance.Endpoint

func NewGrpcServer

func NewGrpcServer(opts *GrpcServerOptions, lb *GrpcLoadBalancer) *grpc.Server

func NewJsonDecoder

func NewJsonDecoder(r io.Reader) runtime.Decoder

func NewLoadBalanceEndpoint

func NewLoadBalanceEndpoint(lb loadbalance.BalanceType, endpoints []*api.EndpointMeta) ([]loadbalance.Endpoint, error)

func NewMaskDecoder

func NewMaskDecoder(dec runtime.Decoder) *maskDecoder

func SetUpdateMaskFields

func SetUpdateMaskFields(message protoreflect.ProtoMessage, fields []string)

func UrlQueryToProtoMessageField

func UrlQueryToProtoMessageField(pb proto.Message, value url.Values) error

Types

type BinaryDecoder

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

func (*BinaryDecoder) Decode

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

type ClientSideStream

type ClientSideStream interface {
	Send(protoreflect.ProtoMessage) error
	CloseAndRecv() (protoreflect.ProtoMessage, error)
	grpc.ClientStream
}

type CorsHandler

type CorsHandler struct {
	Cors []string
}

func (*CorsHandler) Handle

func (cors *CorsHandler) Handle(h http.Handler) http.Handler

type EndpointServer

type EndpointServer struct {
	Addr   string
	Weight int
}

type EventSourceMarshaler

type EventSourceMarshaler struct {
	JSONMarshaler
}

func NewEventSourceMarshaler

func NewEventSourceMarshaler() *EventSourceMarshaler

func (*EventSourceMarshaler) ContentType

func (m *EventSourceMarshaler) ContentType(v interface{}) string

func (*EventSourceMarshaler) Marshal

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

type Exception

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

func NewException

func NewException(log logger.Logger) *Exception

func (*Exception) Name

func (e *Exception) Name() string

func (*Exception) Priority

func (e *Exception) Priority() int

func (*Exception) SetPriority

func (e *Exception) SetPriority(priority int)

func (*Exception) StreamInterceptor

func (e *Exception) StreamInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error)

func (*Exception) UnaryInterceptor

func (e *Exception) UnaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error)

type FormDataDecoder

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

func (*FormDataDecoder) Decode

func (f *FormDataDecoder) Decode(v interface{}) error

func (*FormDataDecoder) SetBoundary

func (f *FormDataDecoder) SetBoundary(boundary string)

type FormDataMarshaler

type FormDataMarshaler struct {
	runtime.JSONPb
}

func NewFormDataMarshaler

func NewFormDataMarshaler() *FormDataMarshaler

func (*FormDataMarshaler) ContentType

func (f *FormDataMarshaler) ContentType(v interface{}) string

func (*FormDataMarshaler) NewDecoder

func (f *FormDataMarshaler) NewDecoder(r io.Reader) runtime.Decoder

type FormUrlEncodedDecoder

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

func (*FormUrlEncodedDecoder) Decode

func (f *FormUrlEncodedDecoder) Decode(v interface{}) error

type FormUrlEncodedMarshaler

type FormUrlEncodedMarshaler struct {
	runtime.JSONPb
}

func NewFormUrlEncodedMarshaler

func NewFormUrlEncodedMarshaler() *FormUrlEncodedMarshaler

func (*FormUrlEncodedMarshaler) ContentType

func (f *FormUrlEncodedMarshaler) ContentType(v interface{}) string

func (*FormUrlEncodedMarshaler) NewDecoder

func (f *FormUrlEncodedMarshaler) NewDecoder(r io.Reader) runtime.Decoder

type FormatDataDecoder

type FormatDataDecoder interface {
	SetBoundary(string)
}

type GatewayConfig

type GatewayConfig struct {
	GatewayAddr   string
	GrpcProxyAddr string
}

type GatewayServer

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

func Get

func Get() *GatewayServer

func New

func NewGateway

func NewGateway(cfg *GatewayConfig, opts *GrpcServerOptions) *GatewayServer

func (*GatewayServer) DeleteHandlerClient

func (g *GatewayServer) DeleteHandlerClient(ctx context.Context, pd ProtobufDescription) error

func (*GatewayServer) DeleteLoadBalance

func (g *GatewayServer) DeleteLoadBalance(pd ProtobufDescription)

func (*GatewayServer) DeleteLocalService

func (g *GatewayServer) DeleteLocalService(pd ProtobufDescription)

func (*GatewayServer) GetLoadbalanceName

func (g *GatewayServer) GetLoadbalanceName() loadbalance.BalanceType

func (*GatewayServer) GetOptions

func (g *GatewayServer) GetOptions() *GrpcServerOptions

func (*GatewayServer) RegisterHandlerClient

func (g *GatewayServer) RegisterHandlerClient(ctx context.Context, pd ProtobufDescription) error

func (*GatewayServer) RegisterLocalService

func (g *GatewayServer) RegisterLocalService(ctx context.Context, pd ProtobufDescription, sd *grpc.ServiceDesc, ss any) error

func (*GatewayServer) RegisterService

func (*GatewayServer) Start

func (g *GatewayServer) Start()

func (*GatewayServer) UpdateLoadbalance

func (g *GatewayServer) UpdateLoadbalance(pd ProtobufDescription, lb loadbalance.LoadBalance)

type GrpcLoadBalancer

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

func NewGrpcLoadBalancer

func NewGrpcLoadBalancer() *GrpcLoadBalancer

func (*GrpcLoadBalancer) Delete

func (g *GrpcLoadBalancer) Delete(pd ProtobufDescription)

func (*GrpcLoadBalancer) Name

func (*GrpcLoadBalancer) Register

func (*GrpcLoadBalancer) Select

func (g *GrpcLoadBalancer) Select(method string, args ...interface{}) (loadbalance.Endpoint, error)

type GrpcProxy

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

func NewGrpcProxy

func NewGrpcProxy(lb *GrpcLoadBalancer, middlewares ...GrpcProxyMiddleware) *GrpcProxy

func (*GrpcProxy) Handler

func (g *GrpcProxy) Handler(srv interface{}, serverStream grpc.ServerStream) error

type GrpcProxyMiddleware

type GrpcProxyMiddleware func(srv interface{}, serverStream grpc.ServerStream) error

type GrpcRequest

type GrpcRequest interface {
	GetContext() context.Context

	// TODO:with gateway
	GetMarshaler() runtime.Marshaler
	GetReq() *http.Request
	GetPathParams() map[string]string

	GetIn() proto.Message
	GetOut() proto.Message
	GetOutType() protoreflect.MessageDescriptor
	GetInType() protoreflect.MessageDescriptor
	GetFullMethodName() string
	GetCallOptions() []grpc.CallOption
}

func NewGrpcRequest

func NewGrpcRequest(ctx context.Context, inType protoreflect.MessageDescriptor, outType protoreflect.MessageDescriptor, fullMethodName string, opts ...GrpcRequestOptions) GrpcRequest

type GrpcRequestImpl

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

func (*GrpcRequestImpl) GetCallOptions

func (g *GrpcRequestImpl) GetCallOptions() []grpc.CallOption

func (*GrpcRequestImpl) GetContext

func (g *GrpcRequestImpl) GetContext() context.Context

func (*GrpcRequestImpl) GetFullMethodName

func (g *GrpcRequestImpl) GetFullMethodName() string

func (*GrpcRequestImpl) GetIn

func (g *GrpcRequestImpl) GetIn() proto.Message

func (*GrpcRequestImpl) GetInType

func (*GrpcRequestImpl) GetMarshaler

func (g *GrpcRequestImpl) GetMarshaler() runtime.Marshaler

func (*GrpcRequestImpl) GetOut

func (g *GrpcRequestImpl) GetOut() proto.Message

func (*GrpcRequestImpl) GetOutType

func (*GrpcRequestImpl) GetPathParams

func (g *GrpcRequestImpl) GetPathParams() map[string]string

func (*GrpcRequestImpl) GetReq

func (g *GrpcRequestImpl) GetReq() *http.Request

type GrpcRequestOptions

type GrpcRequestOptions func(req *GrpcRequestImpl)

func WithGatewayCallOptions

func WithGatewayCallOptions(options ...grpc.CallOption) GrpcRequestOptions

func WithGatewayMarshaler

func WithGatewayMarshaler(m runtime.Marshaler) GrpcRequestOptions

func WithGatewayPathParams

func WithGatewayPathParams(pathParams map[string]string) GrpcRequestOptions

func WithGatewayReq

func WithGatewayReq(r *http.Request) GrpcRequestOptions

func WithIn

func WithIn(in proto.Message) GrpcRequestOptions

func WithOut

func WithOut(out proto.Message) GrpcRequestOptions

type GrpcServerOptions

type GrpcServerOptions struct {
	Middlewares     []GrpcProxyMiddleware
	Options         []grpc.ServerOption
	PoolOptions     []loadbalance.PoolOptionsBuildOption
	HttpMiddlewares []runtime.ServeMuxOption
	HttpHandlers    []func(http.Handler) http.Handler
}

type HttpEndpoint

type HttpEndpoint interface {
	RegisterHandlerClient(ctx context.Context, pd ProtobufDescription, mux *runtime.ServeMux) error
	DeleteEndpoint(ctx context.Context, pd ProtobufDescription, mux *runtime.ServeMux) error
}

func NewHttpEndpoint

func NewHttpEndpoint(client HttpForwardGrpcEndpoint) (HttpEndpoint, error)

func NewHttpServer

func NewHttpServer(addr string, poolOpt ...loadbalance.PoolOptionsBuildOption) (HttpEndpoint, error)

type HttpEndpointImpl

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

func (*HttpEndpointImpl) DeleteEndpoint

func (h *HttpEndpointImpl) DeleteEndpoint(ctx context.Context, pd ProtobufDescription, mux *runtime.ServeMux) error

func (*HttpEndpointImpl) NotFound

func (h *HttpEndpointImpl) NotFound(w http.ResponseWriter, r *http.Request, pathParams map[string]string)

func (*HttpEndpointImpl) RegisterHandlerClient

func (h *HttpEndpointImpl) RegisterHandlerClient(ctx context.Context, pd ProtobufDescription, mux *runtime.ServeMux) error

type HttpEndpointItem

type HttpEndpointItem struct {
	Pattern  runtime.Pattern `json:"-"`
	Template *Template

	HttpMethod     string
	FullMethodName string
	HttpUri        string
	PathParams     []string
	In             protoreflect.MessageDescriptor
	Out            protoreflect.MessageDescriptor
	IsClientStream bool
	IsServerStream bool
	InName         string
	OutName        string
	Pkg            string
	InPkg          string
	OutPkg         string
	HttpResponse   string `json:"http_response"`
}

type HttpForwardGrpcEndpoint

type HttpForwardGrpcEndpoint interface {
	Request(req GrpcRequest) (proto.Message, runtime.ServerMetadata, error)
	ServerSideStream(req GrpcRequest) (ServerSideStream, error)
	ClientSideStream(req GrpcRequest) (ClientSideStream, error)
	Stream(req GrpcRequest) (StreamClient, error)
}

func NewEndpoint

func NewEndpoint(pool loadbalance.Pool) HttpForwardGrpcEndpoint

type HttpProtobufStream

type HttpProtobufStream interface {
	runtime.Marshaler
}

ProtobufWithLengthPrefix 是一个编码器,它将消息编码为长度前缀的字节切片 方便stream处理,不需要使用分隔符,将连续的消息流转换为字节流

func NewProtobufWithLengthPrefix

func NewProtobufWithLengthPrefix() HttpProtobufStream

type HttpProtobufStreamImpl

type HttpProtobufStreamImpl struct {
	*runtime.ProtoMarshaller
}

func (*HttpProtobufStreamImpl) ContentType

func (p *HttpProtobufStreamImpl) ContentType(v interface{}) string

func (*HttpProtobufStreamImpl) Marshal

func (*HttpProtobufStreamImpl) Marshal(value interface{}) ([]byte, error)

Marshal 将消息编码为长度前缀的字节切片

func (*HttpProtobufStreamImpl) NewDecoder

func (p *HttpProtobufStreamImpl) NewDecoder(reader io.Reader) runtime.Decoder

func (*HttpProtobufStreamImpl) NewEncoder

func (p *HttpProtobufStreamImpl) NewEncoder(writer io.Writer) runtime.Encoder

func (*HttpProtobufStreamImpl) Unmarshal

func (p *HttpProtobufStreamImpl) Unmarshal(data []byte, value interface{}) error

Unmarshal 将消息解码为长度前缀的字节切片

type JSONMarshaler

type JSONMarshaler struct {
	runtime.JSONPb
}

func NewJSONMarshaler

func NewJSONMarshaler() *JSONMarshaler

func (*JSONMarshaler) ContentType

func (m *JSONMarshaler) ContentType(v interface{}) string

func (*JSONMarshaler) Marshal

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

type LengthPrefixMarshalerOption

type LengthPrefixMarshalerOption func(cxt context.Context, message proto.Message) error

type LengthPrefixUnmarshalerOption

type LengthPrefixUnmarshalerOption func(cxt context.Context, data []byte) error

type LoggerImpl

type LoggerImpl struct {
	*logrus.Entry
}

func (*LoggerImpl) Debug

func (l *LoggerImpl) Debug(ctx context.Context, args ...interface{})

func (*LoggerImpl) Debugf

func (l *LoggerImpl) Debugf(ctx context.Context, format string, args ...interface{})

func (*LoggerImpl) Error

func (l *LoggerImpl) Error(ctx context.Context, err error)

func (*LoggerImpl) Errorf

func (l *LoggerImpl) Errorf(ctx context.Context, format string, args ...interface{})

func (*LoggerImpl) Info

func (l *LoggerImpl) Info(ctx context.Context, args ...interface{})

func (*LoggerImpl) Infof

func (l *LoggerImpl) Infof(ctx context.Context, format string, args ...interface{})

func (*LoggerImpl) Logurs

func (l *LoggerImpl) Logurs() *logrus.Logger

func (*LoggerImpl) SetReportCaller

func (l *LoggerImpl) SetReportCaller(reportCaller bool)

func (*LoggerImpl) Warn

func (l *LoggerImpl) Warn(ctx context.Context, args ...interface{})

func (*LoggerImpl) Warnf

func (l *LoggerImpl) Warnf(ctx context.Context, format string, args ...interface{})

func (*LoggerImpl) WithField

func (l *LoggerImpl) WithField(key string, value interface{}) logger.Logger

func (*LoggerImpl) WithFields

func (l *LoggerImpl) WithFields(fields logrus.Fields) logger.Logger

type LoggerMiddleware

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

func NewLoggerMiddleware

func NewLoggerMiddleware(log logger.Logger) *LoggerMiddleware

func (*LoggerMiddleware) Name

func (log *LoggerMiddleware) Name() string

func (*LoggerMiddleware) Priority

func (log *LoggerMiddleware) Priority() int

func (*LoggerMiddleware) SetPriority

func (log *LoggerMiddleware) SetPriority(priority int)

func (*LoggerMiddleware) StreamInterceptor

func (log *LoggerMiddleware) StreamInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error)

func (*LoggerMiddleware) UnaryInterceptor

func (log *LoggerMiddleware) UnaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (rsp interface{}, err error)

type ProtobufDescription

type ProtobufDescription interface {
	GetFileDescriptorSet() *descriptorpb.FileDescriptorSet
	GetMessageTypeByName(pkg string, name string) protoreflect.MessageDescriptor
	GetGatewayJsonSchema() string
	SetHttpResponse(option protoreflect.ExtensionType) error
	GetMessageTypeByFullName(fullName string) protoreflect.MessageDescriptor
	GetDescription() []byte
}

func NewDescription

func NewDescription(dir string) (ProtobufDescription, error)

func NewDescriptionFromBinary

func NewDescriptionFromBinary(data []byte, outDir string) (ProtobufDescription, error)

type RawBinaryUnmarshaler

type RawBinaryUnmarshaler runtime.HTTPBodyMarshaler

func NewRawBinaryUnmarshaler

func NewRawBinaryUnmarshaler() *RawBinaryUnmarshaler

func (*RawBinaryUnmarshaler) ContentType

func (m *RawBinaryUnmarshaler) ContentType(v interface{}) string

func (*RawBinaryUnmarshaler) Marshal

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

func (*RawBinaryUnmarshaler) NewDecoder

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

type ServerSideStream

type ServerSideStream interface {
	Recv() (protoreflect.ProtoMessage, error)
	grpc.ClientStream
}

type StreamClient

type StreamClient interface {
	Send(protoreflect.ProtoMessage) error
	Recv() (protoreflect.ProtoMessage, error)
	grpc.ClientStream
}

type Template

type Template struct {
	// Version is the version number of the format.
	Version int
	// OpCodes is a sequence of operations.
	OpCodes []int
	// Pool is a constant pool
	Pool []string
	// Verb is a VERB part in the template.
	Verb string
	// Fields is a list of field paths bound in this template.
	Fields []string
	// Original template (example: /v1/a_bit_of_everything)
	Template string
}

type WebsocketForwarder

type WebsocketForwarder interface {
	http.ResponseWriter
	// Read() ([]byte, error)
	Write([]byte) (int, error)
	Close() error
	CloseConn() error
	NextReader() (io.Reader, error)
}

func NewWebsocketForwarder

func NewWebsocketForwarder(w http.ResponseWriter, req *http.Request, responseType int) (WebsocketForwarder, error)

Jump to

Keyboard shortcuts

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