github.com/kaydxh/golang@v0.0.131/pkg/grpc-gateway/grpc_gateway_grpc.option.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package grpcgateway
    23  
    24  import (
    25  	"runtime/debug"
    26  	"time"
    27  
    28  	interceptorlogrus_ "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
    29  	interceptorrecovery_ "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
    30  	"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
    31  	runtime_ "github.com/kaydxh/golang/go/runtime"
    32  	rate_ "github.com/kaydxh/golang/go/time/rate"
    33  	interceptortcloud_ "github.com/kaydxh/golang/pkg/middleware/api/tcloud/v3.0"
    34  	interceptordebug_ "github.com/kaydxh/golang/pkg/middleware/grpc-middleware/debug"
    35  	interceptoropentelemetry_ "github.com/kaydxh/golang/pkg/middleware/grpc-middleware/monitor/opentelemetry"
    36  	interceptorprometheus_ "github.com/kaydxh/golang/pkg/middleware/grpc-middleware/monitor/prometheus"
    37  	interceptorratelimit_ "github.com/kaydxh/golang/pkg/middleware/grpc-middleware/ratelimit"
    38  
    39  	"github.com/sirupsen/logrus"
    40  	"google.golang.org/grpc"
    41  	"google.golang.org/grpc/codes"
    42  	"google.golang.org/grpc/status"
    43  )
    44  
    45  func WithServerOptions(opts ...grpc.ServerOption) GRPCGatewayOption {
    46  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
    47  		c.opts.serverOptions = append(c.opts.serverOptions, opts...)
    48  	})
    49  }
    50  
    51  func WithClientDialOptions(opts ...grpc.DialOption) GRPCGatewayOption {
    52  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
    53  		c.opts.clientDialOptions = append(c.opts.clientDialOptions, opts...)
    54  	})
    55  }
    56  
    57  func WithServerUnaryInterceptorsOptions(opts ...grpc.UnaryServerInterceptor) GRPCGatewayOption {
    58  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
    59  		c.opts.interceptionOptions.grpcServerOpts.unaryInterceptors = append(
    60  			c.opts.interceptionOptions.grpcServerOpts.unaryInterceptors,
    61  			opts...)
    62  	})
    63  }
    64  
    65  func WithServerStreamInterceptorsOptions(opts ...grpc.StreamServerInterceptor) GRPCGatewayOption {
    66  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
    67  		c.opts.interceptionOptions.grpcServerOpts.streamInterceptors = append(
    68  			c.opts.interceptionOptions.grpcServerOpts.streamInterceptors,
    69  			opts...)
    70  	})
    71  }
    72  
    73  func WithServerInterceptorsLogrusOptions(
    74  	logger *logrus.Logger,
    75  ) GRPCGatewayOption {
    76  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
    77  		l := logrus.NewEntry(logger)
    78  		WithServerUnaryInterceptorsOptions(interceptorlogrus_.UnaryServerInterceptor(l)).apply(c)
    79  		WithServerStreamInterceptorsOptions(interceptorlogrus_.StreamServerInterceptor(l)).apply(c)
    80  	})
    81  }
    82  
    83  // recover
    84  func WithServerInterceptorsRecoveryOptions() GRPCGatewayOption {
    85  	opts := []interceptorrecovery_.Option{
    86  		interceptorrecovery_.WithRecoveryHandler(func(r interface{}) (err error) {
    87  			out, err := runtime_.FormatStack()
    88  			if err != nil {
    89  				logrus.WithError(status.Errorf(codes.Internal, "%s", r)).Errorf("%s", debug.Stack())
    90  			} else {
    91  				logrus.WithError(status.Errorf(codes.Internal, "%s", r)).Errorf("%s", out)
    92  			}
    93  
    94  			return status.Errorf(codes.Internal, "panic: %v", r)
    95  		}),
    96  	}
    97  
    98  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
    99  		WithServerUnaryInterceptorsOptions(interceptorrecovery_.UnaryServerInterceptor(opts...)).apply(c)
   100  		WithServerStreamInterceptorsOptions(interceptorrecovery_.StreamServerInterceptor(opts...)).apply(c)
   101  	})
   102  }
   103  
   104  func WithServerUnaryInterceptorsTimerOptions(enabledMetric bool) GRPCGatewayOption {
   105  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
   106  		WithServerUnaryInterceptorsOptions(interceptorprometheus_.UnaryServerInterceptorOfTimer(enabledMetric)).apply(c)
   107  	})
   108  }
   109  
   110  func WithServerUnaryInterceptorsCodeMessageOptions(enabledMetric bool) GRPCGatewayOption {
   111  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
   112  		WithServerUnaryInterceptorsOptions(
   113  			interceptorprometheus_.UnaryServerInterceptorOfCodeMessage(enabledMetric),
   114  		).apply(
   115  			c,
   116  		)
   117  	})
   118  }
   119  
   120  func WithServerUnaryMetricInterceptorOptions() GRPCGatewayOption {
   121  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
   122  		WithServerUnaryInterceptorsOptions(
   123  			interceptoropentelemetry_.UnaryServerMetricInterceptor(),
   124  		).apply(
   125  			c,
   126  		)
   127  	})
   128  }
   129  
   130  func WithServerUnaryInterceptorsRequestIdOptions() GRPCGatewayOption {
   131  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
   132  		WithServerUnaryInterceptorsOptions(interceptordebug_.UnaryServerInterceptorOfRequestId()).apply(c)
   133  	})
   134  }
   135  
   136  func WithServerUnaryInterceptorsErrorOptions() GRPCGatewayOption {
   137  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
   138  		WithServerUnaryInterceptorsOptions(interceptortcloud_.UnaryServerInterceptorOfError()).apply(c)
   139  		//WithServerStreamInterceptorsOptions(interceptortcloud_.StreamServerInterceptor()).apply(c)
   140  	})
   141  }
   142  
   143  //limiter rate for grpc api
   144  func WithServerInterceptorsLimitRateOptions(burstUnary, burstStream int) GRPCGatewayOption {
   145  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
   146  		if burstUnary > 0 {
   147  			limiterUnary := rate_.NewLimiter(burstUnary)
   148  			WithServerUnaryInterceptorsOptions(interceptorratelimit_.UnaryServerInterceptor(limiterUnary)).apply(c)
   149  		}
   150  
   151  		if burstStream > 0 {
   152  			limiterStream := rate_.NewLimiter(burstStream)
   153  			WithServerStreamInterceptorsOptions(interceptorratelimit_.StreamServerInterceptor(limiterStream)).apply(c)
   154  		}
   155  	})
   156  }
   157  
   158  func WithServerUnaryInterceptorsInOutPacketOptions() GRPCGatewayOption {
   159  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
   160  		WithServerUnaryInterceptorsOptions(interceptordebug_.UnaryServerInterceptorOfInOutputPrinter()).apply(c)
   161  	})
   162  }
   163  
   164  // timeout
   165  func WithServerInterceptorTimeoutOptions(timeout time.Duration) GRPCGatewayOption {
   166  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
   167  		if timeout > 0 {
   168  			runtime.DefaultContextTimeout = timeout
   169  		}
   170  	})
   171  }
   172  
   173  /*
   174  // WithHttpHandlerInterceptorOptions
   175  func WithHttpHandlerInterceptorOptions(opts ...http_.HandlerChainOption) GRPCGatewayOption {
   176  
   177  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
   178  		c.opts.interceptionOptions.httpServerOpts.handlerChain.ApplyOptions(opts...)
   179  	})
   180  }
   181  
   182  // WithHttpHandlerInterceptorOptions
   183  func WithHttpHandlerInterceptorOptions(opts ...http_.HandlerInterceptorsOption) GRPCGatewayOption {
   184  
   185  	return GRPCGatewayOptionFunc(func(c *GRPCGateway) {
   186  		c.opts.interceptionOptions.httpServerOpts.httpInterceptors.ApplyOptions(opts...)
   187  	})
   188  }
   189  
   190  func WithHttpHandlerInterceptor(handler func(http.Handler) http.Handler) GRPCGatewayOption {
   191  
   192  	return WithHttpHandlerInterceptorOptions(
   193  		http_.HandlerInterceptorsOptionFunc(func(handlers *http_.HandlerInterceptors) {
   194  			handlers.Interceptors = append(handlers.Interceptors, handler)
   195  		}),
   196  	)
   197  }
   198  */
   199  
   200  // WithHttpHandlerInterceptorTraceIDOptions
   201  /*
   202  func WithHttpHandlerInterceptorInOutPacketOptions() GRPCGatewayOption {
   203  	//return WithHttpHandlerInterceptor(httpinterceptorinoutpacket_.InOutPacket)
   204  	return WithHttpHandlerInterceptor(httpinterceptorinoutpacket_.InOutPacket)
   205  }
   206  */