forked from dapr/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Dapr Docs for Go SDK * Fix section titles * Update grpc-service.md * Update cards links
- Loading branch information
1 parent
4122595
commit a9c6bcb
Showing
7 changed files
with
523 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Dapr Go SDK documentation | ||
|
||
This page covers how the documentation is structured for the Dapr Go SDK | ||
|
||
## Dapr Docs | ||
|
||
All Dapr documentation is hosted at [docs.dapr.io](https://docs.dapr.io), including the docs for the [Go SDK](https://docs.dapr.io/developing-applications/sdks/go/). Head over there if you want to read the docs. | ||
|
||
### Go SDK docs source | ||
|
||
Although the docs site code and content is in the [docs repo](https://github.com/dapr/docs), the Go SDK content and images are within the `content` and `static` directories, respectively. | ||
|
||
This allows separation of roles and expertise between maintainers, and makes it easy to find the docs files you are looking for. | ||
|
||
## Writing Go SDK docs | ||
|
||
To get up and running to write Go SDK docs, visit the [docs repo](https://github.com/dapr/docs) to initialize your environment. It will clone both the docs repo and this repo, so you can make changes and see it rendered within the site instantly, as well as commit and PR into this repo. | ||
|
||
Make sure to read the [docs contributing guide](https://docs.dapr.io/contributing/contributing-docs/) for information on style/semantics/etc. | ||
|
||
## Docs architecture | ||
|
||
The docs site is built on [Hugo](https://gohugo.io), which lives in the docs repo. This repo is setup as a git submodule so that when the repo is cloned and initialized, the dotnet-sdk repo, along with the docs, are cloned as well. | ||
|
||
Then, in the Hugo configuration file, the `daprdocs/content` and `daprdocs/static` directories are redirected to the `daprdocs/developing-applications/sdks/go` and `static/go` directories, respectively. Thus, all the content within this repo is folded into the main docs site. |
23 changes: 23 additions & 0 deletions
23
daprdocs/content/en/go-sdk-contributing/go-contributing.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
type: docs | ||
title: "Contributing to the Go SDK" | ||
linkTitle: "Go SDK" | ||
weight: 3000 | ||
description: Guidelines for contributing to the Dapr Go SDK | ||
--- | ||
|
||
When contributing to the [Go SDK](https://github.com/dapr/go-sdk) the following rules and best-practices should be followed. | ||
|
||
## Examples | ||
|
||
The `examples` directory contains code samples for users to run to try out specific functionality of the various Go SDK packages and extensions. When writing new and updated samples keep in mind: | ||
|
||
- All examples should be runnable on Windows, Linux, and MacOS. While Go code is consistent among operating systems, any pre/post example commands should provide options through [codetabs]({{< ref "contributing-docs.md#tabbed-content" >}}) | ||
- Contain steps to download/install any required pre-requisites. Someone coming in with a fresh OS install should be able to start on the example and complete it without an error. Links to external download pages are fine. | ||
|
||
## Docs | ||
|
||
The `daprdocs` directory contains the markdown files that are rendered into the [Dapr Docs](https://docs.dapr.io) website. When the documentation website is built this repo is cloned and configured so that its contents are rendered with the docs content. When writing docs keep in mind: | ||
|
||
- All rules in the [docs guide]({{< ref contributing-docs.md >}}) should be followed in addition to these. | ||
- All files and directories should be prefixed with `go-` to ensure all file/directory names are globally unique across all Dapr documentation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
type: docs | ||
title: "Dapr Go SDK" | ||
linkTitle: "Go" | ||
weight: 1000 | ||
description: Go SDK packages for developing Dapr applications | ||
no_list: true | ||
--- | ||
|
||
A client library to help build Dapr applications in Go. This client supports all public Dapr APIs while focusing on idiomatic Go experiences and developer productivity. | ||
|
||
{{< cardpane >}} | ||
{{< card title="**Client**">}} | ||
Use the Go Client SDK for invoking public Dapr APIs | ||
|
||
[**Learn more about the Go Client SDK**]({{< ref go-client >}}) | ||
{{< /card >}} | ||
{{< card title="**Service**">}} | ||
Use the Dapr Service (Callback) SDK for Go to create services that will be invoked by Dapr. | ||
|
||
[**Learn more about the Go Service (Callback) SDK**]({{< ref go-service >}}) | ||
{{< /card >}} | ||
{{< /cardpane >}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
--- | ||
type: docs | ||
title: "Getting started with the Dapr client Go SDK" | ||
linkTitle: "Client" | ||
weight: 20000 | ||
description: How to get up and running with the Dapr Go SDK | ||
no_list: true | ||
--- | ||
|
||
The Dapr client package allows you to interact with other Dapr applications from a Go application. | ||
|
||
## Prerequisites | ||
|
||
- [Dapr CLI]({{< ref install-dapr-cli.md >}}) installed | ||
- Initialized [Dapr environment]({{< ref install-dapr-selfhost.md >}}) | ||
- [Go installed](https://golang.org/doc/install) | ||
|
||
|
||
## Import the client package | ||
```go | ||
import "github.com/dapr/go-sdk/client" | ||
``` | ||
|
||
## Building blocks | ||
|
||
The Go SDK allows you to interface with all of the [Dapr building blocks]({{< ref building-blocks >}}). | ||
|
||
### Service Invocation | ||
|
||
To invoke a specific method on another service running with Dapr sidecar, the Dapr client Go SDK provides two options: | ||
|
||
Invoke a service without data: | ||
```go | ||
resp, err := client.InvokeMethod(ctx, "app-id", "method-name", "post") | ||
``` | ||
|
||
Invoke a service with data: | ||
```go | ||
content := &dapr.DataContent{ | ||
ContentType: "application/json", | ||
Data: []byte(`{ "id": "a123", "value": "demo", "valid": true }`), | ||
} | ||
|
||
resp, err = client.InvokeMethodWithContent(ctx, "app-id", "method-name", "post", content) | ||
``` | ||
|
||
- For a full guide on service invocation visit [How-To: Invoke a service]({{< ref howto-invoke-discover-services.md >}}). | ||
|
||
### State Management | ||
|
||
For simple use-cases, Dapr client provides easy to use `Save`, `Get`, `Delete` methods: | ||
|
||
```go | ||
ctx := context.Background() | ||
data := []byte("hello") | ||
store := "my-store" // defined in the component YAML | ||
|
||
// save state with the key key1, default options: strong, last-write | ||
if err := client.SaveState(ctx, store, "key1", data); err != nil { | ||
panic(err) | ||
} | ||
|
||
// get state for key key1 | ||
item, err := client.GetState(ctx, store, "key1") | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("data [key:%s etag:%s]: %s", item.Key, item.Etag, string(item.Value)) | ||
|
||
// delete state for key key1 | ||
if err := client.DeleteState(ctx, store, "key1"); err != nil { | ||
panic(err) | ||
} | ||
``` | ||
|
||
For more granular control, the Dapr Go client exposes `SetStateItem` type, which can be use to gain more control over the state operations and allow for multiple items to be saved at once: | ||
|
||
```go | ||
item1 := &dapr.SetStateItem{ | ||
Key: "key1", | ||
Etag: &ETag{ | ||
Value: "1", | ||
}, | ||
Metadata: map[string]string{ | ||
"created-on": time.Now().UTC().String(), | ||
}, | ||
Value: []byte("hello"), | ||
Options: &dapr.StateOptions{ | ||
Concurrency: dapr.StateConcurrencyLastWrite, | ||
Consistency: dapr.StateConsistencyStrong, | ||
}, | ||
} | ||
|
||
item2 := &dapr.SetStateItem{ | ||
Key: "key2", | ||
Metadata: map[string]string{ | ||
"created-on": time.Now().UTC().String(), | ||
}, | ||
Value: []byte("hello again"), | ||
} | ||
|
||
item3 := &dapr.SetStateItem{ | ||
Key: "key3", | ||
Etag: &dapr.ETag{ | ||
Value: "1", | ||
}, | ||
Value: []byte("hello again"), | ||
} | ||
|
||
if err := client.SaveBulkState(ctx, store, item1, item2, item3); err != nil { | ||
panic(err) | ||
} | ||
``` | ||
|
||
Similarly, `GetBulkState` method provides a way to retrieve multiple state items in a single operation: | ||
|
||
```go | ||
keys := []string{"key1", "key2", "key3"} | ||
items, err := client.GetBulkState(ctx, store, keys, nil,100) | ||
``` | ||
|
||
And the `ExecuteStateTransaction` method to execute multiple upsert or delete operations transactionally. | ||
|
||
```go | ||
ops := make([]*dapr.StateOperation, 0) | ||
|
||
op1 := &dapr.StateOperation{ | ||
Type: dapr.StateOperationTypeUpsert, | ||
Item: &dapr.SetStateItem{ | ||
Key: "key1", | ||
Value: []byte(data), | ||
}, | ||
} | ||
op2 := &dapr.StateOperation{ | ||
Type: dapr.StateOperationTypeDelete, | ||
Item: &dapr.SetStateItem{ | ||
Key: "key2", | ||
}, | ||
} | ||
ops = append(ops, op1, op2) | ||
meta := map[string]string{} | ||
err := testClient.ExecuteStateTransaction(ctx, store, meta, ops) | ||
``` | ||
|
||
### Publish Messages | ||
To publish data onto a topic, the Dapr Go client provides a simple method: | ||
|
||
```go | ||
data := []byte(`{ "id": "a123", "value": "abcdefg", "valid": true }`) | ||
if err := client.PublishEvent(ctx, "component-name", "topic-name", data); err != nil { | ||
panic(err) | ||
} | ||
``` | ||
|
||
- For a full list of state operations visit [How-To: Publish & subscribe]({{< ref howto-publish-subscribe.md >}}). | ||
|
||
### Output Bindings | ||
The Dapr Go client SDK provides two methods to invoke an operation on a Dapr-defined binding. Dapr supports input, output, and bidirectional bindings. | ||
|
||
For simple, output only biding: | ||
```go | ||
in := &dapr.InvokeBindingRequest{ Name: "binding-name", Operation: "operation-name" } | ||
err = client.InvokeOutputBinding(ctx, in) | ||
``` | ||
To invoke method with content and metadata: | ||
```go | ||
in := &dapr.InvokeBindingRequest{ | ||
Name: "binding-name", | ||
Operation: "operation-name", | ||
Data: []byte("hello"), | ||
Metadata: map[string]string{"k1": "v1", "k2": "v2"}, | ||
} | ||
|
||
out, err := client.InvokeBinding(ctx, in) | ||
``` | ||
|
||
|
||
- For a full guide on output bindings visit [How-To: Use bindings]({{< ref howto-bindings.md >}}). | ||
|
||
### Secret Management | ||
|
||
The Dapr client also provides access to the runtime secrets that can be backed by any number of secrete stores (e.g. Kubernetes Secrets, HashiCorp Vault, or Azure KeyVault): | ||
|
||
```go | ||
opt := map[string]string{ | ||
"version": "2", | ||
} | ||
|
||
secret, err := client.GetSecret(ctx, "store-name", "secret-name", opt) | ||
``` | ||
|
||
### Authentication | ||
|
||
By default, Dapr relies on the network boundary to limit access to its API. If however the target Dapr API is configured with token-based authentication, users can configure the Go Dapr client with that token in two ways: | ||
|
||
**Environment Variable** | ||
|
||
If the DAPR_API_TOKEN environment variable is defined, Dapr will automatically use it to augment its Dapr API invocations to ensure authentication. | ||
|
||
**Explicit Method** | ||
|
||
In addition, users can also set the API token explicitly on any Dapr client instance. This approach is helpful in cases when the user code needs to create multiple clients for different Dapr API endpoints. | ||
|
||
```go | ||
func main() { | ||
client, err := dapr.NewClient() | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer client.Close() | ||
client.WithAuthToken("your-Dapr-API-token-here") | ||
} | ||
``` | ||
|
||
|
||
- For a full guide on secrets visit [How-To: Retrieve secrets]({{< ref howto-secrets.md >}}). | ||
|
||
## Related links | ||
- [Go SDK Examples](https://github.com/dapr/go-sdk/tree/main/examples) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
type: docs | ||
title: "Getting started with the Dapr Service (Callback) SDK for Go" | ||
linkTitle: "Service" | ||
weight: 20000 | ||
description: How to get up and running with the Dapr Service (Callback) SDK for Go | ||
no_list: true | ||
--- | ||
In addition to this Dapr API client, Dapr Go SDK also provides service package to bootstrap your Dapr callback services. These services can be developed in either gRPC or HTTP: | ||
- [HTTP Service]({{< ref http-service.md >}}) | ||
- [gRPC Service]({{< ref grpc-service.md >}}) |
Oops, something went wrong.