/
main.go
91 lines (75 loc) · 2.39 KB
/
main.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"context"
"log"
"os"
"crypto/tls"
"crypto/x509"
"github.com/ktenzer/temporal-demo-apps/helloworld"
"go.temporal.io/sdk/client"
)
func main() {
// The client and worker are heavyweight objects that should be created once per process.
clientOptions := client.Options{
HostPort: os.Getenv("TEMPORAL_HOST_URL"),
Namespace: os.Getenv("TEMPORAL_NAMESPACE"),
}
if os.Getenv("TEMPORAL_MTLS_TLS_CERT") != "" && os.Getenv("TEMPORAL_MTLS_TLS_KEY") != "" {
if os.Getenv("TEMPORAL_MTLS_TLS_CA") != "" {
caCert, err := os.ReadFile(os.Getenv("TEMPORAL_MTLS_TLS_CA"))
if err != nil {
log.Fatalln("failed reading server CA's certificate", err)
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(caCert) {
log.Fatalln("failed to add server CA's certificate", err)
}
cert, err := tls.LoadX509KeyPair(os.Getenv("TEMPORAL_MTLS_TLS_CERT"), os.Getenv("TEMPORAL_MTLS_TLS_KEY"))
if err != nil {
log.Fatalln("Unable to load certs", err)
}
var serverName string
if os.Getenv("TEMPORAL_MTLS_TLS_ENABLE_HOST_VERIFICATION") == "true" {
serverName = os.Getenv("TEMPORAL_MTLS_TLS_SERVER_NAME")
}
clientOptions.ConnectionOptions = client.ConnectionOptions{
TLS: &tls.Config{
RootCAs: certPool,
Certificates: []tls.Certificate{cert},
ServerName: serverName,
},
}
} else {
cert, err := tls.LoadX509KeyPair(os.Getenv("TEMPORAL_MTLS_TLS_CERT"), os.Getenv("TEMPORAL_MTLS_TLS_KEY"))
if err != nil {
log.Fatalln("Unable to load certs", err)
}
clientOptions.ConnectionOptions = client.ConnectionOptions{
TLS: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}
}
}
c, err := client.Dial(clientOptions)
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()
workflowOptions := client.StartWorkflowOptions{
ID: "hello_world_workflowID",
TaskQueue: "hello-world",
}
we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, helloworld.Workflow, "Temporal")
if err != nil {
log.Fatalln("Unable to execute workflow", err)
}
log.Println("Started workflow", "WorkflowID", we.GetID(), "RunID", we.GetRunID())
// Synchronously wait for the workflow completion.
var result string
err = we.Get(context.Background(), &result)
if err != nil {
log.Fatalln("Unable get workflow result", err)
}
log.Println("Workflow result:", result)
}