Documentation ΒΆ
Overview ΒΆ
Package ffclient aids adding instrumentation to have feature flags in your app without any backend server.
Summary ΒΆ
This package and its subpackages contain bits of code to have an easy feature flag solution with no complex installation to do on your infrastructure and without using 3rd party vendor for this.
The ffclient package provides the entry point - initialization and the basic method to get your flags value.
Before using the module you need to initialized it this way:
func main() { err := ffclient.Init(ffclient.Config{ PollInterval: 3, HTTPRetriever: &ffClient.HTTPRetriever{ URL: "http://example.com/flag-config.yaml", }, }) defer ffclient.Close() ...
This example will load a file from an HTTP endpoint and will refresh the flags every 3 seconds.
Now you can evalute your flags anywhere in your code.
func main() { ... user := ffuser.NewUser("user-unique-key") hasFlag, _ := ffclient.BoolVariation("test-flag", user, false) if hasFlag { //flag "test-flag" is true for the user } else { // flag "test-flag" is false for the user } ...
Index ΒΆ
- func BoolVariation(flagKey string, user ffuser.User, defaultValue bool) (bool, error)
- func Close()
- func Float64Variation(flagKey string, user ffuser.User, defaultValue float64) (float64, error)
- func Init(config Config) error
- func IntVariation(flagKey string, user ffuser.User, defaultValue int) (int, error)
- func JSONArrayVariation(flagKey string, user ffuser.User, defaultValue []interface{}) ([]interface{}, error)
- func JSONVariation(flagKey string, user ffuser.User, defaultValue map[string]interface{}) (map[string]interface{}, error)
- func StringVariation(flagKey string, user ffuser.User, defaultValue string) (string, error)
- type Config
- type FileRetriever
- type GithubRetriever
- type GoFeatureFlag
- func (g *GoFeatureFlag) BoolVariation(flagKey string, user ffuser.User, defaultValue bool) (bool, error)
- func (g *GoFeatureFlag) Close()
- func (g *GoFeatureFlag) Float64Variation(flagKey string, user ffuser.User, defaultValue float64) (float64, error)
- func (g *GoFeatureFlag) IntVariation(flagKey string, user ffuser.User, defaultValue int) (int, error)
- func (g *GoFeatureFlag) JSONArrayVariation(flagKey string, user ffuser.User, defaultValue []interface{}) ([]interface{}, error)
- func (g *GoFeatureFlag) JSONVariation(flagKey string, user ffuser.User, defaultValue map[string]interface{}) (map[string]interface{}, error)
- func (g *GoFeatureFlag) StringVariation(flagKey string, user ffuser.User, defaultValue string) (string, error)
- type HTTPRetriever
- type Retriever
- type S3Retriever
- type WebhookConfig
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func BoolVariation ΒΆ
BoolVariation return the value of the flag in boolean. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value.
func Close ΒΆ
func Close()
Close the component by stopping the background refresh and clean the cache.
func Float64Variation ΒΆ
Float64Variation return the value of the flag in float64. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value.
func Init ΒΆ
Init the feature flag component with the configuration of ffclient.Config
func main() { err := ffclient.Init(ffclient.Config{ PollInterval: 3, Retriever: &ffClient.HTTPRetriever{ URL: "http://example.com/flag-config.yaml", }, }) defer ffclient.Close()
func IntVariation ΒΆ
IntVariation return the value of the flag in int. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value.
func JSONArrayVariation ΒΆ
func JSONArrayVariation(flagKey string, user ffuser.User, defaultValue []interface{}) ([]interface{}, error)
JSONArrayVariation return the value of the flag in []interface{}. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value.
func JSONVariation ΒΆ
func JSONVariation( flagKey string, user ffuser.User, defaultValue map[string]interface{}) (map[string]interface{}, error)
JSONVariation return the value of the flag in map[string]interface{}. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value.
func StringVariation ΒΆ
StringVariation return the value of the flag in string. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value.
Types ΒΆ
type Config ΒΆ
type Config struct { PollInterval int // Poll every X seconds Logger *log.Logger Context context.Context // default is context.Background() Retriever Retriever Webhooks []WebhookConfig // webhooks we should call when a flag create/update/delete FileFormat string }
Config is the configuration of go-feature-flag. PollInterval is the interval in seconds where we gonna read the file to update the cache. You should also have a retriever to specify where to read the flags file.
func (*Config) GetRetriever ΒΆ
func (c *Config) GetRetriever() (retriever.FlagRetriever, error)
GetRetriever returns a retriever.FlagRetriever configure with the retriever available in the config.
type FileRetriever ΒΆ added in v0.5.0
type FileRetriever struct {
Path string
}
FileRetriever is a configuration struct for a local flat file.
type GithubRetriever ΒΆ added in v0.4.0
type GithubRetriever struct { RepositorySlug string Branch string // default is main FilePath string GithubToken string Timeout time.Duration // default is 10 seconds }
GithubRetriever is a configuration struct for a GitHub retriever.
type GoFeatureFlag ΒΆ added in v0.6.0
type GoFeatureFlag struct {
// contains filtered or unexported fields
}
GoFeatureFlag is the main object of the library it contains the cache, the config and the update.
func New ΒΆ added in v0.6.0
func New(config Config) (*GoFeatureFlag, error)
New creates a new go-feature-flag instance that retrieve the config from a YAML file and return everything you need to manage your flags.
func (*GoFeatureFlag) BoolVariation ΒΆ added in v0.6.0
func (g *GoFeatureFlag) BoolVariation(flagKey string, user ffuser.User, defaultValue bool) (bool, error)
BoolVariation return the value of the flag in boolean. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value. Note: Use this function only if you are using multiple go-feature-flag instances.
func (*GoFeatureFlag) Close ΒΆ added in v0.6.0
func (g *GoFeatureFlag) Close()
Close wait until thread are done
func (*GoFeatureFlag) Float64Variation ΒΆ added in v0.6.0
func (g *GoFeatureFlag) Float64Variation(flagKey string, user ffuser.User, defaultValue float64) (float64, error)
Float64Variation return the value of the flag in float64. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value. Note: Use this function only if you are using multiple go-feature-flag instances.
func (*GoFeatureFlag) IntVariation ΒΆ added in v0.6.0
func (g *GoFeatureFlag) IntVariation(flagKey string, user ffuser.User, defaultValue int) (int, error)
IntVariation return the value of the flag in int. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value. Note: Use this function only if you are using multiple go-feature-flag instances.
func (*GoFeatureFlag) JSONArrayVariation ΒΆ added in v0.6.0
func (g *GoFeatureFlag) JSONArrayVariation( flagKey string, user ffuser.User, defaultValue []interface{}) ([]interface{}, error)
JSONArrayVariation return the value of the flag in []interface{}. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value. Note: Use this function only if you are using multiple go-feature-flag instances.
func (*GoFeatureFlag) JSONVariation ΒΆ added in v0.6.0
func (g *GoFeatureFlag) JSONVariation( flagKey string, user ffuser.User, defaultValue map[string]interface{}) (map[string]interface{}, error)
JSONVariation return the value of the flag in map[string]interface{}. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value. Note: Use this function only if you are using multiple go-feature-flag instances.
func (*GoFeatureFlag) StringVariation ΒΆ added in v0.6.0
func (g *GoFeatureFlag) StringVariation(flagKey string, user ffuser.User, defaultValue string) (string, error)
StringVariation return the value of the flag in string. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value. Note: Use this function only if you are using multiple go-feature-flag instances.
type HTTPRetriever ΒΆ
type HTTPRetriever struct { URL string Method string Body string Header http.Header Timeout time.Duration }
HTTPRetriever is a configuration struct for an HTTP endpoint retriever.
type Retriever ΒΆ added in v0.5.0
type Retriever interface {
// contains filtered or unexported methods
}
type S3Retriever ΒΆ
S3Retriever is a configuration struct for a S3 retriever.
type WebhookConfig ΒΆ added in v0.7.0
type WebhookConfig struct { PayloadURL string // PayloadURL of your webhook Secret string // Secret used to sign your request body. Meta map[string]string // Meta information that you want to send to your webhook (not mandatory) }
WebhookConfig is the configuration of your webhook. we will call this URL with a POST request with the following format
{ "meta":{ "hostname": "server01" }, "flags":{ "deleted": { "test-flag": { "rule": "key eq \"random-key\"", "percentage": 100, "true": true, "false": false, "default": false } }, "added": { "test-flag3": { "percentage": 5, "true": "test", "false": "false", "default": "default" } }, "updated": { "test-flag2": { "old_value": { "rule": "key eq \"not-a-key\"", "percentage": 100, "true": true, "false": false, "default": false }, "new_value": { "disable": true, "rule": "key eq \"not-a-key\"", "percentage": 100, "true": true, "false": false, "default": false } } } } }