mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package temporalclient
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
|
|
"github.com/formancehq/orchestration/internal/workflow"
|
|
"github.com/formancehq/stack/libs/go-libs/logging"
|
|
"go.temporal.io/sdk/client"
|
|
"go.temporal.io/sdk/contrib/opentelemetry"
|
|
"go.temporal.io/sdk/interceptor"
|
|
"go.uber.org/fx"
|
|
)
|
|
|
|
func NewModule(address, namespace string, certStr string, key string) fx.Option {
|
|
return fx.Options(
|
|
fx.Provide(func(logger logging.Logger) (client.Options, error) {
|
|
|
|
var cert *tls.Certificate
|
|
if key != "" && certStr != "" {
|
|
clientCert, err := tls.X509KeyPair([]byte(certStr), []byte(key))
|
|
if err != nil {
|
|
return client.Options{}, err
|
|
}
|
|
cert = &clientCert
|
|
}
|
|
|
|
tracingInterceptor, err := opentelemetry.NewTracingInterceptor(opentelemetry.TracerOptions{
|
|
Tracer: workflow.Tracer,
|
|
})
|
|
if err != nil {
|
|
return client.Options{}, err
|
|
}
|
|
|
|
options := client.Options{
|
|
Namespace: namespace,
|
|
HostPort: address,
|
|
Interceptors: []interceptor.ClientInterceptor{tracingInterceptor},
|
|
Logger: newLogger(logger),
|
|
}
|
|
if cert != nil {
|
|
options.ConnectionOptions = client.ConnectionOptions{
|
|
TLS: &tls.Config{Certificates: []tls.Certificate{*cert}},
|
|
}
|
|
}
|
|
return options, nil
|
|
}),
|
|
fx.Provide(client.Dial),
|
|
fx.Invoke(func(lifecycle fx.Lifecycle, c client.Client) {
|
|
lifecycle.Append(fx.Hook{
|
|
OnStop: func(ctx context.Context) error {
|
|
c.Close()
|
|
return nil
|
|
},
|
|
})
|
|
}),
|
|
)
|
|
}
|