summaryrefslogtreecommitdiff
path: root/src/types
diff options
context:
space:
mode:
Diffstat (limited to 'src/types')
-rw-r--r--src/types/extract.rs29
1 files changed, 21 insertions, 8 deletions
diff --git a/src/types/extract.rs b/src/types/extract.rs
index bb50aa7..1379828 100644
--- a/src/types/extract.rs
+++ b/src/types/extract.rs
@@ -1,10 +1,10 @@
-use std::io::Read;
+use std::{io::Read, net::SocketAddr};
-use axum::{extract::{FromRequestParts, FromRequest}, async_trait, response::Response, http::{request::Parts, Request}, TypedHeader, headers::Cookie, body::HttpBody, BoxError};
+use axum::{extract::{FromRequestParts, FromRequest, ConnectInfo}, async_trait, response::Response, http::{request::Parts, Request}, TypedHeader, headers::Cookie, body::HttpBody, BoxError, RequestExt};
use bytes::Bytes;
use serde::de::DeserializeOwned;
-use crate::types::{user::User, response::{ResponseCode, Result}, session::Session};
+use crate::{types::{user::User, response::{ResponseCode, Result}, session::Session}, console};
pub struct AuthorizedUser(pub User);
@@ -39,24 +39,33 @@ pub struct Json<T>(pub T);
#[async_trait]
impl<T, S, B> FromRequest<S, B> for Json<T> where
T: DeserializeOwned + Check,
- B: HttpBody + Send + 'static,
+ B: HttpBody + Sync + Send + 'static,
B::Data: Send,
B::Error: Into<BoxError>,
S: Send + Sync,
{
type Rejection = Response;
- async fn from_request(req: Request<B>, state: &S) -> Result<Self> {
+ async fn from_request(mut req: Request<B>, state: &S) -> Result<Self> {
+
+ let Ok(ConnectInfo(info)) = req.extract_parts::<ConnectInfo<SocketAddr>>().await else {
+ return Err(ResponseCode::InternalServerError.msg("Failed to read connection info"));
+ };
+ let method = req.method().clone();
+ let path = req.extensions().get::<RouterURI>().unwrap().0;
+ let uri = req.uri().clone();
let Ok(bytes) = Bytes::from_request(req, state).await else {
return Err(ResponseCode::InternalServerError.msg("Failed to read request body"));
};
- let Ok(string) = String::from_utf8(bytes.bytes().flatten().collect()) else {
+ let Ok(body) = String::from_utf8(bytes.bytes().flatten().collect()) else {
return Err(ResponseCode::BadRequest.msg("Invalid utf8 body"))
};
+
+ console::log(&info.ip(), &method, &uri, Some(path), Some(&body)).await;
- let Ok(value) = serde_json::from_str::<T>(&string) else {
+ let Ok(value) = serde_json::from_str::<T>(&body) else {
return Err(ResponseCode::BadRequest.msg("Invalid request body"))
};
@@ -87,4 +96,8 @@ pub trait Check {
}
Ok(())
}
-} \ No newline at end of file
+}
+
+#[derive(Clone)]
+pub struct RouterURI(pub &'static str);
+