Skip to content

Commit

Permalink
Merge branch 'main' into ethanperry/update-callout
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan committed Nov 9, 2023
2 parents 33093c9 + e0606e2 commit f3f7b1d
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 79 deletions.
4 changes: 4 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ethanperry1
jlian
ryanwinter
udtri
4 changes: 2 additions & 2 deletions samples/dapr-quickstart-go/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module dapr-quickstart-go

go 1.20
go 1.21

require github.com/dapr/go-sdk v1.9.1

Expand All @@ -17,4 +17,4 @@ require (
google.golang.org/grpc v1.57.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
)
7 changes: 6 additions & 1 deletion samples/dapr-quickstart-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ github.com/dapr/dapr v1.12.0-rc.4/go.mod h1:JZGZh8T0rz75DZBX3zGESi1p9IWWM0ZAGAza
github.com/dapr/go-sdk v1.9.1 h1:f5gV8HtGz6iBJSsh6eI+/Ews4sGC3W9gX0/oD9ANVqM=
github.com/dapr/go-sdk v1.9.1/go.mod h1:bK9bNEsC6hY3RMKh69r0nBjLqb6njeWTEGVMOgP9g20=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand All @@ -20,6 +23,7 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand All @@ -40,5 +44,6 @@ google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
113 changes: 37 additions & 76 deletions samples/dapr-quickstart-go/main.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

package main

import (
"context"
"encoding/json"
"errors"
"log"
"net/http"
"strconv"
"time"

dapr "github.com/dapr/go-sdk/client"
"github.com/dapr/go-sdk/service/common"
Expand All @@ -32,63 +20,50 @@ type OrderDetails struct {
Name string `json:"item"`
}

var (
c dapr.Client
err error
ctx context.Context
const (
stateStoreComponentName = "aio-mq-statestore"
pubSubComponentName = "aio-mq-pubsub"
daprServerPort = ":6001"
)

var ordersSub = &common.Subscription{
PubsubName: "aio-mq-pubsub",
var ordersSubscription = &common.Subscription{
PubsubName: pubSubComponentName,
Topic: "orders",
Route: "/orders",
Route: "/some-orders",
}

var oddOrdersSub = &common.Subscription{
PubsubName: "aio-mq-pubsub",
var oddOrdersSubscription = &common.Subscription{
PubsubName: pubSubComponentName,
Topic: "odd-numbered-orders",
Route: "/odd-orders",
}

func init() {
// wait for sidecar
time.Sleep(13 * time.Second)
Route: "/odd-numbered-orders",
}

func main() {
// create a Dapr service for subscribing
server := daprd.NewService(daprServerPort)

c, err := dapr.NewClient()
// create a Dapr client for publishing
client, err := dapr.NewClient()
if err != nil {
panic(err)
}

// create a Dapr service
s := daprd.NewService(":6001")

if err := s.AddTopicEventHandler(ordersSub, eventHandler(c)); err != nil {

if err := server.AddTopicEventHandler(ordersSubscription, ordersEventHandler(client)); err != nil {
log.Fatalf("error adding topic subscription: %v", err)
}

if err := s.AddTopicEventHandler(oddOrdersSub, oddOrdersHandler(c)); err != nil {

if err := server.AddTopicEventHandler(oddOrdersSubscription, oddOrdersEventHandler(client)); err != nil {
log.Fatalf("error adding topic subscription: %v", err)
}

// add a service to service invocation handler
if err := s.AddServiceInvocationHandler("/get-order", orderGetter(c)); err != nil {
log.Fatalf("error adding invocation handler: %v", err)
}

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

func eventHandler(c dapr.Client) common.TopicEventHandler {

func ordersEventHandler(client dapr.Client) common.TopicEventHandler {
return func(ctx context.Context, e *common.TopicEvent) (retry bool, err error) {
log.Printf("event - PubsubName:%s, Topic:%s, ID:%s, Data: %s", e.PubsubName, e.Topic, e.ID, e.Data)
log.Printf("orders event - PubsubName: %s, Topic: %s, ID: %s, Data: %s", e.PubsubName, e.Topic, e.ID, e.Data)

var order OrderDetails
s, _ := strconv.Unquote(string(e.RawData))
Expand All @@ -103,7 +78,7 @@ func eventHandler(c dapr.Client) common.TopicEventHandler {
if order.Number%2 != 0 {

// PubsubName seems to be in e.Topic when published by a pure MQTT client
if err := c.PublishEvent(ctx, e.Topic, "odd-numbered-orders", e.Data); err != nil {
if err := client.PublishEvent(ctx, e.Topic, "odd-numbered-orders", e.Data); err != nil {
panic(err)
}
log.Printf("Published to odd-numbered-orders: %v", e)
Expand All @@ -113,10 +88,10 @@ func eventHandler(c dapr.Client) common.TopicEventHandler {
}
}

func oddOrdersHandler(c dapr.Client) common.TopicEventHandler {

func oddOrdersEventHandler(client dapr.Client) common.TopicEventHandler {
return func(ctx context.Context, e *common.TopicEvent) (retry bool, err error) {
log.Printf("event - PubsubName:%s, Topic:%s, ID:%s, Data: %s", e.PubsubName, e.Topic, e.ID, e.Data)
log.Printf("addOrders event - PubsubName: %s, Topic: %s, ID: %s, Data: %s", e.PubsubName, e.Topic, e.ID, e.Data)

var order OrderDetails
err = json.Unmarshal([]byte(e.RawData), &order)
if err != nil {
Expand All @@ -125,42 +100,28 @@ func oddOrdersHandler(c dapr.Client) common.TopicEventHandler {
}
log.Printf("Odd order number is %d with name %s", order.Number, order.Name)

STATE_STORE_NAME := "aio-mq-statestore"
if err := c.SaveState(ctx, STATE_STORE_NAME, strconv.FormatUint(uint64(order.Number), 10), []byte(order.Name), nil); err != nil {
// save to the state store
if err := client.SaveState(ctx, stateStoreComponentName, strconv.FormatUint(uint64(order.Number), 10), []byte(order.Name), nil); err != nil {
log.Printf("Error saving state: %v", err)
return true, err
}

log.Printf("Saved order #%d", order.Number)

return false, nil
}
}

func orderGetter(c dapr.Client) common.ServiceInvocationHandler {
return func(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
if in == nil {
err = errors.New("invocation parameter required")
return
}
log.Printf(
"echo - ContentType:%s, Verb:%s, QueryString:%s, %s",
in.ContentType, in.Verb, in.QueryString, in.Data,
)

STATE_STORE_NAME := "aio-mq-statestore"
result, _ := c.GetState(ctx, STATE_STORE_NAME, string(in.Data), nil)

out = &common.Content{
Data: result.Value,
ContentType: in.ContentType,
DataTypeURL: in.DataTypeURL,
// get from the state store
state, err := client.GetState(ctx, stateStoreComponentName, strconv.FormatUint(uint64(order.Number), 10), nil);
if err != nil {
log.Printf("Error getting state: %v", err)
return true, err
}
return

log.Printf("Got order #%d, state %s", order.Number, state.Value)

return false, nil
}
}

func runHandler(ctx context.Context, in *common.BindingEvent) (out []byte, err error) {
log.Printf("binding - Data:%s, Meta:%v", in.Data, in.Metadata)
return nil, nil
}
}
Loading

0 comments on commit f3f7b1d

Please sign in to comment.