go-sdk/service/grpc
Phil Kedy 8a7a61b443
Decode CloudEvent Data consistently between HTTP and gRPC (#230)
* Decode CloudEvent Data consistently between HTTP and gRPC

Signed-off-by: Phil Kedy <phil.kedy@gmail.com>

* linter issue

Signed-off-by: Phil Kedy <phil.kedy@gmail.com>

* Added parsing media type

Signed-off-by: Phil Kedy <phil.kedy@gmail.com>

* Adding extension json media type handling

Signed-off-by: Phil Kedy <phil.kedy@gmail.com>

* add TTL to actor timer/reminder requests (#225)

Signed-off-by: Phil Kedy <phil.kedy@gmail.com>

* Adding more tests

Signed-off-by: Phil Kedy <phil.kedy@gmail.com>

* Tweak

Signed-off-by: Phil Kedy <phil.kedy@gmail.com>

Co-authored-by: Dmitry Shmulevich <dmitry.shmulevich@gmail.com>
2021-12-08 21:06:35 -08:00
..
Readme.md fixed typos (#172) 2021-06-18 15:04:11 -07:00
binding.go Use proto packages that exist in dapr/dapr instead of generating them locally in the SDK. This enables the ability to embed Dapr and also use the SDK to interact with the Dapr APIs. Also moved go mod to 1.17. (#211) 2021-10-17 11:23:06 +08:00
binding_test.go Use proto packages that exist in dapr/dapr instead of generating them locally in the SDK. This enables the ability to embed Dapr and also use the SDK to interact with the Dapr APIs. Also moved go mod to 1.17. (#211) 2021-10-17 11:23:06 +08:00
invoke.go Use proto packages that exist in dapr/dapr instead of generating them locally in the SDK. This enables the ability to embed Dapr and also use the SDK to interact with the Dapr APIs. Also moved go mod to 1.17. (#211) 2021-10-17 11:23:06 +08:00
invoke_test.go Use proto packages that exist in dapr/dapr instead of generating them locally in the SDK. This enables the ability to embed Dapr and also use the SDK to interact with the Dapr APIs. Also moved go mod to 1.17. (#211) 2021-10-17 11:23:06 +08:00
service.go Feat: Add Go-sdk Actor Support (#196) 2021-11-02 14:45:27 -07:00
service_test.go adds grpc serving error tests (#102) 2020-11-04 08:56:52 -08:00
topic.go Decode CloudEvent Data consistently between HTTP and gRPC (#230) 2021-12-08 21:06:35 -08:00
topic_test.go Decode CloudEvent Data consistently between HTTP and gRPC (#230) 2021-12-08 21:06:35 -08:00

Readme.md

Dapr gRPC Service SDK for Go

Start by importing Dapr Go service/grpc package:

daprd "github.com/dapr/go-sdk/service/grpc"

Creating and Starting Service

To create a gRPC Dapr service, first, create a Dapr callback instance with a specific address:

s, err := daprd.NewService(":50001")
if err != nil {
    log.Fatalf("failed to start the server: %v", err)
}

Or with address and an existing net.Listener in case you want to combine existing server listener:

list, err := net.Listen("tcp", "localhost:0")
if err != nil {
	log.Fatalf("gRPC listener creation failed: %s", err)
}
s := daprd.NewServiceWithListener(list)

Once you create a service instance, you can "attach" to that service any number of event, binding, and service invocation logic handlers as shown below. Onces the logic is defined, you are ready to start the service:

if err := s.Start(); err != nil {
    log.Fatalf("server error: %v", err)
}

Event Handling

To handle events from specific topic you need to add at least one topic event handler before starting the service:

sub := &common.Subscription{
		PubsubName: "messages",
		Topic:      "topic1",
	}
if err := s.AddTopicEventHandler(sub, eventHandler); err != nil {
    log.Fatalf("error adding topic subscription: %v", err)
}

The handler method itself can be any method with the expected signature:

func eventHandler(ctx context.Context, e *common.TopicEvent) (retry bool, err error) {
	log.Printf("event - PubsubName:%s, Topic:%s, ID:%s, Data: %v", e.PubsubName, e.Topic, e.ID, e.Data)
	// do something with the event
	return true, nil
}

Service Invocation Handler

To handle service invocations you will need to add at least one service invocation handler before starting the service:

if err := s.AddServiceInvocationHandler("echo", echoHandler); err != nil {
    log.Fatalf("error adding invocation handler: %v", err)
}

The handler method itself can be any method with the expected signature:

func echoHandler(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
	log.Printf("echo - ContentType:%s, Verb:%s, QueryString:%s, %+v", in.ContentType, in.Verb, in.QueryString, string(in.Data))
	// do something with the invocation here 
	out = &common.Content{
		Data:        in.Data,
		ContentType: in.ContentType,
		DataTypeURL: in.DataTypeURL,
	}
	return
}

Binding Invocation Handler

To handle binding invocations you will need to add at least one binding invocation handler before starting the service:

if err := s.AddBindingInvocationHandler("run", runHandler); err != nil {
    log.Fatalf("error adding binding handler: %v", err)
}

The handler method itself can be any method with the expected signature:

func runHandler(ctx context.Context, in *common.BindingEvent) (out []byte, err error) {
	log.Printf("binding - Data:%v, Meta:%v", in.Data, in.Metadata)
	// do something with the invocation here 
	return nil, nil
}

Templates

To accelerate your gRPC Dapr app development in Go even further you can use one of the GitHub templates integrating the gRPC Dapr callback package:

Contributing to Dapr Go client

See the Contribution Guide to get started with building and developing.