|
| 1 | +package helloworldapiKey |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "time" |
| 10 | + |
| 11 | + "go.temporal.io/sdk/activity" |
| 12 | + "go.temporal.io/sdk/client" |
| 13 | + "go.temporal.io/sdk/workflow" |
| 14 | + "google.golang.org/grpc" |
| 15 | + "google.golang.org/grpc/metadata" |
| 16 | +) |
| 17 | + |
| 18 | +// Workflow is a Hello World workflow definition. |
| 19 | +func Workflow(ctx workflow.Context, name string) (string, error) { |
| 20 | + ao := workflow.ActivityOptions{ |
| 21 | + StartToCloseTimeout: 10 * time.Second, |
| 22 | + } |
| 23 | + ctx = workflow.WithActivityOptions(ctx, ao) |
| 24 | + |
| 25 | + logger := workflow.GetLogger(ctx) |
| 26 | + logger.Info("HelloWorld workflow started", "name", name) |
| 27 | + |
| 28 | + var result string |
| 29 | + err := workflow.ExecuteActivity(ctx, Activity, name).Get(ctx, &result) |
| 30 | + if err != nil { |
| 31 | + logger.Error("Activity failed.", "Error", err) |
| 32 | + return "", err |
| 33 | + } |
| 34 | + |
| 35 | + logger.Info("HelloWorld workflow completed.", "result", result) |
| 36 | + |
| 37 | + return result, nil |
| 38 | +} |
| 39 | + |
| 40 | +func Activity(ctx context.Context, name string) (string, error) { |
| 41 | + logger := activity.GetLogger(ctx) |
| 42 | + logger.Info("Activity", "name", name) |
| 43 | + return "Hello " + name + "!", nil |
| 44 | +} |
| 45 | + |
| 46 | +// ParseClientOptionFlags parses the given arguments into client options. In |
| 47 | +// some cases a failure will be returned as an error, in others the process may |
| 48 | +// exit with help info. |
| 49 | +func ParseClientOptionFlags(args []string) (client.Options, error) { |
| 50 | + // Parse args |
| 51 | + set := flag.NewFlagSet("hello-world-api-key", flag.ExitOnError) |
| 52 | + targetHost := set.String("target-host", "localhost:7233", "Host:port for the server") |
| 53 | + namespace := set.String("namespace", "default", "Namespace for the server") |
| 54 | + apiKey := set.String("api-key", "", "Optional API key, mutually exclusive with cert/key") |
| 55 | + |
| 56 | + if err := set.Parse(args); err != nil { |
| 57 | + return client.Options{}, fmt.Errorf("failed parsing args: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + if *apiKey == "" { |
| 61 | + *apiKey = os.Getenv("TEMPORAL_CLIENT_API_KEY") |
| 62 | + } |
| 63 | + if *apiKey == "" { |
| 64 | + return client.Options{}, fmt.Errorf("-api-key or TEMPORAL_CLIENT_API_KEY env is required required") |
| 65 | + } |
| 66 | + |
| 67 | + connectionOptions := client.ConnectionOptions{ |
| 68 | + TLS: &tls.Config{}, |
| 69 | + DialOptions: []grpc.DialOption{ |
| 70 | + grpc.WithUnaryInterceptor( |
| 71 | + func(ctx context.Context, method string, req any, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { |
| 72 | + return invoker( |
| 73 | + metadata.AppendToOutgoingContext(ctx, "temporal-namespace", *namespace), |
| 74 | + method, |
| 75 | + req, |
| 76 | + reply, |
| 77 | + cc, |
| 78 | + opts..., |
| 79 | + ) |
| 80 | + }, |
| 81 | + ), |
| 82 | + }, |
| 83 | + } |
| 84 | + credentials := client.NewAPIKeyStaticCredentials(*apiKey) |
| 85 | + |
| 86 | + return client.Options{ |
| 87 | + HostPort: *targetHost, |
| 88 | + Namespace: *namespace, |
| 89 | + ConnectionOptions: connectionOptions, |
| 90 | + Credentials: credentials, |
| 91 | + }, nil |
| 92 | +} |
0 commit comments