github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/services/shared/interceptor.go (about)

     1  package shared
     2  
     3  import (
     4  	"google.golang.org/grpc"
     5  
     6  	"github.com/authzed/spicedb/internal/middleware/servicespecific"
     7  )
     8  
     9  // WithUnaryServiceSpecificInterceptor is a helper to add a unary interceptor or interceptor
    10  // chain to a service.
    11  type WithUnaryServiceSpecificInterceptor struct {
    12  	Unary grpc.UnaryServerInterceptor
    13  }
    14  
    15  // UnaryInterceptor implements servicespecific.ExtraUnaryInterceptor
    16  func (wussi WithUnaryServiceSpecificInterceptor) UnaryInterceptor() grpc.UnaryServerInterceptor {
    17  	return wussi.Unary
    18  }
    19  
    20  // WithStreamServiceSpecificInterceptor is a helper to add a stream interceptor or interceptor
    21  // chain to a service.
    22  type WithStreamServiceSpecificInterceptor struct {
    23  	Stream grpc.StreamServerInterceptor
    24  }
    25  
    26  // StreamInterceptor implements servicespecific.ExtraStreamInterceptor
    27  func (wsssi WithStreamServiceSpecificInterceptor) StreamInterceptor() grpc.StreamServerInterceptor {
    28  	return wsssi.Stream
    29  }
    30  
    31  // WithServiceSpecificInterceptors is a helper to add both a unary and stream interceptor
    32  // or interceptor chain to a service.
    33  type WithServiceSpecificInterceptors struct {
    34  	Unary  grpc.UnaryServerInterceptor
    35  	Stream grpc.StreamServerInterceptor
    36  }
    37  
    38  // UnaryInterceptor implements servicespecific.ExtraUnaryInterceptor
    39  func (wssi WithServiceSpecificInterceptors) UnaryInterceptor() grpc.UnaryServerInterceptor {
    40  	return wssi.Unary
    41  }
    42  
    43  // StreamInterceptor implements servicespecific.ExtraStreamInterceptor
    44  func (wssi WithServiceSpecificInterceptors) StreamInterceptor() grpc.StreamServerInterceptor {
    45  	return wssi.Stream
    46  }
    47  
    48  var (
    49  	_ servicespecific.ExtraUnaryInterceptor  = WithUnaryServiceSpecificInterceptor{}
    50  	_ servicespecific.ExtraUnaryInterceptor  = WithServiceSpecificInterceptors{}
    51  	_ servicespecific.ExtraStreamInterceptor = WithServiceSpecificInterceptors{}
    52  )