Skip to content

Commit

Permalink
Add redis UI example
Browse files Browse the repository at this point in the history
One of the big things for this project is maintaining persistence
compatibility with flipper so that the same tools work unmodified.
Here's an example of using ruby `flipper-ui` to manage feature flags via
a Rack webapp.
  • Loading branch information
rgm committed Feb 27, 2023
1 parent f316988 commit 5afe383
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/ui-redis/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

source 'https://rubygems.org'

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gem 'flipper'
gem 'flipper-redis'
gem 'flipper-ui'
gem 'rack'
gem 'webrick'
45 changes: 45 additions & 0 deletions examples/ui-redis/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
GEM
remote: https://rubygems.org/
specs:
concurrent-ruby (1.2.2)
connection_pool (2.3.0)
crass (1.0.6)
erubi (1.12.0)
flipper (0.26.0)
concurrent-ruby (< 2)
flipper-redis (0.26.0)
flipper (~> 0.26.0)
redis (>= 3.0, < 6)
flipper-ui (0.26.0)
erubi (>= 1.0.0, < 2.0.0)
flipper (~> 0.26.0)
rack (>= 1.4, < 3)
rack-protection (>= 1.5.3, <= 4.0.0)
sanitize (< 7)
nokogiri (1.14.2-arm64-darwin)
racc (~> 1.4)
racc (1.6.2)
rack (2.2.6.2)
rack-protection (3.0.5)
rack
redis (5.0.6)
redis-client (>= 0.9.0)
redis-client (0.12.2)
connection_pool
sanitize (6.0.1)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
webrick (1.8.1)

PLATFORMS
arm64-darwin-21

DEPENDENCIES
flipper
flipper-redis
flipper-ui
rack
webrick

BUNDLED WITH
2.3.15
10 changes: 10 additions & 0 deletions examples/ui-redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Web admin example

Show how to use jnunemaker/flipper infrastructure to manage Redis flippers.

## Usage

1. Make sure you have Ruby + bundler installed.
2. Set at least REDIS_URL in your shell environment, see `./start`
3. Issue `./start` at the shell to stand up a Rack-based web UI.
4. Go to `http://127.0.0.1:9292` to see the web UI.
39 changes: 39 additions & 0 deletions examples/ui-redis/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

# https://www.flippercloud.io/docs/ui

require 'flipper-ui'
require 'flipper/adapters/redis'

options = {}

options[:url] = ENV['REDIS_URL']
options[:password] = ENV['REDIS_PASSWORD'] if ENV['REDIS_PASSWORD']
# use below when eg. heroku gives self-signed rediss://
# options[:ssl_params] = { verify_mode: OpenSSL::SSL::VERIFY_NONE }

client = Redis.new options
adapter = Flipper::Adapters::Redis.new client
fstore = Flipper.new(adapter)

Flipper::UI.configure do |config|
config.descriptions_source = lambda { |_keys|
{ 'my:feature' => 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.' }
}

config.show_feature_description_in_list = true
config.banner_text = ENV['FLIPPER_BANNER']
# #{primary secondary success danger warning info light dark}
config.banner_class = ENV['FLIPPER_COLOR']
config.fun = true
end

run Flipper::UI.app(fstore) { |builder|
secret = ENV.fetch('SESSION_SECRET') { SecureRandom.hex(20) }
builder.use(Rack::Session::Cookie, secret:)
builder.use Rack::Auth::Basic do |_username, password|
password == 'secret'
end
}

# vt:ft=ruby
19 changes: 19 additions & 0 deletions examples/ui-redis/start
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail
IFS=$'\n\t'
if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace
fi

export FLIPPER_BANNER="hyak2 ui-redis example"
export FLIPPER_COLOR="primary"
export REDIS_URL="redis://localhost:6379/1"
export REDIS_PASSWORD="someredispassword"
export SESSION_SECRET=$(head -c20 /dev/urandom | base64)

(sleep 1; open "http://localhost:9292") & # open web UI 1s after launch

bundle exec rackup config.ru -p 9292

0 comments on commit 5afe383

Please sign in to comment.