1
0
mirror of https://github.com/CCC-MF/bwhc-kafka-rest-proxy.git synced 2025-04-19 19:16:51 +00:00

refactor: extract route/request handlers

This commit is contained in:
Paul-Christian Volkmer 2024-12-28 13:34:23 +01:00
parent b0bf5ce5dc
commit e6af784ca3
2 changed files with 31 additions and 25 deletions

View File

@ -1,12 +1,10 @@
use axum::body::Body; use axum::body::Body;
use axum::extract::Path;
use axum::http::header::AUTHORIZATION; use axum::http::header::AUTHORIZATION;
use axum::http::{Request, StatusCode}; use axum::http::{Request, StatusCode};
use axum::middleware::{from_fn, Next}; use axum::middleware::{from_fn, Next};
use axum::response::{IntoResponse, Response}; use axum::response::{IntoResponse, Response};
use axum::routing::{delete, post}; use axum::routing::{delete, post};
use axum::{Extension, Json, Router}; use axum::{Extension, Router};
use bwhc_dto::MtbFile;
use clap::Parser; use clap::Parser;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -14,11 +12,13 @@ use serde::{Deserialize, Serialize};
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
use crate::cli::Cli; use crate::cli::Cli;
use crate::routes::{handle_delete, handle_post};
use crate::sender::MtbFileSender; use crate::sender::MtbFileSender;
use crate::AppResponse::{Accepted, InternalServerError, Unauthorized}; use crate::AppResponse::{Accepted, InternalServerError, Unauthorized};
mod auth; mod auth;
mod cli; mod cli;
mod routes;
mod sender; mod sender;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -94,28 +94,6 @@ async fn check_basic_auth(request: Request<Body>, next: Next) -> Response {
Unauthorized.into_response() Unauthorized.into_response()
} }
async fn handle_delete(
Path(patient_id): Path<String>,
Extension(sender): Extension<MtbFileSender>,
) -> Response {
let delete_mtb_file = MtbFile::new_with_consent_rejected(&patient_id);
match sender.send(delete_mtb_file).await {
Ok(request_id) => Accepted(&request_id).into_response(),
_ => InternalServerError.into_response(),
}
}
async fn handle_post(
Extension(sender): Extension<MtbFileSender>,
Json(mtb_file): Json<MtbFile>,
) -> Response {
match sender.send(mtb_file).await {
Ok(request_id) => Accepted(&request_id).into_response(),
_ => InternalServerError.into_response(),
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use axum::http::StatusCode; use axum::http::StatusCode;

28
src/routes.rs Normal file
View File

@ -0,0 +1,28 @@
use crate::sender::MtbFileSender;
use crate::AppResponse::{Accepted, InternalServerError};
use axum::extract::Path;
use axum::response::{IntoResponse, Response};
use axum::{Extension, Json};
use bwhc_dto::MtbFile;
pub async fn handle_delete(
Path(patient_id): Path<String>,
Extension(sender): Extension<MtbFileSender>,
) -> Response {
let delete_mtb_file = MtbFile::new_with_consent_rejected(&patient_id);
match sender.send(delete_mtb_file).await {
Ok(request_id) => Accepted(&request_id).into_response(),
_ => InternalServerError.into_response(),
}
}
pub async fn handle_post(
Extension(sender): Extension<MtbFileSender>,
Json(mtb_file): Json<MtbFile>,
) -> Response {
match sender.send(mtb_file).await {
Ok(request_id) => Accepted(&request_id).into_response(),
_ => InternalServerError.into_response(),
}
}