diff --git a/Cargo.lock b/Cargo.lock index 0afbf9f2..c7f84a05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -343,6 +343,7 @@ dependencies = [ "tower", "tower-http", "tower-service", + "tracing", "uuid", ] diff --git a/Cargo.toml b/Cargo.toml index ca4bc076..d0c11b4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,9 +55,7 @@ mime = "0.3.17" # aws-sdk-s3 = "0.31.1" rust-s3 = "0.33.0" redis = "0.23.3" - -# tracing -tracing = { version = "0.1.37" } +tracing = "0.1.37" tracing-subscriber = { version = "0.3.16", features = ["registry", "env-filter", "ansi", "json"] } tracing-bunyan-formatter = "0.3.6" tracing-actix-web = "0.7" diff --git a/admin_frontend/Cargo.toml b/admin_frontend/Cargo.toml index 1e926d8e..a35b6efc 100644 --- a/admin_frontend/Cargo.toml +++ b/admin_frontend/Cargo.toml @@ -24,3 +24,4 @@ reqwest = "0.11.22" tower-service = "0.3.2" tower-http = { version = "0.4.4", features = ["cors"] } tower = "0.4.13" +tracing = "0.1.37" diff --git a/admin_frontend/src/access_token.rs b/admin_frontend/src/access_token.rs deleted file mode 100644 index 32e2e955..00000000 --- a/admin_frontend/src/access_token.rs +++ /dev/null @@ -1,48 +0,0 @@ -// use axum::{ -// async_trait, -// extract::FromRequestParts, -// http::request::Parts, -// response::{IntoResponse, Redirect}, -// }; -// use axum_extra::extract::CookieJar; -// -// pub struct WebAccessToken(pub String); -// -// #[async_trait] -// impl FromRequestParts for WebAccessToken -// where -// S: Send + Sync, -// { -// type Rejection = AccessTokenRejection; -// -// async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { -// let jar = CookieJar::from_request_parts(parts, state) -// .await -// .map_err(|e| AccessTokenRejection::CookieError(e.to_string()))?; -// -// let token = jar -// .get("access_token") -// .ok_or(AccessTokenRejection::NoAccessToken)? -// .value(); -// -// Ok(WebAccessToken(token.to_string())) -// } -// } -// -// #[derive(Clone, Debug)] -// pub enum AccessTokenRejection { -// NoAccessToken, -// CookieError(String), -// } -// -// impl IntoResponse for AccessTokenRejection { -// fn into_response(self) -> axum::response::Response { -// match self { -// AccessTokenRejection::NoAccessToken => Redirect::permanent("/web/login").into_response(), -// AccessTokenRejection::CookieError(err) => { -// println!("cookie error: {}", err); -// Redirect::permanent("/web/login").into_response() -// }, -// } -// } -// } diff --git a/admin_frontend/src/main.rs b/admin_frontend/src/main.rs index 25cbaac7..beb1269c 100644 --- a/admin_frontend/src/main.rs +++ b/admin_frontend/src/main.rs @@ -1,4 +1,3 @@ -mod access_token; mod error; mod models; mod response; diff --git a/admin_frontend/src/session.rs b/admin_frontend/src/session.rs index 6bb38667..9bea1925 100644 --- a/admin_frontend/src/session.rs +++ b/admin_frontend/src/session.rs @@ -32,7 +32,7 @@ impl SessionStorage { match s { Ok(s) => Some(s), Err(e) => { - println!("redis error: {:?}", e); + tracing::info!("get user session in redis error: {:?}", e); None }, } @@ -53,7 +53,9 @@ impl SessionStorage { pub async fn del_user_session(&self, session_id: &str) -> redis::RedisResult<()> { let key = session_id_key(session_id); - self.redis_client.clone().del::<_, ()>(key).await + let res = self.redis_client.clone().del::<_, i64>(key).await?; + tracing::info!("del user session: {} res: {}", session_id, res); + Ok(()) } } @@ -113,7 +115,7 @@ impl IntoResponse for SessionRejection { match self { SessionRejection::NoSessionId => Redirect::permanent("/web/login").into_response(), SessionRejection::CookieError(err) => { - println!("cookie error: {}", err); + tracing::error!("session rejection cookie error: {}", err); Redirect::permanent("/web/login").into_response() }, SessionRejection::SessionNotFound => Redirect::permanent("/web/login").into_response(), diff --git a/admin_frontend/src/web_app.rs b/admin_frontend/src/web_app.rs index faf32ba8..06c37cd5 100644 --- a/admin_frontend/src/web_app.rs +++ b/admin_frontend/src/web_app.rs @@ -32,7 +32,7 @@ pub async fn home_handler( .await .map(|user_info| user_info.email) .unwrap_or_else(|err| { - println!("Failed to fetch user info: {:?}", err); + tracing::error!("Error getting user info: {:?}", err); "".to_owned() }); @@ -55,8 +55,7 @@ pub async fn admin_users_handler( .await .map_or_else( |err| { - // Log the error and return an empty vector. - println!("Failed to fetch users: {:?}", err); + tracing::error!("Error getting user list: {:?}", err); vec![] }, |r| r.users, diff --git a/admin_frontend/templates/home.html b/admin_frontend/templates/home.html index be753bdd..4d38aca8 100644 --- a/admin_frontend/templates/home.html +++ b/admin_frontend/templates/home.html @@ -17,11 +17,22 @@ .getElementById("logoutBtn") .addEventListener("click", function () { // Remove the cookie by setting its value to an empty string and its expiry date to the past. - document.cookie = - "access_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; - - // Redirect to the login page. - window.location.href = "/web/login"; + fetch("/web-api/logout", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + credentials: 'same-origin', + }) + .then(response => { + if (!response.ok) { + throw new Error('Network response was not ok' + response.statusText); + } + window.location.href = "/web/login"; + }) + .catch(error => { + console.error('There was a problem with the fetch operation:', error); + }); }); document diff --git a/admin_frontend/templates/login.html b/admin_frontend/templates/login.html index 4b1fab4d..8a1d80ef 100644 --- a/admin_frontend/templates/login.html +++ b/admin_frontend/templates/login.html @@ -41,19 +41,18 @@ body: JSON.stringify(data), credentials: 'same-origin', }) - .then((response) => { - if (!response.ok) { - // If HTTP status code is not OK, throw an error with the status text - throw Error(response.statusText); - } - - window.location.href = "/web/home"; - }) - .catch((error) => { - console.error("Error:", error); - document.getElementById("response").innerText = - "Login failed: " + error.message; - }); + .then((response) => { + if (!response.ok) { + // If HTTP status code is not OK, throw an error with the status text + throw Error(response.statusText); + } + window.location.href = "/web/home"; + }) + .catch((error) => { + console.error("Error:", error); + document.getElementById("response").innerText = + "Login failed: " + error.message; + }); });