Server interceptor

Interface implementation

  • To add an interceptor, you need to implement the following functionality

  • Then viaclient.AddInterceptorregister the interceptor; afterwards when the service starts it will call the corresponding interceptor through configuration


// UnaryServerInfo contains metadata about a single (unary) RPC call.
// This is passed to interceptors to provide context about the server and method being called.
type UnaryServerInfo struct {
	// Server is the underlying service implementation.
	Server any
	// FullMethod is the path to the RPC (e.g., "/user.UserService/GetUser").
	FullMethod string
	// Protocol identifies the transport (e.g., "grpc", "rest").
	Protocol string
}

// UnaryHandler is the signature of the final business logic or the next step in the chain.
type UnaryHandler func(ctx context.Context, req any) (any, error)

// UnaryServerInterceptor is a middleware function that wraps the request execution.
// It can modify the context/request before execution or the response/error after.
type UnaryServerInterceptor func(ctx context.Context, req any, info *UnaryServerInfo, handler UnaryHandler) (resp any, err error)

// ServerInterceptor is the interface for pluggable middleware components.
type ServerInterceptor interface {
	// Name returns the unique identifier for the interceptor (e.g., "logger").
	Name() string
	// Interceptor returns the actual function that performs the wrapping.
	Interceptor() UnaryServerInterceptor
}

Supported implementations

Configuration

Update default interceptors:

Custom implementation

Last updated