Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wkazmierczak committed Sep 23, 2024
1 parent 87c656c commit db650eb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 49 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
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;
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use log::info;

mod config;
mod logger;
mod middleware;
mod routes;
mod server;
mod state;
Expand Down
53 changes: 53 additions & 0 deletions src/middleware.rs
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)))
}
56 changes: 7 additions & 49 deletions src/routes.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
use axum::{
async_trait,
body::Body,
extract::{rejection::JsonRejection, ws::WebSocketUpgrade, FromRequest, Request, State},
http::StatusCode,
middleware::{self, Next},
response::{IntoResponse, Response},
middleware,
response::IntoResponse,
routing::{get, post},
Router,
};
use compositor_pipeline::Pipeline;
use core::str;
use http_body_util::BodyExt;
use log::info;
use serde_json::{json, Value};

use crate::state::{self, ApiState};
use crate::state::{ApiState, Response};

use compositor_api::error::ApiError;

use self::{
update_output::handle_keyframe_request, update_output::handle_output_update,
ws::handle_ws_upgrade,
};
use crate::middleware::body_logger_middleware;

mod register_request;
mod unregister_request;
Expand Down Expand Up @@ -59,9 +56,9 @@ pub fn routes(state: ApiState) -> Router {
.route("/:id/register", post(register_request::handle_shader))
.route("/:id/unregister", post(unregister_request::handle_shader));

async fn handle_start(State(state): State<ApiState>) -> Result<state::Response, ApiError> {
async fn handle_start(State(state): State<ApiState>) -> Result<Response, ApiError> {
Pipeline::start(&state.pipeline);
Ok(state::Response::Ok {})
Ok(Response::Ok {})
}

Router::new()
Expand All @@ -80,7 +77,7 @@ pub fn routes(state: ApiState) -> Router {
"instance_id": state.config.instance_id
}))),
)
.layer(middleware::from_fn(log_request_response_body))
.layer(middleware::from_fn(body_logger_middleware))
.with_state(state)
}

Expand Down Expand Up @@ -117,42 +114,3 @@ where
}
}
}

async fn log_request_response_body(
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();

info!("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();

info!("Response body: {:?}", str::from_utf8(&bytes).unwrap());

Ok(Response::from_parts(parts, Body::from(bytes)))
}

0 comments on commit db650eb

Please sign in to comment.