-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87c656c
commit db650eb
Showing
4 changed files
with
62 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod config; | ||
pub mod logger; | ||
pub mod middleware; | ||
pub mod routes; | ||
pub mod server; | ||
pub mod state; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ use log::info; | |
|
||
mod config; | ||
mod logger; | ||
mod middleware; | ||
mod routes; | ||
mod server; | ||
mod state; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use axum::{ | ||
body::Body, | ||
extract::Request, | ||
http::StatusCode, | ||
middleware::Next, | ||
response::{IntoResponse, Response}, | ||
}; | ||
use core::str; | ||
use http_body_util::BodyExt; | ||
use log::{debug, log_enabled, Level}; | ||
|
||
pub async fn body_logger_middleware( | ||
request: Request, | ||
next: Next, | ||
) -> Result<impl IntoResponse, Response> { | ||
let request = buffer_request_body(request).await?; | ||
let response = next.run(request).await; | ||
let response = buffer_response_body(response).await?; | ||
|
||
Ok(response) | ||
} | ||
|
||
async fn buffer_request_body(request: Request) -> Result<Request, Response> { | ||
let (parts, body) = request.into_parts(); | ||
|
||
let bytes = body | ||
.collect() | ||
.await | ||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())? | ||
.to_bytes(); | ||
|
||
if log_enabled!(target: "live_compositor::routes", Level::Debug) { | ||
debug!(target: "live_compositor::routes", "Request body: {:?}", str::from_utf8(&bytes).unwrap()); | ||
} | ||
|
||
Ok(Request::from_parts(parts, Body::from(bytes))) | ||
} | ||
|
||
async fn buffer_response_body(response: Response) -> Result<Response, Response> { | ||
let (parts, body) = response.into_parts(); | ||
|
||
let bytes = body | ||
.collect() | ||
.await | ||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())? | ||
.to_bytes(); | ||
|
||
if log_enabled!(target: "live_compositor::routes", Level::Debug) { | ||
debug!(target: "live_compositor::routes", "Response body: {:?}", str::from_utf8(&bytes).unwrap()); | ||
} | ||
|
||
Ok(Response::from_parts(parts, Body::from(bytes))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters