Skip to main content

http_service/
http_service.rs

1//! HTTP Service Example
2//!
3//! This example shows how to integrate do-over policies with an axum web service.
4
5use axum::{routing::get, Router};
6use do_over::{error::DoOverError, policy::Policy, retry::RetryPolicy, timeout::TimeoutPolicy, wrap::Wrap};
7use std::time::Duration;
8use tokio::net::TcpListener;
9
10#[tokio::main]
11async fn main() {
12    let policy = Wrap::new(
13        RetryPolicy::fixed(2, Duration::from_millis(50)),
14        TimeoutPolicy::new(Duration::from_secs(1)),
15    );
16
17    let app = Router::new().route(
18        "/",
19        get(|| async move {
20            let result: Result<&str, DoOverError<()>> =
21                policy.execute(|| async { Ok("hello from do-over") }).await;
22            result.unwrap()
23        }),
24    );
25
26    println!("Starting server on http://0.0.0.0:3000");
27    let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
28    axum::serve(listener, app).await.unwrap();
29}