use ri::{
RiAppBuilder,
RiGateway,
RiRouter,
RiRoute,
RiRouteTarget,
RiRateLimiter,
RiCircuitBreaker,
};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("=== Ri Gateway Module Example ===\n");
let app = RiAppBuilder::new()
.with_gateway_module(|gateway| {
gateway
.with_router(|router| {
router
.add_route(RiRoute::new(
"/api/users",
vec!["GET", "POST"],
RiRouteTarget::new("http://localhost:8081"),
))
.add_route(RiRoute::new(
"/api/products",
vec!["GET"],
RiRouteTarget::new("http://localhost:8082"),
))
.add_route(RiRoute::new(
"/api/admin/*",
vec!["GET", "POST", "PUT", "DELETE"],
RiRouteTarget::new("http://localhost:8080"),
))
.add_route(RiRoute::new(
"/health",
vec!["GET"],
RiRouteTarget::new("http://localhost:8080/health"),
))
})
.with_rate_limiter(|limiter| {
limiter
.with_requests_per_second(100)
.with_burst_size(200)
})
.with_circuit_breaker(|breaker| {
breaker
.with_failure_threshold(5)
.with_recovery_timeout(Duration::from_secs(30))
})
})
.build()?;
let gateway = app.get_module::<RiGateway>();
let router = gateway.get_router();
let rate_limiter = gateway.get_rate_liter();
let circuit_breaker = gateway.get_circuit_breaker();
println!("1. Route Management");
println!(" -----------------");
let routes = router.get_routes();
println!(" Total routes configured: {}\n", routes.len());
for route in &routes {
println!(" Route: {} -> {}", route.path(), route.target().url());
println!(" Methods: {:?}\n", route.methods());
}
println!("2. Route Lookup");
println!(" -------------");
let user_route = router.find_route("/api/users", "GET");
match user_route {
Some(route) => {
println!(" ✓ Found route for /api/users (GET)");
println!(" Target: {}\n", route.target().url());
}
None => {
println!(" ✗ No route found for /api/users (GET)\n");
}
}
let admin_route = router.find_route("/api/admin/users", "GET");
match admin_route {
Some(route) => {
println!(" ✓ Found route for /api/admin/users (GET)");
println!(" Target: {}\n", route.target().url());
}
None => {
println!(" ✗ No route found for /api/admin/users (GET)\n");
}
}
println!("3. Rate Limiting");
println!(" --------------");
let config = rate_limiter.get_config();
println!(" Rate limiter configuration:");
println!(" - Requests per second: {}", config.requests_per_second());
println!(" - Burst size: {}\n", config.burst_size());
println!(" Simulating request rate check...");
for i in 1..=5 {
let allowed = rate_limiter.try_acquire();
println!(" Request {}: {}\n", i, if allowed { "✓ Allowed" } else { "✗ Rate limited" });
}
println!("4. Circuit Breaker");
println!(" ----------------");
let breaker_config = circuit_breaker.get_config();
println!(" Circuit breaker configuration:");
println!(" - Failure threshold: {}", breaker_config.failure_threshold());
println!(" - Recovery timeout: {:?}\n", breaker_config.recovery_timeout());
let status = circuit_breaker.get_status();
println!(" Circuit breaker status: {:?}\n", status);
println!(" Simulating circuit breaker operations...");
for _ in 0..3 {
circuit_breaker.record_success();
}
println!(" Recorded 3 successes");
for _ in 0..2 {
circuit_breaker.record_failure();
}
println!(" Recorded 2 failures");
let final_status = circuit_breaker.get_status();
println!(" Final circuit breaker status: {:?}\n", final_status);
println!("5. Gateway Statistics");
println!(" -------------------");
let stats = gateway.get_stats();
println!(" Gateway statistics:");
println!(" - Total requests: {}", stats.total_requests());
println!(" - Successful requests: {}", stats.successful_requests());
println!(" - Failed requests: {}", stats.failed_requests());
println!(" - Average response time: {:?}\n", stats.avg_response_time());
println!("=== Gateway Example Completed ===");
Ok(())
}