|
| 1 | +--- |
| 2 | +title: Rust SDK and Axum |
| 3 | +description: Getting Started with the OpenFeature Rust SDK and Axum (with Docker example) |
| 4 | +--- |
| 5 | + |
| 6 | +import FlagdContent from '@site/src/components/custom/tutorial/flagd-content.mdx'; |
| 7 | +import FlagdChangeContent from '@site/src/components/custom/tutorial/flagd-change-content.mdx'; |
| 8 | + |
| 9 | +# Getting Started with the OpenFeature Rust SDK and Axum |
| 10 | + |
| 11 | +## Introduction |
| 12 | + |
| 13 | +This tutorial explains how to integrate the OpenFeature Rust SDK into an Axum application using the flagd provider. In this guide you will learn how to: |
| 14 | + |
| 15 | +- Create a new Rust project with Cargo |
| 16 | +- Update dependencies to include Docker-based flagd configuration |
| 17 | +- Set up an Axum server with a custom AppState that holds a FlagdProvider |
| 18 | +- Evaluate a feature flag and return a custom message with diff markers to highlight changes |
| 19 | + |
| 20 | +## Requirements |
| 21 | + |
| 22 | +- Rust stable (1.80.2 or later recommended) |
| 23 | +- Cargo |
| 24 | +- Docker (to run the flagd service) |
| 25 | +- A valid flagd configuration file (e.g. `flags.flagd.json`) |
| 26 | + |
| 27 | +## Walk-through |
| 28 | + |
| 29 | +### Step 1: Create a new Rust project |
| 30 | + |
| 31 | +```bash |
| 32 | +cargo new axumstart |
| 33 | +``` |
| 34 | + |
| 35 | +Then navigate to the project directory: |
| 36 | + |
| 37 | +```bash |
| 38 | +cd axumstart |
| 39 | +``` |
| 40 | + |
| 41 | +### Step 2: Update Cargo.toml dependencies |
| 42 | + |
| 43 | +In your Cargo.toml add the following dependencies: |
| 44 | + |
| 45 | +```toml |
| 46 | +[dependencies] |
| 47 | +axum = "0.6" |
| 48 | +tokio = { version = "1", features = ["full"] } |
| 49 | +serde_json = "1.0" |
| 50 | +tracing = "0.1" |
| 51 | +open-feature = "^0" |
| 52 | +open-feature-flagd = "^0" |
| 53 | +``` |
| 54 | + |
| 55 | +Adjust version numbers according to the latest releases. |
| 56 | + |
| 57 | +### Step 3: Set Up the Application |
| 58 | + |
| 59 | +Create or modify your `src/main.rs` with the following code. Notice the diff markers to indicate new additions: |
| 60 | +```rust |
| 61 | +// diff-remove-block-start |
| 62 | +fn main() { |
| 63 | + println!("Hello, world!"); |
| 64 | +} |
| 65 | +// diff-remove-block-end |
| 66 | + |
| 67 | +// diff-add-block-start |
| 68 | +use axum::{ |
| 69 | + extract::State, |
| 70 | + http::StatusCode, |
| 71 | + response::IntoResponse, |
| 72 | + routing::get, |
| 73 | + Json, Router, |
| 74 | +}; |
| 75 | +use open_feature_flagd::{FlagdOptions, FlagdProvider}; |
| 76 | +use open_feature::{EvaluationContext, provider::FeatureProvider}; |
| 77 | +use serde_json::json; |
| 78 | +use std::sync::Arc; |
| 79 | + |
| 80 | +#[derive(Clone)] |
| 81 | +pub struct AppState { |
| 82 | + pub flag_provider: Arc<FlagdProvider>, |
| 83 | +} |
| 84 | + |
| 85 | +async fn hello_handler(State(state): State<AppState>) -> impl IntoResponse { |
| 86 | + // Create an evaluation context (custom fields can be added if required) |
| 87 | + let context = EvaluationContext::default(); |
| 88 | + |
| 89 | + // Evaluate the "welcome-message" feature flag. |
| 90 | + let flag_eval = state.flag_provider.resolve_bool_value("welcome-message", &context).await; |
| 91 | + let message = match flag_eval { |
| 92 | + Ok(result) if result.value => "Hello, welcome to this OpenFeature-enabled website!", |
| 93 | + _ => "Hello!", |
| 94 | + }; |
| 95 | + |
| 96 | + (StatusCode::OK, Json(json!({ "message": message }))) |
| 97 | +} |
| 98 | + |
| 99 | +#[tokio::main] |
| 100 | +async fn main() { |
| 101 | + // Initialize the flagd provider with default options. |
| 102 | + let flag_provider = Arc::new( |
| 103 | + FlagdProvider::new(FlagdOptions { |
| 104 | + cache_settings: None, |
| 105 | + ..Default::default() |
| 106 | + }) |
| 107 | + .await |
| 108 | + .expect("Failed to connect to flagd. Please confirm flagd is running."), |
| 109 | + ); |
| 110 | + |
| 111 | + // Create a custom application state with the flag provider. |
| 112 | + let app_state = AppState { flag_provider }; |
| 113 | + |
| 114 | + // Build an Axum application with the "/hello" endpoint. |
| 115 | + let app = Router::new() |
| 116 | + .route("/hello", get(hello_handler)) |
| 117 | + .with_state(app_state); |
| 118 | + |
| 119 | + let addr = "127.0.0.1:8080".parse().unwrap(); |
| 120 | + println!("Listening on http://{}/hello", addr); |
| 121 | + |
| 122 | + axum::Server::bind(&addr) |
| 123 | + .serve(app.into_make_service()) |
| 124 | + .await |
| 125 | + .unwrap(); |
| 126 | +} |
| 127 | +// diff-add-block-end |
| 128 | +``` |
| 129 | + |
| 130 | +### Step 4: Configure a provider (flagd) |
| 131 | + |
| 132 | +<FlagdContent /> |
| 133 | + |
| 134 | +Flagd can be run as a [standalone binary](https://flagd.dev/reference/flagd-cli/flagd/) or [Kubernetes Operator](https://openfeature.dev/docs/tutorials/ofo/) |
| 135 | +as well. If you don't have docker installed, get and install the [Flagd binary](https://github.com/open-feature/flagd/releases). |
| 136 | +With the flagd configuration in place, start flagd service with the following command. |
| 137 | + |
| 138 | +```sh |
| 139 | +flagd start -f file:flags.flagd.json |
| 140 | +``` |
| 141 | + |
| 142 | +### Step 6: Rerun the application |
| 143 | + |
| 144 | +Now that everything is in place, let's start the app. |
| 145 | + |
| 146 | +```sh |
| 147 | +cargo run |
| 148 | +``` |
| 149 | + |
| 150 | +Open your browser and navigate to [http://127.0.0.1:8080/hello](http://127.0.0.1:8080/hello) should show the same value as before. |
| 151 | +This difference is now the feature flag value can be changed at runtime! |
| 152 | + |
| 153 | +<FlagdChangeContent /> |
| 154 | + |
| 155 | +Save the changes to `flags.flagd.json` and refresh the browser tab. |
| 156 | +You should now be greeted with `Hello, welcome to this OpenFeature-enabled website!`. |
| 157 | + |
| 158 | +## Conclusion |
| 159 | + |
| 160 | +This tutorial demonstrated how to integrate the OpenFeature Rust SDK with an Axum web server using a custom AppState containing a FlagdProvider. By leveraging flagd service, you can dynamically update feature flags at runtime without needing to redeploy your application. |
| 161 | + |
| 162 | +For advanced configuration and more details, refer to the [OpenFeature Rust SDK documentation](https://docs.rs/open-feature-flagd/latest/open_feature_flagd/). |
| 163 | + |
0 commit comments