Skip to content

Commit

Permalink
Merge pull request #6 from ryanwinterms/main
Browse files Browse the repository at this point in the history
add dapr quickstart
  • Loading branch information
ryanwinter authored Nov 3, 2023
2 parents 8ccc21e + 628c141 commit 0728fe5
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 0 deletions.
17 changes: 17 additions & 0 deletions samples/dapr-quickstart-go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# syntax=docker/dockerfile:1

## Build
FROM golang:1.21-bullseye AS build
WORKDIR /work
COPY main.go .
COPY go.mod .
COPY go.sum .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -o .

## Deploy
FROM alpine:3.18
WORKDIR /
COPY --from=build work/dapr-quickstart-go /dapr-quickstart-go
EXPOSE 6001
ENTRYPOINT ["/dapr-quickstart-go"]
15 changes: 15 additions & 0 deletions samples/dapr-quickstart-go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Dapr Quickstart Sample

This is a Dapr sample that makes use of the aio-mq-pubsub and aio-mq-statestore pluggable component to interact with the IoT MQ broker and the IoT MQ state store.

## Deploying the app

For instructions on how to deploy and interact with this, refer to [Use Dapr to develop distributed application workloads](https://learn.microsoft.com/azure/iot-operations/develop/howto-develop-dapr-app)

## Building

To build the quickstart image, use the following command:

```
docker build . -t quickstart-sample:0.3 -t quickstart-sample:latest
```
20 changes: 20 additions & 0 deletions samples/dapr-quickstart-go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module dapr-quickstart-go

go 1.20

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

require (
github.com/dapr/dapr v1.12.0-rc.4 // indirect
github.com/go-chi/chi/v5 v5.0.10 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
44 changes: 44 additions & 0 deletions samples/dapr-quickstart-go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/dapr/dapr v1.12.0-rc.4 h1:LOPbekXZ+21HTqlk6Kg4Bf/lFiqq9cRq/IrgZgvK4mM=
github.com/dapr/dapr v1.12.0-rc.4/go.mod h1:JZGZh8T0rz75DZBX3zGESi1p9IWWM0ZAGAzaGMHp+5o=
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/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/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/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=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
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/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=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 h1:wukfNtZmZUurLN/atp2hiIeTKn7QJWIQdHzqmsOnAOk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
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/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
165 changes: 165 additions & 0 deletions samples/dapr-quickstart-go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
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.
*/

package main

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

dapr "github.com/dapr/go-sdk/client"
"github.com/dapr/go-sdk/service/common"
daprd "github.com/dapr/go-sdk/service/http"
)

type OrderDetails struct {
Number uint `json:"orderId"`
Name string `json:"item"`
}

var (
c dapr.Client
err error
ctx context.Context
)

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

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

func init() {
// wait for sidecar
time.Sleep(13 * time.Second)
}

func main() {

c, 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 {

log.Fatalf("error adding topic subscription: %v", err)
}

if err := s.AddTopicEventHandler(oddOrdersSub, oddOrdersHandler(c)); 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 {
log.Fatalf("error listening: %v", err)
}
}

func eventHandler(c 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)

var order OrderDetails
s, _ := strconv.Unquote(string(e.RawData))
err = json.Unmarshal([]byte(s), &order)
if err != nil {
log.Printf("Could not decode data: %v", err)
return false, err
}
log.Printf("Order number is %d with name %s", order.Number, order.Name)

// If the order Id is an odd number, republish on a new topic
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 {
panic(err)
}
log.Printf("Published to odd-numbered-orders: %v", e)
}

return false, nil
}
}

func oddOrdersHandler(c 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)
var order OrderDetails
err = json.Unmarshal([]byte(e.RawData), &order)
if err != nil {
log.Printf("Could not decode data: %v", err)
return false, err
}
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 {
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,
}
return
}
}

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
}

0 comments on commit 0728fe5

Please sign in to comment.