目录
目录README.md

FeatureProbe Server Side SDK for Rust

Top Language codecov Github Star Apache-2.0 license

Feature Probe is an open source feature management service. This SDK is used to control features in rust programs. This SDK is designed primarily for use in multi-user systems such as web servers and applications.

Basic Terms

Reading the short Basic Terms will help to understand the code blow more easily. 中文

Try Out Demo Code

We provide a runnable demo code for you to understand how FeatureProbe SDK is used.

  1. Start FeatureProbe Service with docker composer. How to
  2. Download this repo and run the demo program:
    git clone https://github.com/FeatureProbe/server-sdk-rust.git
    cd server-sdk-rust
    cargo run --example demo
  3. Find the Demo code in examples, do some change and run the program again.
    cargo run --example demo

Step-by-Step Guide

In this guide we explain how to use feature toggles in a Rust application using FeatureProbe.

Step 1. Install the Rust SDK

First, install the FeatureProbe SDK as a dependency in your application.

cargo install cargo-edit
cargo add feature-probe-server-sdk-rs --allow-prerelease

Next, import the FeatureProbe SDK in your application code:

use feature_probe_server_sdk::{FPConfig, FPUser, FeatureProbe};

Step 2. Create a FeatureProbe instance

After you install and import the SDK, create a single, shared instance of the FeatureProbe sdk.

fn main() {
    let remote_url = "https://featureprobe.io/server";
    // let remote_url = "http://localhost:4007"; // for local docker

    let config = FPConfig {
        remote_url: remote_url.to_owned(),
        server_sdk_key: args.server_sdk_key.to_owned(),
        refresh_interval: Duration::from_secs(5),
        wait_first_resp: true,
    };

    let fp = match FeatureProbe::new(config) {
        Ok(fp) => fp,
        Err(e) => {
            tracing::error!("{:?}", e);
            return;
        }
    };
}

Step 3. Use the feature toggle

You can use sdk to check which variation a particular user will receive for a given feature flag.

let user_id = /* unique user id in your business logic */
let user = FPUser::new(user_id).with("name", "bob");
let show_feature = fp.bool_value("bool_toggle", &user, false);

if show_feature {
    // application code to show the feature
} else {
    // the code to run if the feature is off
}

Step 4. Unit Testing (Optional)

You could do unit testing for each variation:

let fp = FeatureProbe::new_for_test("toggle_1", Value::Bool(false));
let u = FPUser::new("key");
assert_eq!(fp.bool_value("toggle_1", &u, true), false);

let mut toggles: HashMap<String, Value> = HashMap::new();
toggles.insert("toggle_2".to_owned(), json!(12.5));
toggles.insert("toggle_3".to_owned(), json!("value"));
let fp = FeatureProbe::new_for_tests(toggles);
assert_eq!(fp.number_value("toggle_2", &u, 20.0), 12.5);
assert_eq!(fp.string_value("toggle_3", &u, "val".to_owned()), "value");

Here is an example

Rust Docs

Docs home

Main functions

Testing SDK

We have unified integration tests for all our SDKs. Integration test cases are added as submodules for each SDK repo. So be sure to pull submodules first to get the latest integration tests before running tests.

git pull --recurse-submodules
cargo test

Contributing

We are working on continue evolving FeatureProbe core, making it flexible and easier to use. Development of FeatureProbe happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements.

Please read CONTRIBUTING for details on our code of conduct, and the process for taking part in improving FeatureProbe.

关于
135.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号