rs_zero/rest/middleware/
timeout.rs1use std::time::Duration;
2
3use axum::{Router, error_handling::HandleErrorLayer, response::IntoResponse};
4use tower::{ServiceBuilder, timeout::TimeoutLayer};
5
6use crate::rest::{RestError, response::ApiResponse};
7
8pub fn apply_timeout(router: Router, duration: Duration) -> Router {
10 router.layer(
11 ServiceBuilder::new()
12 .layer(HandleErrorLayer::new(|error: tower::BoxError| async move {
13 if error.is::<tower::timeout::error::Elapsed>() {
14 RestError::Timeout.into_response()
15 } else {
16 ApiResponse::<()>::fail("INTERNAL", error.to_string()).into_response()
17 }
18 }))
19 .layer(TimeoutLayer::new(duration)),
20 )
21}