#[cfg(feature = "rocket")]
use std::sync::Arc;
#[cfg(feature = "rocket")]
use rocket::{Build, Rocket, State, get, routes};
#[cfg(feature = "rocket")]
use zenmanage::{ConfigBuilder, Context, Zenmanage, integrations::rocket::manage_sdk};
#[cfg(feature = "rocket")]
#[get("/?<user>")]
async fn index(sdk: &State<Arc<Zenmanage>>, user: Option<String>) -> String {
let context = Context::single("user", user.unwrap_or_default(), None::<String>);
let flags = sdk.flags().with_context(context);
match flags.single("example-flag", Some(false)).await {
Ok(flag) => format!("feature enabled: {}", flag.is_enabled()),
Err(error) => format!("error: {error}"),
}
}
#[cfg(feature = "rocket")]
fn rocket() -> Result<Rocket<Build>, Box<dyn std::error::Error>> {
let token = std::env::var("ZENMANAGE_ENVIRONMENT_TOKEN")
.unwrap_or_else(|_| "srv_your_server_key_here".to_owned());
let sdk = Arc::new(Zenmanage::new(
ConfigBuilder::create()
.with_environment_token(token)
.build()?,
)?);
let base = rocket::build().mount("/", routes![index]);
Ok(manage_sdk(base, sdk))
}
#[cfg(feature = "rocket")]
#[rocket::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let app = rocket()?;
println!("Rocket example running on http://127.0.0.1:8000");
app.launch().await?;
Ok(())
}
#[cfg(not(feature = "rocket"))]
fn main() {
eprintln!("Enable the `rocket` feature to run this example.");
}