-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstatus_demo.rs
More file actions
56 lines (48 loc) · 1.89 KB
/
status_demo.rs
File metadata and controls
56 lines (48 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use rustapi_rs::prelude::*;
use std::time::Duration;
use tokio::time::sleep;
/// A simple demo to showcase the RustAPI Status Page.
///
/// Run with: `cargo run --example status_demo`
/// Then verify:
/// - Status Page: http://127.0.0.1:3000/status
/// - Generate Traffic: http://127.0.0.1:3000/api/fast
/// - Generate Errors: http://127.0.0.1:3000/api/slow
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// 1. Define some handlers to generate metrics
// A fast endpoint
async fn fast_handler() -> &'static str {
"Fast response!"
}
// A slow endpoint with random delay to show latency
async fn slow_handler() -> &'static str {
sleep(Duration::from_millis(500)).await;
"Slow response... sleepy..."
}
// An endpoint that sometimes fails
async fn flaky_handler() -> Result<&'static str, rustapi_rs::Response> {
use std::sync::atomic::{AtomicBool, Ordering};
static FAILURE: AtomicBool = AtomicBool::new(false);
// Toggle failure every call
let fail = FAILURE.fetch_xor(true, Ordering::Relaxed);
if !fail {
Ok("Success!")
} else {
Err(rustapi_rs::StatusCode::INTERNAL_SERVER_ERROR.into_response())
}
}
// 2. Build the app with status page enabled
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() // <--- Enable Status Page
.route("/fast", get(fast_handler))
.route("/slow", get(slow_handler))
.route("/flaky", get(flaky_handler))
.run("127.0.0.1:3000")
.await
}