rkgrpc

package
v2.2.22 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: Apache-2.0 Imports: 64 Imported by: 1

Documentation

Overview

Package rkgrpc an implementation of rkentry.Entry which could be used start restful server with grpc framework

Experimental. This is used as grpc proxy server which forwarding grpc request to backend grpc server if not implemented. Currently, grpc-gateway and grpcurl is not supported. The grpc client called from code is supported.

Index

Constants

View Source
const (
	// ProxyEntryType default entry type
	ProxyEntryType = "ProxyEntry"
	// ProxyEntryNameDefault default entry name
	ProxyEntryNameDefault = "ProxyDefault"
	// HeaderBased header based proxy pattern
	HeaderBased = "headerBased"
	// PathBased grpc path(method) based proxy pattern
	PathBased = "pathBased"
	// IpBased remote IP based proxy pattern
	IpBased = "ipBased"
)
View Source
const (
	// GrpcEntryType default entry type
	GrpcEntryType = "gRPCEntry"
)

Variables

This section is empty.

Functions

func Codec

func Codec() encoding.Codec

Codec returns a proxying grpc.Codec with the default protobuf codec as parent.

See CodecWithParent.

func CodecWithFallback

func CodecWithFallback(fallback encoding.Codec) encoding.Codec

CodecWithFallback returns a proxying grpc.Codec with a user provided codec as parent.

This codec is *crucial* to the functioning of the proxy. It allows the proxy server to be oblivious to the schema of the forwarded messages. It basically treats a gRPC message frame as raw bytes. However, if the server handler, or the client caller are not proxy-internal functions it will fall back to trying to decode the message using a fallback codec.

func HttpErrorHandler

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

HttpErrorHandler Mainly copies from runtime.DefaultHTTPErrorHandler. We reformat error response with rkerror.ErrorResp.

func IncomingHeaderMatcher

func IncomingHeaderMatcher(key string) (string, bool)

IncomingHeaderMatcher Pass out all metadata in http header to grpc metadata.

func NewRkGwServerMuxOptions

func NewRkGwServerMuxOptions(mOptIn *protojson.MarshalOptions, uOptIn *protojson.UnmarshalOptions) []runtime.ServeMuxOption

NewRkGwServerMuxOptions creates new gw server mux options.

func NewRule

func NewRule(opts ...ruleOption) *rule

NewRule create a new proxy rules with options.

func OutgoingHeaderMatcher

func OutgoingHeaderMatcher(key string) (string, bool)

OutgoingHeaderMatcher Pass out all metadata in grpc to http header.

func RegisterGrpcEntryYAML

func RegisterGrpcEntryYAML(raw []byte) map[string]rkentry.Entry

RegisterGrpcEntryYAML Register grpc entries with provided config file (Must YAML file).

Currently, support two ways to provide config file path. 1: With function parameters 2: With command line flag "--rkboot" described in rkentry.BootConfigPathFlagKey (Will override function parameter if exists) Command line flag has high priority which would override function parameter

Error handling: Process will shutdown if any errors occur with rkentry.ShutdownWithError function

Override elements in config file: We learned from HELM source code which would override elements in YAML file with "--set" flag followed with comma separated key/value pairs.

We are using "--rkset" described in rkentry.BootConfigOverrideKey in order to distinguish with user flags Example of common usage: ./binary_file --rkset "key1=val1,key2=val2" Example of nested map: ./binary_file --rkset "outer.inner.key=val" Example of slice: ./binary_file --rkset "outer[0].key=val"

func ToAllowOriginFunc added in v2.2.9

func ToAllowOriginFunc(allowOrigins []string) grpcweb.Option

func ToGrpcWebOptions added in v2.2.9

func ToGrpcWebOptions(conf *BootConfigGrpcWeb) []grpcweb.Option

func TransparentHandler

func TransparentHandler(director Director) grpc.StreamHandler

TransparentHandler returns a handler that attempts to proxy all requests that are not registered in the server. The indented use here is as a transparent proxy, where the server doesn't know about the services implemented by the backends. It should be used as a `grpc.UnknownServiceHandler`.

