func WithTracer() gogogrpc.ServerOption { return gogogrpc.WithServiceDescWrapper(func(desc *grpc.ServiceDesc, _ interface{}) { for i, m := range desc.Methods { desc.Methods[i] = wrapMethodWithTracer(desc, m) } for i, s := range desc.Streams { desc.Streams[i] = wrapStreamWithTracer(desc, s) } }) }
func WithGateway(r *router.Router) gogogrpc.ServerOption { return gogogrpc.WithServiceDescWrapper(func(desc *grpc.ServiceDesc, srv interface{}) { gatewayDesc := lookupGatewayDesc(desc.HandlerType) if gatewayDesc == nil { return } // ensure the ServiceDesc doesn't get modified by later options desc = gogogrpc.CloneServiceDesc(desc) for _, route := range gatewayDesc.Routes { r.Addf(route.Method, route.Pattern, wrapServiceWithGateway(desc, srv, route.Handler)) } }) }
func WithAuthGuard(handler interface{}) gogogrpc.ServerOption { return gogogrpc.WithServiceDescWrapper(func(desc *grpc.ServiceDesc, _ interface{}) { authDesc := lookupServiceAuthDesc(desc.HandlerType) if authDesc == nil { return } requiredInterface := reflect.TypeOf(authDesc.AuthHandlerType) for requiredInterface.Kind() == reflect.Ptr { requiredInterface = requiredInterface.Elem() } handlerRV := reflect.ValueOf(handler) handlerRT := handlerRV.Type() if !handlerRT.Implements(requiredInterface) { panic(fmt.Sprintf("WithAuthGuard(handler %s) should implement %s", handlerRT.Name(), requiredInterface.Name())) } for i, methodDesc := range desc.Methods { methodAuthDesc := authDesc.Methods[i] if methodAuthDesc.Authorize != nil || methodAuthDesc.Authenticate != nil { wrapMethodWithAuth(methodAuthDesc, &methodDesc, handler) desc.Methods[i] = methodDesc } } for i, streamDesc := range desc.Streams { streamAuthDesc := authDesc.Streams[i] if streamAuthDesc.Authorize != nil || streamAuthDesc.Authenticate != nil { wrapStreamWithAuth(streamAuthDesc, &streamDesc, handler) desc.Streams[i] = streamDesc } } }) }