diff --git a/src/main.rs b/src/main.rs index 974793b..06b530e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ +use axum::{Extension, Json, Router}; use axum::body::Body; use axum::extract::Path; -use axum::http::header::AUTHORIZATION; use axum::http::{Request, StatusCode}; +use axum::http::header::AUTHORIZATION; use axum::middleware::{from_fn, Next}; -use axum::response::Response; +use axum::response::{IntoResponse, Response}; use axum::routing::{delete, post}; -use axum::{Extension, Json, Router}; use bwhc_dto::MtbFile; use clap::Parser; use lazy_static::lazy_static; @@ -13,6 +13,7 @@ use serde::{Deserialize, Serialize}; #[cfg(debug_assertions)] use tower_http::trace::TraceLayer; +use crate::AppResponse::{Accepted, InternalServerError, Unauthorized}; use crate::cli::Cli; use crate::sender::MtbFileSender; @@ -26,6 +27,26 @@ struct RecordKey { patient_id: String, } +enum AppResponse<'a> { + Accepted(&'a str), + Unauthorized, + InternalServerError, +} + +impl IntoResponse for AppResponse<'_> { + fn into_response(self) -> Response { + match self { + Accepted(request_id) => Response::builder() + .status(StatusCode::ACCEPTED) + .header("X-Request-Id", request_id), + Unauthorized => Response::builder().status(StatusCode::UNAUTHORIZED), + InternalServerError => Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR), + } + .body(Body::empty()) + .expect("response built") + } +} + lazy_static! { static ref CONFIG: Cli = Cli::parse(); } @@ -67,10 +88,7 @@ async fn check_basic_auth(request: Request
, next: Next) -> Response { return next.run(request).await; } } - Response::builder() - .status(StatusCode::UNAUTHORIZED) - .body(Body::empty()) - .expect("response built") + Unauthorized.into_response() } async fn handle_delete( @@ -80,8 +98,8 @@ async fn handle_delete( let delete_mtb_file = MtbFile::new_with_consent_rejected(&patient_id); match sender.send(delete_mtb_file).await { - Ok(request_id) => success_response(&request_id), - _ => error_response(), + Ok(request_id) => Accepted(&request_id).into_response(), + _ => InternalServerError.into_response(), } } @@ -90,43 +108,29 @@ async fn handle_post( Json(mtb_file): Json