This can *only* be used if the `server` also uses grpcproxy.CodecForServer() ServerOption.

func WithHeaderPatterns

func WithHeaderPatterns(pattern ...*HeaderPattern) ruleOption

WithHeaderPatterns provide header based patterns.

func WithIpPatterns

func WithIpPatterns(pattern ...*IpPattern) ruleOption

WithIpPatterns provide IP based patterns.

func WithPathPatterns

func WithPathPatterns(pattern ...*PathPattern) ruleOption

WithPathPatterns provide path based patterns.

Types

type BootConfig

type BootConfig struct {
	Grpc []struct {
		Name               string                        `yaml:"name" json:"name"`
		Description        string                        `yaml:"description" json:"description"`
		Port               uint64                        `yaml:"port" json:"port"`
		GwPort             uint64                        `yaml:"gwPort" json:"gwPort"`
		Enabled            bool                          `yaml:"enabled" json:"enabled"`
		EnableReflection   bool                          `yaml:"enableReflection" json:"enableReflection"`
		NoRecvMsgSizeLimit bool                          `yaml:"noRecvMsgSizeLimit" json:"noRecvMsgSizeLimit"`
		CommonService      rkentry.BootCommonService     `yaml:"commonService" json:"commonService"`
		SW                 rkentry.BootSW                `yaml:"sw" json:"sw"`
		Docs               rkentry.BootDocs              `yaml:"docs" json:"docs"`
		Prom               rkentry.BootProm              `yaml:"prom" json:"prom"`
		Static             rkentry.BootStaticFileHandler `yaml:"static" json:"static"`
		Proxy              BootConfigProxy               `yaml:"proxy" json:"proxy"`
		GrpcWeb            BootConfigGrpcWeb             `yaml:"grpcWeb" json:"grpcWeb"`
		CertEntry          string                        `yaml:"certEntry" json:"certEntry"`
		LoggerEntry        string                        `yaml:"loggerEntry" json:"loggerEntry"`
		EventEntry         string                        `yaml:"eventEntry" json:"eventEntry"`
		PProf              rkentry.BootPProf             `yaml:"pprof" json:"pprof"`
		EnableRkGwOption   bool                          `yaml:"enableRkGwOption" json:"enableRkGwOption"`
		GwOption           *gwOption                     `yaml:"gwOption" json:"gwOption"`
		Middleware         struct {
			Ignore     []string                `yaml:"ignore" json:"ignore"`
			ErrorModel string                  `yaml:"errorModel" json:"errorModel"`
			Logging    rkmidlog.BootConfig     `yaml:"logging" json:"logging"`
			Prom       rkmidprom.BootConfig    `yaml:"prom" json:"prom"`
			Auth       rkmidauth.BootConfig    `yaml:"auth" json:"auth"`
			Cors       rkmidcors.BootConfig    `yaml:"cors" json:"cors"`
			Secure     rkmidsec.BootConfig     `yaml:"secure" json:"secure"`
			Meta       rkmidmeta.BootConfig    `yaml:"meta" json:"meta"`
			Jwt        rkmidjwt.BootConfig     `yaml:"jwt" json:"jwt"`
			Csrf       rkmidcsrf.BootConfig    `yaml:"csrf" yaml:"csrf"`
			RateLimit  rkmidlimit.BootConfig   `yaml:"rateLimit" json:"rateLimit"`
			Timeout    rkmidtimeout.BootConfig `yaml:"timeout" json:"timeout"`
			Trace      rkmidtrace.BootConfig   `yaml:"trace" json:"trace"`
		} `yaml:"middleware" json:"middleware"`
	} `yaml:"grpc" json:"grpc"`
}

BootConfig Boot config which is for grpc entry.

type BootConfigGrpcWeb added in v2.2.9

