/
daprstatestore.go
33 lines (30 loc) · 1.26 KB
/
daprstatestore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package bindings
import (
"context"
"log"
"os"
dapr "github.com/dapr/go-sdk/client"
)
// DaprStateStoreBinding checks if the environment parameter COMPONENTNAME is set and if so, creates a Dapr client, saves a
// value to the state store and returns a BindingStatus indicating success or failure.
func DaprStateStoreBinding(envParams map[string]string) BindingStatus {
// From https://docs.dapr.io/developing-applications/sdks/go/go-client/
stateStoreName := envParams["COMPONENTNAME"]
if stateStoreName == "" {
log.Println("COMPONENTNAME is required")
return BindingStatus{false, "COMPONENTNAME is required"}
}
client, err := dapr.NewClientWithPort(os.Getenv("DAPR_GRPC_PORT"))
if err != nil {
log.Println("failed to create Dapr client - ", err.Error())
return BindingStatus{false, "Error creating Dapr client"}
}
defer client.Close()
ctx := context.Background()
if err := client.SaveState(ctx, stateStoreName, "key", []byte("value"), map[string]string{}); err != nil {
log.Println("failed to save to the Dapr state store - ", stateStoreName, " error - ", err.Error())
return BindingStatus{false, "failed to save to the Dapr state store"}
}
log.Println("successfully saved to the state store - ", stateStoreName)
return BindingStatus{true, "message sent"}
}