Moved more code from hotel_service to lib
Some checks failed
Build & test / build (push) Failing after 6s
Some checks failed
Build & test / build (push) Failing after 6s
This commit is contained in:
@ -152,11 +152,13 @@ impl AppBuilder {
|
||||
let _ = fmt_trace(); // Allowed to fail
|
||||
let listener = self.listener().await?;
|
||||
|
||||
if self.normalize_path.unwrap_or(true) {
|
||||
let app = NormalizePathLayer::trim_trailing_slash().layer(self.build());
|
||||
let should_normalize = self.normalize_path.unwrap_or(true);
|
||||
let app = self.build();
|
||||
|
||||
if should_normalize {
|
||||
let app = NormalizePathLayer::trim_trailing_slash().layer(app);
|
||||
axum::serve(listener, ServiceExt::<Request>::into_make_service(app)).await?;
|
||||
} else {
|
||||
let app = self.build();
|
||||
axum::serve(listener, app.into_make_service()).await?;
|
||||
};
|
||||
Ok(())
|
||||
|
14
src/axum/builder.rs
Normal file
14
src/axum/builder.rs
Normal file
@ -0,0 +1,14 @@
|
||||
use crate::axum::traits::BuildJson;
|
||||
use axum::body::Body;
|
||||
use axum::http::header::CONTENT_TYPE;
|
||||
use axum::http::Request;
|
||||
use mime::APPLICATION_JSON;
|
||||
use serde::Serialize;
|
||||
use serde_json::json;
|
||||
|
||||
impl BuildJson for axum::http::request::Builder {
|
||||
fn json<T: Serialize>(self, body: T) -> Result<Request<Body>, axum::http::Error> {
|
||||
self.header(CONTENT_TYPE, APPLICATION_JSON.as_ref())
|
||||
.body(Body::new(json!(body).to_string()))
|
||||
}
|
||||
}
|
@ -1,8 +1,11 @@
|
||||
pub mod app;
|
||||
#[cfg(feature = "serde")]
|
||||
pub mod builder;
|
||||
pub mod extractor;
|
||||
pub mod load;
|
||||
#[cfg(feature = "serde")]
|
||||
pub mod response;
|
||||
pub mod router;
|
||||
pub mod traits;
|
||||
#[cfg(feature = "serde")]
|
||||
pub mod wrappers;
|
||||
|
@ -1,10 +1,15 @@
|
||||
use {
|
||||
crate::serde::response::BaseResponse,
|
||||
crate::{serde::response::BaseResponse, serde::traits::DeserializeInto},
|
||||
async_trait::async_trait,
|
||||
axum::{
|
||||
body::to_bytes,
|
||||
response::{IntoResponse, Response},
|
||||
Json,
|
||||
},
|
||||
serde::Serialize,
|
||||
serde::{
|
||||
de::{DeserializeOwned, Error},
|
||||
Serialize,
|
||||
},
|
||||
};
|
||||
|
||||
impl<T: Serialize> IntoResponse for BaseResponse<T> {
|
||||
@ -13,6 +18,16 @@ impl<T: Serialize> IntoResponse for BaseResponse<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DeserializeInto for Response {
|
||||
async fn deserialize_into<T: DeserializeOwned>(self) -> Result<T, serde_json::Error> {
|
||||
let body = to_bytes(self.into_body(), usize::MAX).await.map_err(|e| {
|
||||
serde_json::Error::custom(format!("Failed to read response body: {}", e))
|
||||
})?;
|
||||
serde_json::from_slice(&body)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use axum::http::header::CONTENT_TYPE;
|
||||
|
7
src/axum/traits.rs
Normal file
7
src/axum/traits.rs
Normal file
@ -0,0 +1,7 @@
|
||||
use axum::body::Body;
|
||||
use axum::http::Request;
|
||||
use serde::Serialize;
|
||||
|
||||
pub trait BuildJson {
|
||||
fn json<T: Serialize>(self, body: T) -> Result<Request<Body>, axum::http::Error>;
|
||||
}
|
Reference in New Issue
Block a user