Implement basic middleware handling
All checks were successful
Test / verify-generated-code (pull_request) Successful in 8m1s
Test / test (pull_request) Successful in 1m9s
Test / lint (pull_request) Successful in 1m5s

This commit is contained in:
GW_MC
2025-12-02 15:47:56 +08:00
parent 547d73fab7
commit 6cd55d06a2
5 changed files with 45 additions and 22 deletions

View File

@@ -0,0 +1,34 @@
use axum::{
BoxError, Router,
error_handling::HandleErrorLayer,
http::{Method, StatusCode, Uri},
};
use std::time::Duration;
use tower::{ServiceBuilder, timeout::TimeoutLayer};
use tracing::warn;
pub const TIMEOUT_DURATION_SECS: u64 = 30;
pub fn apply_root_middleware(router: Router) -> Router {
let timeout_layer = TimeoutLayer::new(Duration::from_secs(TIMEOUT_DURATION_SECS));
let service_builder = ServiceBuilder::new()
.layer(HandleErrorLayer::new(handle_timeout_error))
.layer(timeout_layer);
router.layer(service_builder)
}
pub async fn handle_timeout_error(
method: Method,
uri: Uri,
//
err: BoxError,
) -> (StatusCode, String) {
warn!("`{method} {uri}` failed with {err}");
(
StatusCode::INTERNAL_SERVER_ERROR,
"Internal server error".to_string(),
)
}