Getting started with the Dapr Service (Callback) SDK for Go

How to get up and running with the Dapr Service (Callback) SDK for Go

Dapr gRPC Service SDK for Go

Prerequisite

Start by importing Dapr Go service/grpc package:

  1. 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:

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

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

  1. list, err := net.Listen("tcp", "localhost:0")
  2. if err != nil {
  3. log.Fatalf("gRPC listener creation failed: %s", err)
  4. }
  5. 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:

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

Event Handling

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

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

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

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

Optionally, you can use routing rules to send messages to different handlers based on the contents of the CloudEvent.

  1. sub := &common.Subscription{
  2. PubsubName: "messages",
  3. Topic: "topic1",
  4. Route: "/important",
  5. Match: `event.type == "important"`,
  6. Priority: 1,
  7. }
  8. err := s.AddTopicEventHandler(sub, importantHandler)
  9. if err != nil {
  10. log.Fatalf("error adding topic subscription: %v", err)
  11. }

Service Invocation Handler

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

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

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

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

Binding Invocation Handler

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

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

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

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