-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
68 lines (60 loc) · 1.92 KB
/
client.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
// Package temporalclient provides functionality to create and initialize
// a client for Temporal, a distributed, scalable, durable, and highly available
// orchestration engine. This package supports configuration with optional
// mutual TLS (mTLS) for enhanced security.
//
// The main components are the TemporalConfig struct and the NewTemporalClient
// function, which uses the configuration to create a Temporal client.
package temporalclient // import "github.com/SolomonAIEngineering/backend-core-library/temporal-client"
import (
"crypto/tls"
"go.temporal.io/sdk/client"
"go.uber.org/zap"
)
type TemporalExecutorClient struct {
Client client.Client
Logger *zap.Logger
}
// New creates and initializes a new Temporal client using the provided TemporalConfig.
// This function handles the setup of mTLS based on the configuration and returns an initialized client.
//
// Example:
//
// config := NewTemporalConfig("./ca.key", "./ca.pem", "example_namespace", "localhost:7233", true)
// client, err := NewTemporalClient(config)
// if err != nil {
// log.Fatal(err)
// }
// defer client.Close()
//
// // Use the client...
func New(config *TemporalConfig) (*TemporalExecutorClient, error) {
var tlsConfig *tls.Config
// Load the certificate and key only if mTLS is enabled
if config.MtlsEnabled {
cert, err := tls.LoadX509KeyPair(config.ClientCertPath, config.ClientKeyPath)
if err != nil {
return nil, err
}
tlsConfig = &tls.Config{Certificates: []tls.Certificate{cert}}
}
// Create the Temporal client
temporalClient, err := client.Dial(client.Options{
HostPort: config.HostPort,
Namespace: config.Namespace,
ConnectionOptions: client.ConnectionOptions{
TLS: tlsConfig,
},
})
if err != nil {
return nil, err
}
return &TemporalExecutorClient{
Client: temporalClient,
Logger: config.Logger,
}, nil
}
// Close closes the Temporal client.
func (c *TemporalExecutorClient) Close() {
c.Client.Close()
}