type BootConfigGrpcWeb struct {
	Enabled bool `yaml:"enabled" json:"enabled"`
	Cors    struct {
		AllowOrigins []string `yaml:"allowOrigins" json:"allowOrigins"`
	} `yaml:"cors" json:"cors"`
	Websocket struct {
		Enabled               bool   `yaml:"enabled" json:"enabled"`
		PingIntervalMs        int64  `yaml:"pingIntervalMs" json:"pingIntervalMs"`
		MessageReadLimitBytes int64  `yaml:"messageReadLimitBytes" json:"messageReadLimitBytes"`
		CompressMode          string `yaml:"compressMode" json:"compressMode"`
	} `yaml:"websocket" json:"websocket"`
}

type BootConfigProxy

type BootConfigProxy struct {
	Enabled bool `yaml:"enabled" json:"enabled"`
	Rules   []struct {
		Type        string   `yaml:"type" json:"type"`
		HeaderPairs []string `yaml:"headerPairs" json:"headerPairs"`
		Dest        []string `yaml:"dest" json:"dest"`
		Paths       []string `yaml:"paths" json:"paths"`
		Ips         []string `yaml:"ips" json:"ips"`
	} `yaml:"rules" json:"rules"`
}

BootConfigProxy Boot config which is for proxy entry.

1: Enabled: Enable prom entry. 2: Rules: Provide rules for proxying.

type Director

type Director func(context.Context) (context.Context, *grpc.ClientConn, error)

Director creates context and connection based on proxy rules.

type GrpcEntry

type GrpcEntry struct {
	LoggerEntry       *rkentry.LoggerEntry `json:"-" yaml:"-"`
	EventEntry        *rkentry.EventEntry  `json:"-" yaml:"-"`
	Port              uint64               `json:"-" yaml:"-"`
	GwPort            uint64               `json:"-" yaml:"-"`
	TlsConfig         *tls.Config          `json:"-" yaml:"-"`
	TlsConfigInsecure *tls.Config          `json:"-" yaml:"-"`
	// GRPC related
	Server             *grpc.Server                   `json:"-" yaml:"-"`
	ServerOpts         []grpc.ServerOption            `json:"-" yaml:"-"`
	UnaryInterceptors  []grpc.UnaryServerInterceptor  `json:"-" yaml:"-"`
	StreamInterceptors []grpc.StreamServerInterceptor `json:"-" yaml:"-"`
	GrpcRegF           []GrpcRegFunc                  `json:"-" yaml:"-"`
	EnableReflection   bool                           `json:"-" yaml:"-"`
	// grpcWeb related
	GrpcWebOptions []grpcweb.Option `json:"-" yaml:"-"`
	// Gateway related
	HttpMux       *http.ServeMux             `json:"-" yaml:"-"`
	HttpServer    *http.Server               `json:"-" yaml:"-"`
	GwMux         *gwruntime.ServeMux        `json:"-" yaml:"-"`
	GwMuxOptions  []gwruntime.ServeMuxOption `json:"-" yaml:"-"`
	GwRegF        []GwRegFunc                `json:"-" yaml:"-"`
	GwDialOptions []grpc.DialOption          `json:"-" yaml:"-"`

	// Utility related
	SWEntry            *rkentry.SWEntry                `json:"-" yaml:"-"`
	DocsEntry          *rkentry.DocsEntry              `json:"-" yaml:"-"`
	ProxyEntry         *ProxyEntry                     `json:"-" yaml:"-"`
	PromEntry          *rkentry.PromEntry              `json:"-" yaml:"-"`
	StaticFileEntry    *rkentry.StaticFileHandlerEntry `json:"-" yaml:"-"`
	CommonServiceEntry *rkentry.CommonServiceEntry     `json:"-" yaml:"-"`
	PProfEntry         *rkentry.PProfEntry             `json:"-" yaml:"-"`
	CertEntry          *rkentry.CertEntry              `json:"-" yaml:"-"`
	// contains filtered or unexported fields
}

GrpcEntry implements rkentry.Entry interface.

func GetGrpcEntry

func GetGrpcEntry(name string) *GrpcEntry

GetGrpcEntry Get GrpcEntry from rkentry.GlobalAppCtx.

func RegisterGrpcEntry

func RegisterGrpcEntry(opts ...GrpcEntryOption) *GrpcEntry

