grpckit

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorGRPC added in v0.3.0

func ErrorGRPC[T any](ctx context.Context, err error, options ...ResponseOption) (T, error)

ErrorGRPC tries to map err to errkit.Error and based on result writes standard gRPC error with status statusCode to the response writer.

func LoggingInterceptor added in v0.2.7

func LoggingInterceptor(logger *slog.Logger) grpc.UnaryServerInterceptor

LoggingInterceptor is a gRPC unary server interceptor that logs method calls and their durations. It takes a logger instance as input and returns a UnaryServerInterceptor function.

func MetricsInterceptor added in v0.2.7

func MetricsInterceptor() grpc.UnaryServerInterceptor

func SetGRPCErrorResponder added in v0.3.0

func SetGRPCErrorResponder(responder GRPCErrorResponder)

SetGRPCErrorResponder sets the given responder as errGRPCResponder.

Types

type GRPCEndpointRegistrator

type GRPCEndpointRegistrator interface {
	Mount(server *grpc.Server)
}

GRPCEndpointRegistrator abstracts a mechanics of registering the gRPC service in the gRPC server.

type GRPCErrorResponder added in v0.3.0

type GRPCErrorResponder func(err error, options ...ResponseOption) error

GRPCErrorResponder represents a function type that handles errors from gRPC responses.

type ListenerConfig added in v0.2.15

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

ListenerConfig represents a struct that holds the configuration options for a gRPC server.

type ListenerGRPC

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

ListenerGRPC represents a struct that encapsulates a gRPC server listener.

func NewListenerGRPC

func NewListenerGRPC(addr string, options ...Option[ListenerConfig]) (*ListenerGRPC, error)

NewListenerGRPC creates a new ListenerGRPC instance by creating a gRPC listener using a given address. It applies all the options to a default `applyOptionsGRPC` instance and sets the server options with the provided unary and stream interceptors. Finally, it returns the ListenerGRPC instance and potential error.

func (*ListenerGRPC) Mount

func (l *ListenerGRPC) Mount(handlers ...GRPCEndpointRegistrator)

Mount the given handlers to the listener gRPC server.

func (*ListenerGRPC) Serve

func (l *ListenerGRPC) Serve(ctx context.Context) error

type Option

type Option[T ListenerConfig] func(o *T)

Option implements functional options pattern for the ListenerGRPC type. Represents a function which receive a pointer to the generic struct that represents a part of ListenerGRPC configuration and changes it default values to the given ones.

See the applyOptionsGRPC function to understand the configuration behaviour. Option functions should only be passed to ListenerGRPC constructor function NewListenerGRPC.

func WithLogger

func WithLogger(logger *slog.Logger) Option[ListenerConfig]

WithLogger sets the server logger.

func WithStreamInterceptors

func WithStreamInterceptors(interceptors ...StreamInterceptor) Option[ListenerConfig]

WithStreamInterceptors is a function that takes a variable number of StreamInterceptor functions and returns an Option[ListenerConfig]. This function is used to add StreamInterceptors to the streamInterceptors field of the ListenerConfig struct.

func WithUnaryInterceptors

func WithUnaryInterceptors(interceptors ...UnaryInterceptor) Option[ListenerConfig]

WithUnaryInterceptors is a function that takes a variable number of UnaryInterceptor functions and returns an Option[ListenerConfig]. This function is used to add UnaryInterceptors to the unaryInterceptors field of the ListenerConfig struct.

type ResponseOption added in v0.3.0

type ResponseOption func(o *ResponseOptions)

ResponseOption represents a function type that modifies ResponseOptions for an gRPC response.

func WithStatus added in v0.3.0

func WithStatus(code codes.Code) ResponseOption

WithStatus sets the given status code as the statusCode field of the ResponseOptions parameter.

type ResponseOptions added in v0.3.0

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

ResponseOptions represents the options for an gRPC response.

func NewResponseOptions added in v0.3.0

func NewResponseOptions(options ...ResponseOption) *ResponseOptions

NewResponseOptions returns a pointer to a new ResponseOptions object with default values and applies the given options to it.

type StreamInterceptor added in v0.2.7

type StreamInterceptor = grpc.StreamServerInterceptor

StreamInterceptor is an alias for grpc.StreamServerInterceptor. It is used as a middleware to intercept streaming RPC calls in a gRPC server.

type UnaryInterceptor added in v0.2.7

type UnaryInterceptor = grpc.UnaryServerInterceptor

UnaryInterceptor is an alias for grpc.UnaryServerInterceptor. It is used as a middleware to intercept unary RPC calls in a gRPC server.

Jump to

Keyboard shortcuts

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