A simple implementation of the Feature Toggles pattern for Golang
- Configure from environment variables
- Configure from JSON file
- Randomly enabled feature
- Time base configuration
- Percentage based configuration
- Week day based configuration
At first create a struct containing the features you want to be able to toggle:
type MyFeatures struct{
SimpleFeature togglr.Feature
EnabledOnOrBefore togglr.Feature
EnabledOnOrAfter togglr.Feature
FiftyFifty togglr.Feature
OnlyOnWeekend togglr.Feature
FeatureFromEnv togglr.Feature `env:"FEATURE"`
}
Then create a json file configuring thoses feature by thier names:
{
"SimpleFeature": true,
"EnabledOnOrBefore": { "enabledOnOrBefore": "2006-01-02 15:04:05" },
"EnabledOnOrAfter": { "enabledOnOrAfter": "2016-01-02" },
"FiftyFifty": { "percent": 50 },
"OnlyOnWeekend" : { "days": "Saturday | Sunday" }
}
Almost done. Now just fil the feature sruct and use it
package main
import "github.com/mouk/togglr"
func main() {
features := MyFeatures{}
togglr.Init("feature_configuration.json")
togglr.Read(&features)
if(features.EnabledOnOrBefore.IsEnabled()){
// Do somthing
}
}
A static overview of all features can be exported as a dictionary. This should be used only for debugging and reporting puposes, but not to change the control flow.
package main
import "github.com/mouk/togglr"
type MyFeatures struct{
Feature1 togglr.Feature
//....
}
func main() {
togglr.Init("features.json")
features := MyFeatures{}
togglr.Read(&features)
snapshot = togglr.GetFeatureSpanshots(&features)
for feature, enabled := snapshot m {
fmt.Println("Feature:", feature, "Enabled:", enabled)
}
}