Documentation ¶
Overview ¶
Package tinygrpc provides gRPC server implementation.
Index ¶
- func GetClientIP(ctx context.Context) net.IP
- func NewTokenCredentials(token string) credentials.PerRPCCredentials
- type CallMetadata
- type DuplexStream
- type DuplexStreamConfig
- type DuplexStreamOpt
- type Server
- type ServerConfig
- type ServerOpt
- func EnableAuthMiddleware[T any](verifier TokenVerifier[T]) ServerOpt
- func EnableAuthMiddlewareFunc[T any](verifierFunc TokenVerifierFunc[T]) ServerOpt
- func ServerOptions(opts ...grpc.ServerOption) ServerOpt
- func StreamInterceptor(interceptor grpc.StreamServerInterceptor) ServerOpt
- func UnaryInterceptor(interceptor grpc.UnaryServerInterceptor) ServerOpt
- type TokenVerificationResult
- type TokenVerifier
- type TokenVerifierFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetClientIP ¶
GetClientIP resolves the IP address (either v4 or v6) of the client. By default, function returns a remote address associated with the socket. In case the "x-forwarded-for" header is specified and parseable - the value of this header is returned.
func NewTokenCredentials ¶
func NewTokenCredentials(token string) credentials.PerRPCCredentials
NewTokenCredentials creates new credentials.PerRPCCredentials instance, appending given token to gRPC call.
Types ¶
type CallMetadata ¶
type CallMetadata struct { // IP is an IP address of the client. IP net.IP // MethodName is the full name of the method. MethodName string }
CallMetadata represents additional information about the gRPC call.
type DuplexStream ¶
DuplexStream simplifies operation on bidirectional gRPC streams.
func NewDuplexStream ¶
func NewDuplexStream[R any, S any](stream grpc.ServerStream, opts ...DuplexStreamOpt) *DuplexStream[R, S]
NewDuplexStream creates new DuplexStream.
func (*DuplexStream[R, S]) OnEnd ¶
func (ds *DuplexStream[R, S]) OnEnd(handler func(reason error))
OnEnd specifies a handler for stream end event. The handler is called either on stream error or after you call Stop on given stream.
func (*DuplexStream[R, S]) OnReceive ¶
func (ds *DuplexStream[R, S]) OnReceive(handler func(msg *R))
OnReceive specifies a handler for incoming messages. The function will call the handler for all incoming messages sequentially, using the same goroutine for each call.
func (*DuplexStream[R, S]) Send ¶
func (ds *DuplexStream[R, S]) Send(msg *S)
Send sends a new message to the client.
func (*DuplexStream[R, S]) Start ¶
func (ds *DuplexStream[R, S]) Start() (err error)
Start bootstraps goroutines responsible for handling receive and send channels and blocks until either the server (with Stop), or the client interrupts connection.
func (*DuplexStream[R, S]) Stop ¶
func (ds *DuplexStream[R, S]) Stop()
Stop cancels goroutines responsible for handling receive and send channels and unblocks Start.
type DuplexStreamConfig ¶
type DuplexStreamConfig struct {
// contains filtered or unexported fields
}
DuplexStreamConfig provides a configuration for DuplexStream.
type DuplexStreamOpt ¶
type DuplexStreamOpt = func(config *DuplexStreamConfig)
DuplexStreamOpt is an option to be passed to NewDuplexStream.
func ReceiveChannelCapacity ¶
func ReceiveChannelCapacity(capacity int64) DuplexStreamOpt
ReceiveChannelCapacity sets a total capacity (in number of messages) of receive channel's buffer.
func SendChannelCapacity ¶
func SendChannelCapacity(capacity int64) DuplexStreamOpt
SendChannelCapacity sets a total capacity (in number of messages) of send channel's buffer.
type Server ¶
Server is an object representing grpc.Server and implementing the tiny.Service interface.
type ServerConfig ¶ added in v1.0.8
type ServerConfig struct {
// contains filtered or unexported fields
}
ServerConfig holds a configuration for NewServer.
type ServerOpt ¶
type ServerOpt = func(*ServerConfig)
ServerOpt is an option to be specified to NewServer.
func EnableAuthMiddleware ¶
func EnableAuthMiddleware[T any](verifier TokenVerifier[T]) ServerOpt
EnableAuthMiddleware makes server use token-based authorization based on passed TokenVerifier.
func EnableAuthMiddlewareFunc ¶
func EnableAuthMiddlewareFunc[T any](verifierFunc TokenVerifierFunc[T]) ServerOpt
EnableAuthMiddlewareFunc makes server use token-based authorization based on passed TokenVerifierFunc.
func ServerOptions ¶
func ServerOptions(opts ...grpc.ServerOption) ServerOpt
ServerOptions allows to specify custom grpc.ServerOption options.
func StreamInterceptor ¶
func StreamInterceptor(interceptor grpc.StreamServerInterceptor) ServerOpt
StreamInterceptor adds specified interceptor to the tail of stream interceptors chains.
func UnaryInterceptor ¶
func UnaryInterceptor(interceptor grpc.UnaryServerInterceptor) ServerOpt
UnaryInterceptor adds specified interceptor to the tail of unary interceptors chains.
type TokenVerificationResult ¶
type TokenVerificationResult[T any] struct { // IsAuthorized defines whether call has been authorized by the middleware. IsAuthorized bool // SessionData carries additional information about the session, such as account data. // It can be set by TokenVerifierFunc. SessionData T }
TokenVerificationResult represents a result of token verification procedure.
func GetTokenVerificationResult ¶
func GetTokenVerificationResult[T any](ctx context.Context) *TokenVerificationResult[T]
GetTokenVerificationResult returns token verification result for a given call.
type TokenVerifier ¶
type TokenVerifier[T any] interface { // Verify defines an application-specific procedure for token verification. Verify(token string, metadata *CallMetadata) (*TokenVerificationResult[T], error) }
TokenVerifier defines an interface for application-specific procedure for token verification.
type TokenVerifierFunc ¶
type TokenVerifierFunc[T any] func(token string, metadata *CallMetadata) (*TokenVerificationResult[T], error)
TokenVerifierFunc defines an application-specific procedure for token verification.