From 7805c730e8ead9cd421f45d4720fd7f4062675e6 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Sun, 29 Jan 2023 00:35:06 -0500 Subject: no mass rerendering html plus logging fix --- src/api/pages.rs | 14 +++++++------- src/console.rs | 5 +---- src/main.rs | 15 ++++----------- src/types/extract.rs | 5 ++--- 4 files changed, 14 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/api/pages.rs b/src/api/pages.rs index 4661b91..9149744 100644 --- a/src/api/pages.rs +++ b/src/api/pages.rs @@ -6,10 +6,10 @@ use axum::{ use crate::{ console, - types::{extract::AuthorizedUser, http::ResponseCode}, + types::{extract::{AuthorizedUser, Log}, http::ResponseCode}, }; -async fn root(user: Option) -> Response { +async fn root(user: Option, _: Log) -> Response { if user.is_some() { Redirect::to("/home").into_response() } else { @@ -17,7 +17,7 @@ async fn root(user: Option) -> Response { } } -async fn login(user: Option) -> Response { +async fn login(user: Option, _: Log) -> Response { if user.is_some() { Redirect::to("/home").into_response() } else { @@ -25,7 +25,7 @@ async fn login(user: Option) -> Response { } } -async fn home(user: Option) -> Response { +async fn home(user: Option, _: Log) -> Response { if user.is_none() { Redirect::to("/login").into_response() } else { @@ -33,7 +33,7 @@ async fn home(user: Option) -> Response { } } -async fn people(user: Option) -> Response { +async fn people(user: Option, _: Log) -> Response { if user.is_none() { Redirect::to("/login").into_response() } else { @@ -41,7 +41,7 @@ async fn people(user: Option) -> Response { } } -async fn profile(user: Option) -> Response { +async fn profile(user: Option, _: Log) -> Response { if user.is_none() { Redirect::to("/login").into_response() } else { @@ -53,7 +53,7 @@ async fn console() -> Response { console::generate().await } -async fn wordpress() -> Response { +async fn wordpress(_: Log) -> Response { ResponseCode::ImATeapot.text("Hello i am a teapot owo") } diff --git a/src/console.rs b/src/console.rs index 14324fa..eb78c6a 100644 --- a/src/console.rs +++ b/src/console.rs @@ -45,10 +45,7 @@ lazy_static! { } pub async fn log(ip: IpAddr, method: Method, uri: Uri, path: Option, body: Option) { - if uri.to_string().starts_with("/console") { - return; - } - + let path = path.unwrap_or_default(); let body = body.unwrap_or_default(); diff --git a/src/main.rs b/src/main.rs index cd137b9..20627d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ use axum::{ body::HttpBody, - extract::ConnectInfo, http::{Request, StatusCode}, middleware::{self, Next}, response::Response, Extension, RequestExt, Router, }; +use axum_client_ip::ClientIp; use std::{net::SocketAddr, process::exit}; use tower_cookies::CookieManagerLayer; use tracing::{error, info, metadata::LevelFilter}; @@ -40,18 +40,11 @@ async fn log(mut req: Request, next: Next) -> Response where B: Send + Sync + 'static + HttpBody, { - let Ok(ConnectInfo(info)) = req.extract_parts::>().await else { + let Ok(ClientIp(ip)) = req.extract_parts::().await else { return next.run(req).await }; - console::log( - info.ip(), - req.method().clone(), - req.uri().clone(), - None, - None, - ) - .await; + console::log(ip, req.method().clone(), req.uri().clone(), None, None).await; next.run(req).await } @@ -80,9 +73,9 @@ async fn main() { let app = Router::new() .fallback(not_found) - .nest("/", pages::router()) .layer(middleware::from_fn(log)) .layer(middleware::from_fn(serve)) + .nest("/", pages::router()) .nest( "/api/auth", auth::router().layer(Extension(RouterURI("/api/auth"))), diff --git a/src/types/extract.rs b/src/types/extract.rs index e79aa7a..4d92a3b 100644 --- a/src/types/extract.rs +++ b/src/types/extract.rs @@ -85,10 +85,9 @@ where type Rejection = Response; async fn from_request(req: Request, state: &S) -> Result { - let body = match parse_body(req, state).await { Ok(body) => body, - Err(err) => return Err(err) + Err(err) => return Err(err), }; let Ok(value) = serde_json::from_str::(&body) else { @@ -128,7 +127,7 @@ where B: HttpBody + Sync + Send + 'static, B::Data: Send, B::Error: Into, - S: Send + Sync + S: Send + Sync, { let Ok(ClientIp(ip)) = req.extract_parts::().await else { tracing::error!("Failed to read client ip"); -- cgit v1.2.3-freya