RegisterGrpcEntry Register GrpcEntry with options.

func (*GrpcEntry) AddGwCorsOptions

func (entry *GrpcEntry) AddGwCorsOptions(opts ...rkmidcors.Option)

AddGwCorsOptions Enable CORS at gateway side with options.

func (*GrpcEntry) AddGwCsrfOptions

func (entry *GrpcEntry) AddGwCsrfOptions(opts ...rkmidcsrf.Option)

AddGwCsrfOptions Enable CORS at gateway side with options.

func (*GrpcEntry) AddGwDialOptions

func (entry *GrpcEntry) AddGwDialOptions(opts ...grpc.DialOption)

AddGwDialOptions Add grpc dial options called from grpc gateway

func (*GrpcEntry) AddGwMuxOptions added in v2.1.4

func (entry *GrpcEntry) AddGwMuxOptions(opts ...gwruntime.ServeMuxOption)

AddGwMuxOptions Add mux options at gateway side.

func (*GrpcEntry) AddGwSecureOptions

func (entry *GrpcEntry) AddGwSecureOptions(opts ...rkmidsec.Option)

AddGwSecureOptions Enable secure at gateway side with options.

func (*GrpcEntry) AddRegFuncGrpc

func (entry *GrpcEntry) AddRegFuncGrpc(f ...GrpcRegFunc)

AddRegFuncGrpc Add grpc registration func.

func (*GrpcEntry) AddRegFuncGw

func (entry *GrpcEntry) AddRegFuncGw(f ...GwRegFunc)

AddRegFuncGw Add gateway registration func.

func (*GrpcEntry) AddServerOptions

func (entry *GrpcEntry) AddServerOptions(opts ...grpc.ServerOption)

AddServerOptions Add grpc server options.

func (*GrpcEntry) AddStreamInterceptors

func (entry *GrpcEntry) AddStreamInterceptors(inter ...grpc.StreamServerInterceptor)

AddStreamInterceptors Add stream interceptor.

func (*GrpcEntry) AddUnaryInterceptors

func (entry *GrpcEntry) AddUnaryInterceptors(inter ...grpc.UnaryServerInterceptor)

AddUnaryInterceptors Add unary interceptor.

func (*GrpcEntry) Bootstrap

func (entry *GrpcEntry) Bootstrap(ctx context.Context)

Bootstrap GrpcEntry.

func (*GrpcEntry) GetDescription

func (entry *GrpcEntry) GetDescription() string

GetDescription Get description of entry.

func (*GrpcEntry) GetName

func (entry *GrpcEntry) GetName() string

GetName Get entry name.

func (*GrpcEntry) GetType

func (entry *GrpcEntry) GetType() string

GetType Get entry type.

func (*GrpcEntry) Interrupt

func (entry *GrpcEntry) Interrupt(ctx context.Context)

Interrupt GrpcEntry.

func (*GrpcEntry) IsCommonServiceEnabled

func (entry *GrpcEntry) IsCommonServiceEnabled() bool

IsCommonServiceEnabled Is common service enabled?

func (*GrpcEntry) IsDocsEnabled

func (entry *GrpcEntry) IsDocsEnabled() bool

IsDocsEnabled Is tv enabled?

func (*GrpcEntry) IsGrpcWebEnabled added in v2.2.9

func (entry *GrpcEntry) IsGrpcWebEnabled() bool

IsGrpcWebEnabled Is grpc web enabled?

func (*GrpcEntry) IsPProfEnabled added in v2.1.0

func (entry *GrpcEntry) IsPProfEnabled() bool

IsPProfEnabled Is pprof enabled?

func (*GrpcEntry) IsPromEnabled

func (entry *GrpcEntry) IsPromEnabled() bool

IsPromEnabled Is prometheus client enabled?

func (*GrpcEntry) IsProxyEnabled

func (entry *GrpcEntry) IsProxyEnabled() bool

IsProxyEnabled Is proxy enabled?

func (*GrpcEntry) IsSWEnabled

func (entry *GrpcEntry) IsSWEnabled() bool

IsSWEnabled Is swagger enabled?

