Skip to content

adamvialpando/flagsmith-demos

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flagsmith Go Demo: Cosmic Coffee Co.

This document guides you through setting up the "Cosmic Coffee Co." demo application and the required configuration in your Flagsmith project.

1. Prerequisites

  • Go (version 1.18 or later) installed on your machine.
  • A Flagsmith account. If you don't have one, you can sign up for free.

2. Flagsmith Project Setup

First, we need to create the flags and segments in your Flagsmith project that the Go application will use.

Step A: Create a New Project

  • Log in to your Flagsmith account.
  • Create a new project named "Cosmic Coffee Co." or similar.
  • Navigate to the Settings page for your project's Development environment and copy the Server-Side Environment Key. You will need this key to run the app.

Step B: Create the Feature Flags

Navigate to the Features page and create the following three flags:

  • Dark Mode Toggle (dark_mode)

    • ID: dark_mode
    • Type: Standard Feature Flag (On/Off)
    • Enabled by default: You can set this to either on or off to start.
  • Sale Banner & Discount (sale_banner)

    • ID: sale_banner
    • Type: Remote Config (Value-based flag)
    • Enabled by default: Check this on.
    • Value: Set the value to 10%.
  • Beta Tester Banner (beta_user_banner)

    • ID: beta_user_banner
    • Type: Standard Feature Flag (On/Off)
    • Enabled by default: Leave this unchecked (disabled). We will enable it for a specific segment only.

Step C: Create a "Beta Testers" Segment

Now, let's create a segment to target our internal beta testers.

  • Navigate to the Segments page on the left-hand navigation.
  • Click Create a Segment.
  • Name the segment Beta Testers.
  • Add a rule:
    • Trait Key: is_beta_tester
    • Condition: Equal to
    • Value: true
  • Click Create Segment.

Step D: Create a Segment Override

Finally, we'll use the segment to turn on the beta_user_banner only for our beta testers.

  • Go back to the Features page.
  • Find the beta_user_banner flag and click on it.
  • Go to the Overrides tab.
  • Click Add Segment Override.
  • Select the Beta Testers segment.
  • In the override rule, toggle the feature ON.
  • Click Create Override.

Now, the beta_user_banner is disabled for everyone except users who are identified as being part of the "Beta Testers" segment.

3. Running the Go Application

  • Save the Code: Save the Go code from the first file as main.go.
  • Set Environment Key: Open your terminal and set the environment variable for your Flagsmith key.
export FLAGSMITH_ENVIRONMENT_KEY="<your_flagsmith_server_side_key>"

Replace <your_flagsmith_server_side_key> with the key you copied in Step A.

  • Install Dependencies: In the same directory as main.go, run the following commands to initialize a Go module and fetch the Flagsmith client.
go mod init cosmic-coffee-demo
go get github.com/Flagsmith/flagsmith-go-client/v3
  • Run the App:
go run main.go

You should see a message saying Cosmic Coffee Co. server starting on http://localhost:8080.

4. Test the Features!

Now for the fun part!

  • Default View: Open your browser and go to http://localhost:8080. You'll see the standard page.
  • Become a Beta Tester: Now, go to http://localhost:8080?user=beta_tester. You should see the purple "You are a Beta Tester!" banner appear at the top! This is the segment override in action.
  • Control from Flagsmith:
    • Go to your Flagsmith dashboard.
    • Toggle the dark_mode flag on and off. Refresh your browser page to see the site switch between light and dark mode instantly.
    • Change the value of the sale_banner from 10% to 25% OFF. Refresh your browser—the text in the green sale banner will update immediately.

You've now successfully controlled your running Go application from an external dashboard without touching a single line of code!

About

A place to hold my example code for flagsmith

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors