1- package client
1+ package ffclient
22
33import (
4+ "github.com/pho3b/tiny-logger/logs"
5+ "github.com/pho3b/tiny-logger/logs/log_level"
46 "strings"
57
68 "github.com/Unleash/unleash-client-go/v4"
79 "github.com/Unleash/unleash-client-go/v4/context"
8- "github.com/pho3b/gitlab-ff-wrapper/client/config"
910 pubconst "github.com/pho3b/gitlab-ff-wrapper/constants"
1011 "github.com/pho3b/gitlab-ff-wrapper/enums"
12+ "github.com/pho3b/gitlab-ff-wrapper/ffclient/ffconfig"
1113 "github.com/pho3b/gitlab-ff-wrapper/internal/constants"
1214 "github.com/pho3b/gitlab-ff-wrapper/internal/service"
13- "github.com/pho3b/tiny-logger/logs"
14- "github.com/pho3b/tiny-logger/logs/log_level"
1515 "github.com/pho3b/tiny-logger/shared"
1616)
1717
@@ -20,7 +20,7 @@ var clientInstance *FeatureFlagsClient = nil
2020type FeatureFlagsClient struct {
2121 // UnleashClientInterface is an interface that defines the methods used to interact with the Unleash feature flag service.
2222 unleashClient UnleashClientInterface
23- // envType represents the environment type (e.g., Production, Staging, Development) that the client is configured for.
23+ // envType represents the environment type (e.g., Production, Staging, Development) that the ffclient is configured for.
2424 envType enums.EnvType
2525 // envTypeVariableName is the name of the environment variable that is used to determine the environment type.
2626 envTypeVariableName string
@@ -48,31 +48,27 @@ func (c *FeatureFlagsClient) IsFeatureEnabledForUser(featureName string, userId
4848 )
4949}
5050
51- // GetEnvironmentType returns the EnvironmentType that is currently set in the FeatureFlags client
51+ // GetEnvironmentType returns the EnvironmentType that is currently set in the FeatureFlags ffclient
5252func (c * FeatureFlagsClient ) GetEnvironmentType () enums.EnvType {
5353 return c .envType
5454}
5555
5656// Init initialized the FeatureFlagsClient instance with default configurations and binds it to the project
5757// referred to the given 'projectId' and 'projectUrl'
5858//
59- // This function initializes the client with the default logger (Warn level),
59+ // This function initializes the ffclient with the default logger (Warn level),
6060// and uses the default environment type variable name from constants.EnvTypeVariableName.
6161func Init (projectUrl string , projectId string ) {
62- InitWithConfig (config .ClientConfig {ProjectId : projectId , ProjectUrl : projectUrl })
62+ InitWithConfig (ffconfig .ClientConfig {ProjectId : projectId , ProjectUrl : projectUrl })
6363}
6464
65- // InitWithConfig initializes the FeatureFlagsClient instance using the provided config .ClientConfig.
65+ // InitWithConfig initializes the FeatureFlagsClient instance using the provided ffconfig .ClientConfig.
6666//
67- // This function initializes the client with the provided logger and environment type variable name.
67+ // This function initializes the ffclient with the provided logger and environment type variable name.
6868// If no logger is provided, a default logger with Warn level is created.
6969// If no environment type variable name is provided, the default value from constants.EnvTypeVariableName is used.
70- // The environment type is determined from the config or the environment variable.
71- func InitWithConfig (config config.ClientConfig ) {
72- if clientInstance != nil {
73- return
74- }
75-
70+ // The environment type is determined from the ffconfig or the environment variable.
71+ func InitWithConfig (config ffconfig.ClientConfig ) {
7672 var logger shared.LoggerInterface
7773 var envType enums.EnvType
7874 var envTypeVariableName , projectUrl , projectId string
@@ -84,27 +80,38 @@ func InitWithConfig(config config.ClientConfig) {
8480 AddDateTime (true )
8581 }
8682
87- if envTypeVariableName = config .EnvironmentTypeVariableName ; config .EnvironmentTypeVariableName == "" {
88- envTypeVariableName = pubconst .EnvTypeVariableName
83+ if projectUrl = config .ProjectUrl ; projectUrl == "" {
84+ logger .Error ("ProjectUrl not specified, it cannot be empty" )
85+ return
8986 }
9087
91- envTypeService := service . NewEnvTypeService ( logger )
92- if envType = config . EnvironmentType ; ! envTypeService . IsEnvTypeValid ( envType ) {
93- envType = envTypeService . GetEnvTypeFromEnvironment ( envTypeVariableName )
88+ if projectId = config . ProjectId ; projectId == "" {
89+ logger . Error ( "ProjectId not specified, it cannot be empty" )
90+ return
9491 }
9592
96- if projectUrl = config . ProjectUrl ; projectUrl == "" {
97- logger .Error ( "ProjectUrl not specified and cannot be empty " )
93+ if clientInstance != nil {
94+ logger .Warn ( "FeatureFlagsClient already initialized " )
9895 return
9996 }
10097
101- if projectId = config .ProjectId ; projectId == "" {
102- logger .Error ("ProjectId not specified and cannot be empty" )
103- return
98+ if envTypeVariableName = config .EnvironmentTypeVariableName ; config .EnvironmentTypeVariableName == "" {
99+ envTypeVariableName = pubconst .EnvTypeVariableName
100+ }
101+
102+ envTypeService := service .NewEnvTypeService (logger )
103+ if len (config .ValidEnvironmentTypes ) > 0 {
104+ for _ , validEnvType := range config .ValidEnvironmentTypes {
105+ envTypeService .AddValidEnvType (validEnvType )
106+ }
107+ }
108+
109+ if envType = config .EnvironmentType ; ! envTypeService .IsEnvTypeValid (envType ) {
110+ envType = envTypeService .GetEnvTypeFromEnvironment (envTypeVariableName )
104111 }
105112
106113 clientInstance = & FeatureFlagsClient {
107- initUnleashClient (logger , envType , projectUrl , projectId ),
114+ initUnleashClient (logger , envType , projectUrl , projectId , config . AsyncInitialization ),
108115 envType ,
109116 envTypeVariableName ,
110117 logger ,
@@ -116,16 +123,17 @@ func Get() *FeatureFlagsClient {
116123 return clientInstance
117124}
118125
119- // initUnleashClient initializes and returns a new Unleash client that will be configured to use the
126+ // initUnleashClient initializes and returns a new Unleash ffclient that will be configured to use the
120127// provided logger and environment type.
121128//
122- // It also sets the necessary configuration options for the Unleash client .
123- // If the client fails to initialize, an error message is logged and nil is returned.
129+ // It also sets the necessary configuration options for the Unleash ffclient .
130+ // If the ffclient fails to initialize, an error message is logged and nil is returned.
124131func initUnleashClient (
125132 logger shared.LoggerInterface ,
126133 envType enums.EnvType ,
127134 projectUrl string ,
128135 projectId string ,
136+ asyncInitialization bool ,
129137) UnleashClientInterface {
130138 unleashClient , err := unleash .NewClient (
131139 unleash .WithUrl (projectUrl ),
@@ -137,11 +145,13 @@ func initUnleashClient(
137145 )
138146
139147 if err != nil {
140- logger .Error ("Feature Flags client initialization error " , err .Error ())
148+ logger .Error ("Feature Flags ffclient initialization error " , err .Error ())
141149 return nil
142150 }
143151
144- unleashClient .WaitForReady ()
152+ if ! asyncInitialization {
153+ unleashClient .WaitForReady ()
154+ }
145155
146156 return unleashClient
147157}
0 commit comments