Giter Club home page Giter Club logo

go-grpc-info's Introduction

service and method info for grpc-go interceptors

Build Status

Getting started

import (
	"github.com/prasek/protoer/proto"
	"github.com/prasek/protoer/proto/gogo"
	"github.com/prasek/go-grpc-info/grpcinfo"
)

func main() {
	proto.SetProtoer(gogo.NewProtoer(nil))

	reg := grpcinfo.NewRegistry()
	server := grpc.NewServer(grpc.UnaryInterceptor(UnaryInterceptor(reg)))
	RegisterTestServiceServer(server, &TestService{})

	l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
	if err != nil {
		log.Printf("%v", err)
		os.Exit(1)
	}

	//after services are registered
	err = reg.Load(server)
	if err != nil {
		log.Printf("%v", err)
		os.Exit(1)
	}

	server.Serve(l)
}

type TestService struct{}

func (s *TestService) Simple(ctx context.Context, req *TestRequest) (*TestResponse, error) {
	out := &TestResponse{Foo: req.Foo, Bar: req.Bar}

	mi := ToMethodInfo(ctx)
	if mi == nil {
		return out, nil
	}

	out.Sopt1 = mi.Service().GetBoolExtension(E_Sopt1, false)
	out.Mopt1 = mi.Method().GetBoolExtension(E_Mopt1, false)
	mopt2, _ := mi.Method().GetExtension(E_Mopt2)
	out.Mopt2, _ = mopt2.(*CustomOption)
	return out, nil
}

func UnaryInterceptor(reg grpcinfo.Registry) grpc.UnaryServerInterceptor {
	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
		mi := reg.GetMethodInfo(info.FullMethod)
		ctx = WithMethodInfo(ctx, mi)
		return handler(ctx, req)
	}
}

type ctxKey struct{}

func WithMethodInfo(ctx context.Context, mi grpcinfo.MethodInfo) context.Context {
	return context.WithValue(ctx, ctxKey{}, mi)
}

func ToMethodInfo(ctx context.Context) grpcinfo.MethodInfo {
	if mi, ok := ctx.Value(ctxKey{}).(grpcinfo.MethodInfo); ok {
		return mi
	}
	return nil
}

go-grpc-info's People

Contributors

prasek avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.