diff --git a/example/README.md b/example/README.md deleted file mode 100644 index 1542a63..0000000 --- a/example/README.md +++ /dev/null @@ -1,23 +0,0 @@ -# Flagsmith Basic Go SDK Example - -This directory contains a basic go http application which utilises Flagsmith. To run the example application, you'll -need to go through the following steps: - -1. Create an account, organisation and project on [Flagsmith](https://flagsmith.com) -2. Create a feature in the project called "secret_button" -3. Give the feature a value using the json editor as follows: - -```json -{"colour": "#ababab"} -``` - - -4. Set the environment variable `FLAGSMITH_ENVIRONMENT_KEY` with the environment key of one of the environments -in flagsmith (This can be found on the 'settings' page accessed from the menu on the left under the chosen environment.) -5. Run the app using `go run server.go` -6. Browse to http://localhost:5000 - - -Now you can play around with the 'secret_button' feature in flagsmith, turn it on to show it and edit the colour in the -json value to edit the colour of the button. You can also identify as a given user and then update the settings for the -secret button feature for that user in the flagsmith interface to see the affect that has too. diff --git a/example/home.html b/example/home.html deleted file mode 100644 index 131a2af..0000000 --- a/example/home.html +++ /dev/null @@ -1,29 +0,0 @@ - - - - -Flagsmith Example - -

Hello, {{ if .Identifier }} - {{ .Identifier }} - {{else}} - World - {{end}} -

- {{ if .ShowButton }} - - {{end}} -

- -
-

Identify as a user

-
- -

... with an optional user trait

-
-

- - -
- - diff --git a/example/server.go b/example/server.go deleted file mode 100644 index bb81da4..0000000 --- a/example/server.go +++ /dev/null @@ -1,96 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - "html/template" - "log" - "net/http" - "os" - - flagsmith "github.com/Flagsmith/flagsmith-go-client/v3" -) - -func main() { - http.HandleFunc("/", RootHandler) - - fmt.Printf("Starting server at port 5000\n") - if err := http.ListenAndServe(":5000", nil); err != nil { - log.Fatal(err) - } -} - -type TemplateData struct { - Identifier string - ShowButton bool - ButtonColour string -} - -func DefaultFlagHandler(featureName string) (flagsmith.Flag, error) { - return flagsmith.Flag{ - FeatureName: featureName, - IsDefault: true, - Value: `{"colour": "#FFFF00"}`, - Enabled: true, - }, nil -} - -func RootHandler(w http.ResponseWriter, r *http.Request) { - ctx, cancel := context.WithCancel(r.Context()) - defer cancel() - - // Intialise the flagsmith client - client := flagsmith.NewClient(os.Getenv("FLAGSMITH_ENVIRONMENT_KEY"), - flagsmith.WithDefaultHandler(DefaultFlagHandler), - ) - q := r.URL.Query() - - if q.Get("identifier") != "" { - identifier := q.Get("identifier") - var traits []*flagsmith.Trait - traits = nil - - if q.Get("trait-key") != "" { - trait := flagsmith.Trait{TraitKey: q.Get("trait-key"), TraitValue: q.Get("trait-value")} - traits = []*flagsmith.Trait{&trait} - } - - flags, _ := client.GetIdentityFlags(ctx, identifier, traits) - - showButton, _ := flags.IsFeatureEnabled("secret_button") - buttonData, _ := flags.GetFeatureValue("secret_button") - - // convert button data to map - buttonData = buttonData.(string) - var buttonDataMap map[string]string - _ = json.Unmarshal([]byte(buttonData.(string)), &buttonDataMap) - - templateData := TemplateData{ - Identifier: identifier, - ShowButton: showButton, - ButtonColour: buttonDataMap["colour"], - } - t, _ := template.ParseFiles("home.html") - _ = t.Execute(w, templateData) - return - } - flags, _ := client.GetEnvironmentFlags(ctx) - - showButton, _ := flags.IsFeatureEnabled("secret_button") - - buttonData, _ := flags.GetFeatureValue("secret_button") - - // convert button data to map - buttonData = buttonData.(string) - var buttonDataMap map[string]string - _ = json.Unmarshal([]byte(buttonData.(string)), &buttonDataMap) - - templateData := TemplateData{ - ShowButton: showButton, - ButtonColour: buttonDataMap["colour"], - } - - t, _ := template.ParseFiles("home.html") - _ = t.Execute(w, templateData) -}