Implement basic middleware handling
This commit is contained in:
34
apps/api/src/middlewares.rs
Normal file
34
apps/api/src/middlewares.rs
Normal 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(),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user