func (*GrpcEntry) IsStaticFileHandlerEnabled

func (entry *GrpcEntry) IsStaticFileHandlerEnabled() bool

IsStaticFileHandlerEnabled Is static file handler entry enabled?

func (*GrpcEntry) IsTlsEnabled

func (entry *GrpcEntry) IsTlsEnabled() bool

IsTlsEnabled Is TLS enabled?

func (*GrpcEntry) MarshalJSON

func (entry *GrpcEntry) MarshalJSON() ([]byte, error)

MarshalJSON Marshal entry.

func (*GrpcEntry) String

func (entry *GrpcEntry) String() string

String Stringfy entry.

func (*GrpcEntry) UnmarshalJSON

func (entry *GrpcEntry) UnmarshalJSON([]byte) error

UnmarshalJSON Not supported.

type GrpcEntryOption

type GrpcEntryOption func(*GrpcEntry)

GrpcEntryOption GrpcEntry option.

func WithCertEntry

func WithCertEntry(certEntry *rkentry.CertEntry) GrpcEntryOption

WithCertEntry Provide rkentry.CertEntry.

func WithCommonServiceEntry

func WithCommonServiceEntry(commonService *rkentry.CommonServiceEntry) GrpcEntryOption

WithCommonServiceEntry Provide rkentry.CommonServiceEntry.

func WithDescription

func WithDescription(description string) GrpcEntryOption

WithDescription Provide description.

func WithDocsEntry

func WithDocsEntry(docs *rkentry.DocsEntry) GrpcEntryOption

WithDocsEntry Provide rkentry.DocsEntry.

func WithEnableReflection

func WithEnableReflection(enabled bool) GrpcEntryOption

WithEnableReflection Provide EnableReflection.

func WithEventEntry

func WithEventEntry(logger *rkentry.EventEntry) GrpcEntryOption

WithEventEntry Provide rkentry.EventEntry

func WithGrpcDialOptions

func WithGrpcDialOptions(opts ...grpc.DialOption) GrpcEntryOption

WithGrpcDialOptions Provide grpc dial options.

func WithGrpcRegF

func WithGrpcRegF(f ...GrpcRegFunc) GrpcEntryOption

WithGrpcRegF Provide GrpcRegFunc.

func WithGrpcWebOptions added in v2.2.9

func WithGrpcWebOptions(opts ...grpcweb.Option) GrpcEntryOption

WithGrpcWebOptions Provide grpcweb server options.

func WithGwMuxOptions

func WithGwMuxOptions(opts ...gwruntime.ServeMuxOption) GrpcEntryOption

WithGwMuxOptions Provide gateway server mux options.

func WithGwPort added in v2.2.19

func WithGwPort(gwPort uint64) GrpcEntryOption

WithGwPort Provide gateway port, gateway will use same port if not provided

func WithGwRegF

func WithGwRegF(f ...GwRegFunc) GrpcEntryOption

WithGwRegF Provide registration function.

func WithLoggerEntry

func WithLoggerEntry(logger *rkentry.LoggerEntry) GrpcEntryOption

WithLoggerEntry Provide rkentry.LoggerEntry

func WithName

func WithName(name string) GrpcEntryOption

WithName Provide name.

func WithPProfEntry added in v2.1.0

func WithPProfEntry(p *rkentry.PProfEntry) GrpcEntryOption

WithPProfEntry Provide rkentry.PProfEntry.

func WithPort

func WithPort(port uint64) GrpcEntryOption

WithPort Provide grpc port, gateway will use same port if not provided

func WithPromEntry

func WithPromEntry(prom *rkentry.PromEntry) GrpcEntryOption

WithPromEntry Provide rkentry.PromEntry.

func WithProxyEntry

func WithProxyEntry(proxy *ProxyEntry) GrpcEntryOption

WithProxyEntry Provide ProxyEntry.

func WithServerOptions

func WithServerOptions(opts ...grpc.ServerOption) GrpcEntryOption

WithServerOptions Provide grpc.ServerOption.

func WithStaticFileHandlerEntry

