Safe Haskell | None |
---|---|
Language | Haskell2010 |
Use feature flags in WAI applications.
A feature flag is a way to toggle functionality on or off without having to redeploy the application. Feature flags have many possible uses, one is making it safer to release new functionality by providing a way to turn it off immediately if it misbehaves.
An application using this library first needs to define which feature flags
is supports. This is done by creating a record type containing only boolean
fields and adding a Flags
instance to it.
data Features = Features { openWindow :: Bool, feedPigeons :: Bool } deriving (Generic) instance Flags Features
Then we need a place to persist flag data. This library provides a
memoryStore
but it doesn't remember flag states across restarts. For
production applications it's probably best to implement a Store
that reads
and writes flag data to the database or key,value store backing your project.
mkApplication
provides a frontend from which each feature flag can be
fully enabled, fully disabled, or enabled for a specific percentage of
traffic. It is compatible with Wai
-based web frameworks like spock
,
scotty
, and servant
. Setup instructions will be different for each. If
you're having trouble integrating this in your choice of web framework please
feel free to open an issue.
Now you're all set up. You can use fetch
to read your feature flags from
your store and can use their values in conditionals. For a full example check
out this sample application.
Synopsis
- class Flags flags
- fetch :: forall flags. Flags flags => Store flags -> IO flags
- data Store flags = Store {
- readKeys :: IO [(ByteString, ByteString)]
- writeKey :: ByteString -> ByteString -> IO ()
- memoryStore :: IO (Store flags)
- mkApplication :: Flags flags => Store flags -> IO Application
Flags
The feature flags you define are described by a type you create yourself.
It needs to be a record though, with every field a boolean. Then we add a
Flags
instance to it so this library is able to work with the type.
data Features = Features { openWindow :: Bool, feedPigeons :: Bool } deriving (Generic) instance Flags Features
fetch :: forall flags. Flags flags => Store flags -> IO flags Source #
Read feature flag states out of the store. The states of flags enabled for part of the traffic will be determined by die-roll.
The default state for new flags and flags we cannot find values for in the
store is off. This library offers no way to set other defaults to keep it as
simple as possibe. You are encouraged to phrase your flag names in such a
way that off corresponds to what you'd like the default value to be, i.e.
enableExperimentalDoodad
is likely safer than disableExperimentalDoodad
.
Store
A type describing a store in which feature flag data can be saved. You are recommended to define your own stores using a persistence mechanism of your choice.
Store | |
|
memoryStore :: IO (Store flags) Source #
An in-memory store that does not persist feature flag data across application restarts. Suitable for experimentation but not recommended for production use.
Feature flag frontend
mkApplication :: Flags flags => Store flags -> IO Application Source #
Create a WAI application that serves a frontend for modifying feature flag states. How you embed this into your real application depends on the web framework you're using.