Skip to content

Commit

Permalink
FFM-7010 Use SDK HTTP Client for all requests (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
erdirowlands authored Oct 19, 2023
1 parent 0202715 commit d3d1765
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
2 changes: 1 addition & 1 deletion analyticsservice/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
variationValueAttribute string = "featureValue"
targetAttribute string = "target"
sdkVersionAttribute string = "SDK_VERSION"
sdkVersion string = "1.12.0"
sdkVersion string = "0.1.14"
sdkTypeAttribute string = "SDK_TYPE"
sdkType string = "server"
sdkLanguageAttribute string = "SDK_LANGUAGE"
Expand Down
5 changes: 4 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ func (c *CfClient) streamConnect(ctx context.Context) {
c.config.Logger.Info("Registering SSE consumer")
sseClient := sse.NewClient(fmt.Sprintf("%s/stream?cluster=%s", c.config.url, c.clusterIdentifier))

// Use the SDKs http client
sseClient.Connection = c.config.httpClient

streamErr := func() {
c.config.Logger.Warnf("%s Stream disconnected. Swapping to polling mode", sdk_codes.StreamDisconnected)
c.mux.RLock()
Expand Down Expand Up @@ -400,7 +403,7 @@ func (c *CfClient) authenticate(ctx context.Context) error {
metricsClient, err := metricsclient.NewClientWithResponses(c.config.eventsURL,
metricsclient.WithRequestEditorFn(bearerTokenProvider.Intercept),
metricsclient.WithRequestEditorFn(c.InterceptAddCluster),
metricsclient.WithHTTPClient(http.DefaultClient),
metricsclient.WithHTTPClient(c.config.httpClient),
)
if err != nil {
return err
Expand Down
103 changes: 103 additions & 0 deletions examples/tls/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"crypto/tls"
"crypto/x509"
"fmt"
"log"
"net/http"
"os"
"time"

harness "github.com/harness/ff-golang-server-sdk/client"
"github.com/harness/ff-golang-server-sdk/evaluation"
)

var (
flagName string = getEnvOrDefault("FF_FLAG_NAME", "harnessappdemodarkmode")
sdkKey string = getEnvOrDefault("FF_API_KEY", "change me")
)

func main() {
log.Println("Harness SDK TLS Example")

certPool, err := loadCertificates([]string{"path to PEM", "path to PEM"})
if err != nil {
log.Printf("Failed to parse PEM files: `%s`\n", err)
}

// Create a custom TLS configuration and use the CA pool.
tlsConfig := &tls.Config{
RootCAs: certPool,
}

transport := &http.Transport{
TLSClientConfig: tlsConfig,
}

httpClient := http.Client{Transport: transport}

// Create a feature flag client and wait for it to successfully initialize
startTime := time.Now()

// Note that this code uses ffserver hostname as an example, likely you'll have your own hostname or IP.
// You should ensure the endpoint is returning a cert with valid SANs configured for the host/IP.
client, err := harness.NewCfClient(sdkKey, harness.WithEventsURL("https://ffserver:8003/api/1.0"), harness.WithURL("https://ffserver:8003/api/1.0"), harness.WithWaitForInitialized(true), harness.WithHTTPClient(&httpClient))

elapsedTime := time.Since(startTime)
log.Printf("Took '%v' seconds to get a client initialization result ", elapsedTime.Seconds())

if err != nil {
log.Printf("Client failed to initialize: `%s`\n", err)
}

defer func() {
err := client.Close()
if err != nil {
return
}
}()

// Create a target (different targets can get different results based on rules)
target := evaluation.Target{
Identifier: "HT_1",
Name: "Harness_Target_1",
Attributes: &map[string]interface{}{"email": "[email protected]"},
}

// Loop forever reporting the state of the flag
for {
resultBool, err := client.BoolVariation(flagName, &target, false)
if err != nil {
log.Printf("failed to get evaluation: %v ", err)
}
log.Printf("Flag variation %v\n", resultBool)

time.Sleep(10 * time.Second)
}

}

func getEnvOrDefault(key, defaultStr string) string {
value := os.Getenv(key)
if value == "" {
return defaultStr
}
return value
}

// Load certificates from PEM files
func loadCertificates(filePaths []string) (*x509.CertPool, error) {
pool := x509.NewCertPool()
for _, ca := range filePaths {
caBytes, err := os.ReadFile(ca)
if err != nil {
return nil, fmt.Errorf("failed to read CA certificate from file: %w", err)
}

if !pool.AppendCertsFromPEM(caBytes) {
return nil, fmt.Errorf("could not append CA certificate")
}
}
return pool, nil
}

0 comments on commit d3d1765

Please sign in to comment.