func WithStaticFileHandlerEntry(staticEntry *rkentry.StaticFileHandlerEntry) GrpcEntryOption

WithStaticFileHandlerEntry provide rkentry.StaticFileHandlerEntry.

func WithStreamInterceptors

func WithStreamInterceptors(opts ...grpc.StreamServerInterceptor) GrpcEntryOption

WithStreamInterceptors Provide grpc.StreamServerInterceptor.

func WithSwEntry

func WithSwEntry(sw *rkentry.SWEntry) GrpcEntryOption

WithSwEntry Provide rkentry.SWEntry.

func WithUnaryInterceptors

func WithUnaryInterceptors(opts ...grpc.UnaryServerInterceptor) GrpcEntryOption

WithUnaryInterceptors Provide grpc.UnaryServerInterceptor.

type GrpcRegFunc

type GrpcRegFunc func(server *grpc.Server)

GrpcRegFunc Grpc registration func.

type GwRegFunc

type GwRegFunc func(context.Context, *gwruntime.ServeMux, string, []grpc.DialOption) error

GwRegFunc Registration function grpc gateway.

type HeaderPattern

type HeaderPattern struct {
	Headers map[string]string
	Dest    []string
}

HeaderPattern defines proxy rules based on header.

Proxy will validate headers in metadata with provided rules.

type IpPattern

type IpPattern struct {
	Cidrs []string
	Dest  []string
}

IpPattern defines proxy rules based on remote IPs.

Ip rule support CIDR.

type PathPattern

type PathPattern struct {
	Paths []string
	Dest  []string
}

PathPattern defines proxy rules based on path.

The incoming path should match with rules. Path rule support regex.

type ProxyEntry

type ProxyEntry struct {
	LoggerEntry *rkentry.LoggerEntry `json:"-" yaml:"-"`
	EventEntry  *rkentry.EventEntry  `json:"-" yaml:"-"`
	// contains filtered or unexported fields
}

func NewProxyEntry

func NewProxyEntry(opts ...ProxyEntryOption) *ProxyEntry

NewProxyEntry Create a proxy entry with options

func (*ProxyEntry) Bootstrap

func (entry *ProxyEntry) Bootstrap(ctx context.Context)

Bootstrap Start prometheus client

func (*ProxyEntry) GetDescription

func (entry *ProxyEntry) GetDescription() string

GetDescription Get description of entry

func (*ProxyEntry) GetName

func (entry *ProxyEntry) GetName() string

GetName Return name of proxy entry

func (*ProxyEntry) GetType

func (entry *ProxyEntry) GetType() string

GetType Return type of prom entry

func (*ProxyEntry) Interrupt

func (entry *ProxyEntry) Interrupt(ctx context.Context)

Interrupt Shutdown prometheus client

func (*ProxyEntry) MarshalJSON

func (entry *ProxyEntry) MarshalJSON() ([]byte, error)

MarshalJSON Marshal entry

func (*ProxyEntry) String

func (entry *ProxyEntry) String() string

String Stringfy prom entry

func (*ProxyEntry) UnmarshalJSON

func (entry *ProxyEntry) UnmarshalJSON(b []byte) error

UnmarshalJSON Unmarshal entry

type ProxyEntryOption

type ProxyEntryOption func(*ProxyEntry)

ProxyEntryOption Proxy entry option used while initializing proxy entry via code

func WithEventEntryProxy

func WithEventEntryProxy(eventEntry *rkentry.EventEntry) ProxyEntryOption

WithEventEntryProxy rkentry.EventEntry of proxy entry

func WithLoggerEntryProxy

func WithLoggerEntryProxy(loggerEntry *rkentry.LoggerEntry) ProxyEntryOption

WithLoggerEntryProxy rkentry.LoggerEntry of proxy entry

func WithNameProxy

func WithNameProxy(name string) ProxyEntryOption

WithNameProxy Name of proxy entry

func WithRuleProxy

func WithRuleProxy(r *rule) ProxyEntryOption

WithRuleProxy Provide rule

Directories

Path Synopsis
api
gen

Jump to

Keyboard shortcuts

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