go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/config/temporal.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package config
     9  
    10  import (
    11  	"context"
    12  
    13  	"go.charczuk.com/sdk/certutil"
    14  	"go.charczuk.com/sdk/configutil"
    15  	"go.temporal.io/sdk/client"
    16  )
    17  
    18  // Temporal represent options for connecting to a temporal server.
    19  //
    20  // This is used by both a worker and servers submitting work tasks.
    21  type Temporal struct {
    22  	HostPort              string           `yaml:"hostport"`
    23  	Namespace             string           `yaml:"namespace"`
    24  	Identity              string           `yaml:"identity"`
    25  	Credentials           certutil.KeyPair `yaml:"credentials"`
    26  	TLSServerName         string           `yaml:"tlsServerName"`
    27  	TLSInsecureSkipVerify bool             `yaml:"tlsInsecureSkipVerify"`
    28  }
    29  
    30  func (t *Temporal) Resolve(ctx context.Context) error {
    31  	return configutil.Resolve(ctx,
    32  		configutil.Set(&t.HostPort, configutil.Env[string]("TEMPORAL_HOSTPORT"), configutil.Lazy(&t.HostPort), configutil.Const("localhost:7233")),
    33  		configutil.Set(&t.Namespace, configutil.Env[string]("TEMPORAL_NAMESPACE"), configutil.Lazy(&t.Namespace), configutil.Const("default")),
    34  		configutil.Set(&t.Credentials.CertBase64, configutil.Env[string]("TLS_CLIENT_CERT"), configutil.Lazy(&t.Credentials.CertBase64)),
    35  		configutil.Set(&t.Credentials.KeyBase64, configutil.Env[string]("TLS_CLIENT_KEY"), configutil.Lazy(&t.Credentials.KeyBase64)),
    36  		configutil.Set(&t.TLSServerName, configutil.Env[string]("TLS_SERVER_NAME"), configutil.Const[string]("nodes-temporal.internal")),
    37  		configutil.Set(&t.TLSInsecureSkipVerify, configutil.Env[bool]("TLS_INSECURE_SKIP_VERIFY"), configutil.Const[bool](false)),
    38  	)
    39  }
    40  
    41  // Client returns a temporal client for the config.
    42  func (t Temporal) Client() (client.Client, error) {
    43  	options := client.Options{
    44  		HostPort:  t.HostPort,
    45  		Namespace: t.Namespace,
    46  		Identity:  t.Identity,
    47  	}
    48  	if !t.Credentials.IsZero() {
    49  		tlsConfig, err := t.Credentials.TLSConfig()
    50  		if err != nil {
    51  			return nil, err
    52  		}
    53  		tlsConfig.ServerName = t.TLSServerName
    54  		tlsConfig.InsecureSkipVerify = t.TLSInsecureSkipVerify
    55  		options.ConnectionOptions = client.ConnectionOptions{
    56  			TLS: tlsConfig,
    57  		}
    58  	}
    59  	return client.Dial(options)
    60  }