use rustapi_rs::prelude::*;
use std::time::Duration;
use tokio::time::sleep;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
async fn fast_handler() -> &'static str {
"Fast response!"
}
async fn slow_handler() -> &'static str {
sleep(Duration::from_millis(500)).await;
"Slow response... sleepy..."
}
async fn flaky_handler() -> Result<&'static str, rustapi_rs::Response> {
use std::sync::atomic::{AtomicBool, Ordering};
static FAILURE: AtomicBool = AtomicBool::new(false);
let fail = FAILURE.fetch_xor(true, Ordering::Relaxed);
if !fail {
Ok("Success!")
} else {
Err(rustapi_rs::StatusCode::INTERNAL_SERVER_ERROR.into_response())
}
}
println!("Starting Status Page Demo...");
println!(" -> Open http://127.0.0.1:3000/status to see the dashboard");
println!(" -> Visit http://127.0.0.1:3000/fast to generate traffic");
println!(" -> Visit http://127.0.0.1:3000/slow to generate latency");
println!(" -> Visit http://127.0.0.1:3000/flaky to generate errors");
RustApi::auto()
.status_page() .route("/fast", get(fast_handler))
.route("/slow", get(slow_handler))
.route("/flaky", get(flaky_handler))
.run("127.0.0.1:3000")
.await
}