github.com/gofunct/common@v0.0.0-20190131174352-fd058c7fbf22/pkg/transport/engine/options.go (about) 1 package engine 2 3 import ( 4 "github.com/gofunct/common/pkg/transport/api" 5 "github.com/gofunct/common/pkg/transport/config" 6 "github.com/gofunct/common/pkg/transport/middleware" 7 "os" 8 9 "github.com/grpc-ecosystem/grpc-gateway/runtime" 10 "google.golang.org/grpc" 11 "google.golang.org/grpc/grpclog" 12 ) 13 14 // Option configures a gRPC and a gateway server. 15 type Option func(*config.Config) 16 17 func createConfig(opts []Option) *config.Config { 18 c := config.CreateDefaultConfig() 19 for _, f := range opts { 20 f(c) 21 } 22 return c 23 } 24 25 // WithServers returns an Option that sets gRPC service server implementation(s). 26 func WithServers(svrs ...api.Server) Option { 27 return func(c *config.Config) { 28 c.Servers = append(c.Servers, svrs...) 29 } 30 } 31 32 // WithAddr returns an Option that sets an network address for a gRPC and a gateway server. 33 func WithAddr(network, addr string) Option { 34 return func(c *config.Config) { 35 WithGrpcAddr(network, addr)(c) 36 WithGatewayAddr(network, addr)(c) 37 } 38 } 39 40 // WithGrpcAddr returns an Option that sets an network address for a gRPC server. 41 func WithGrpcAddr(network, addr string) Option { 42 return func(c *config.Config) { 43 c.GrpcAddr = &config.Address{ 44 Network: network, 45 Addr: addr, 46 } 47 } 48 } 49 50 // WithGrpcInternalAddr returns an Option that sets an network address connected by a gateway server. 51 func WithGrpcInternalAddr(network, addr string) Option { 52 return func(c *config.Config) { 53 c.GrpcInternalAddr = &config.Address{ 54 Network: network, 55 Addr: addr, 56 } 57 } 58 } 59 60 // WithGatewayAddr returns an Option that sets an network address for a gateway server. 61 func WithGatewayAddr(network, addr string) Option { 62 return func(c *config.Config) { 63 c.GatewayAddr = &config.Address{ 64 Network: network, 65 Addr: addr, 66 } 67 } 68 } 69 70 // WithGrpcServerUnaryInterceptors returns an Option that sets unary interceptor(s) for a gRPC server. 71 func WithGrpcServerUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) Option { 72 return func(c *config.Config) { 73 c.GrpcServerUnaryInterceptors = append(c.GrpcServerUnaryInterceptors, interceptors...) 74 } 75 } 76 77 // WithGrpcServerStreamInterceptors returns an Option that sets stream interceptor(s) for a gRPC server. 78 func WithGrpcServerStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) Option { 79 return func(c *config.Config) { 80 c.GrpcServerStreamInterceptors = append(c.GrpcServerStreamInterceptors, interceptors...) 81 } 82 } 83 84 // WithGatewayServerUnaryInterceptors returns an Option that sets unary interceptor(s) for a gRPC client used by a gateway server. 85 func WithGatewayServerUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) Option { 86 return func(c *config.Config) { 87 c.GatewayServerUnaryInterceptors = append(c.GatewayServerUnaryInterceptors, interceptors...) 88 } 89 } 90 91 // WithGatewayServerStreamInterceptors returns an Option that sets stream interceptor(s) for a gRPC client used by a gateway server. 92 func WithGatewayServerStreamInterceptors(interceptors ...grpc.StreamClientInterceptor) Option { 93 return func(c *config.Config) { 94 c.GatewayServerStreamInterceptors = append(c.GatewayServerStreamInterceptors, interceptors...) 95 } 96 } 97 98 // WithGrpcServerOptions returns an Option that sets grpc.ServerOption(s) to a gRPC server. 99 func WithGrpcServerOptions(opts ...grpc.ServerOption) Option { 100 return func(c *config.Config) { 101 c.GrpcServerOption = append(c.GrpcServerOption, opts...) 102 } 103 } 104 105 // WithGatewayDialOptions returns an Option that sets grpc.DialOption(s) to a gRPC clinet used by a gateway server. 106 func WithGatewayDialOptions(opts ...grpc.DialOption) Option { 107 return func(c *config.Config) { 108 c.GatewayDialOption = append(c.GatewayDialOption, opts...) 109 } 110 } 111 112 // WithGatewayMuxOptions returns an Option that sets runtime.ServeMuxOption(s) to a gateway server. 113 func WithGatewayMuxOptions(opts ...runtime.ServeMuxOption) Option { 114 return func(c *config.Config) { 115 c.GatewayMuxOptions = append(c.GatewayMuxOptions, opts...) 116 } 117 } 118 119 // WithGatewayServerMiddlewares returns an Option that sets middleware(s) for http.api.Server to a gateway server. 120 func WithGatewayServerMiddlewares(middlewares ...middleware.HTTPServerMiddleware) Option { 121 return func(c *config.Config) { 122 c.GatewayServerMiddlewares = append(c.GatewayServerMiddlewares, middlewares...) 123 } 124 } 125 126 // WithGatewayServerConfig returns an Option that specifies http.api.Server configuration to a gateway server. 127 func WithGatewayServerConfig(cfg *config.HTTPServerConfig) Option { 128 return func(c *config.Config) { 129 c.GatewayServerConfig = cfg 130 } 131 } 132 133 // WithPassedHeader returns an Option that sets configurations about passed headers for a gateway server. 134 func WithPassedHeader(decider middleware.PassedHeaderDeciderFunc) Option { 135 return WithGatewayServerMiddlewares(middleware.CreatePassingHeaderMiddleware(decider)) 136 } 137 138 // WithDefaultLogger returns an Option that sets default grpclogger.LoggerV2 object. 139 func WithDefaultLogger() Option { 140 return func(c *config.Config) { 141 grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stdout, os.Stderr, os.Stderr)) 142 } 143 }