|
1 |
| -# fastapi-featureflags |
2 |
| -FastAPI Feature Flags |
| 1 | +# FastAPI Feature Flags |
| 2 | + |
| 3 | +Very simple implementation of feature flags for FastAPI. |
| 4 | +- Minimum configuration required. |
| 5 | +- No unnecessary dependencies |
| 6 | +- Does its job |
| 7 | + |
| 8 | +## Installing |
| 9 | +To install the package from pip, first run: |
| 10 | +```bash |
| 11 | +pip install -U https://github.com/Pytlicek/fastapi-featureflags |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | +A simple example of feature flags: |
| 16 | +``` |
| 17 | +from fastapi_featureflags import FeatureFlags, feature_flag, feature_enabled |
| 18 | +
|
| 19 | +my_ff = FeatureFlags(conf_from_url="https://pastebin.com/raw/4Ai3j2DC") |
| 20 | +print("Enabled Features:", my_ff.get_features()) |
| 21 | +
|
| 22 | +
|
| 23 | +@feature_flag("web_1") |
| 24 | +def web_1_enabled(): |
| 25 | + print("Feature Should be enabled: web_1") |
| 26 | +
|
| 27 | +web_1_enabled() |
| 28 | +
|
| 29 | +if feature_enabled("web_2"): |
| 30 | + print("Feature Should be disabled: web_2") |
| 31 | +``` |
| 32 | + |
| 33 | +You can get FF (feature flags) from a file or URL: |
| 34 | +``` |
| 35 | +FeatureFlags(conf_from_url="https://pastebin.com/raw/4Ai3j2DC") |
| 36 | +FeatureFlags(conf_from_json="tests/features.json") |
| 37 | +``` |
| 38 | +There is also a handler that recognizes if the "@feature_flag" wrapper is used and the flag is not registered in the config. |
| 39 | +This way you can also use FF at runtime. Defaults to False, so it's safer if you forget the feature flag in the code. |
| 40 | + |
| 41 | +Function `get_features` returns a list of all registered FF |
| 42 | +You can enable or disable functions on the fly with `enable_feature` or `enable_feature` |
| 43 | + |
| 44 | +When needed you can reload all feature flags with `reload_feature_flags`, |
| 45 | +this is handy when you want to read and change features from URL fe. |
| 46 | + |
| 47 | +For non-production testing, is there a router available, |
| 48 | +so you can see the paths in swagger-ui docs. |
| 49 | +Use `include_in_schema=False` when defining the router for public deployments |
| 50 | +``` |
| 51 | +from fastapi_featureflags import router as ff_router |
| 52 | +app.include_router(ff_router) |
| 53 | +``` |
| 54 | + |
| 55 | +### TODO |
| 56 | +- Tests |
| 57 | +- Better rewrite of the main class |
| 58 | +- Packaging |
| 59 | +- FF from environments |
0 commit comments