diff --git a/crates/flash/Cargo.toml b/crates/flash/Cargo.toml index 1444b15ed..c916d7ac0 100644 --- a/crates/flash/Cargo.toml +++ b/crates/flash/Cargo.toml @@ -19,7 +19,7 @@ rustdoc-args = ["--cfg", "docsrs"] [features] default = ["cookie_store"] -cookie_store = ["salvo_core/cookie"] +cookie_store = ["salvo_core/cookie", "dep:serde_json"] session_store = ["dep:salvo-session"] [dependencies] @@ -27,9 +27,9 @@ async-session = { workspace = true, optional = true } salvo_core = { workspace = true, default-features = false } salvo_extra = { workspace = true, default-features = false } salvo-session = { workspace = true, default-features = false, optional = true } -tracing = "0.1" -serde = "1" -serde_json = "1" +tracing.workspace = true +serde.workspace = true +serde_json = { workspace = true, optional = true } [dev-dependencies] salvo_core = {workspace = true, features = ["test"] } diff --git a/crates/flash/src/lib.rs b/crates/flash/src/lib.rs index 5db14f209..74114c955 100644 --- a/crates/flash/src/lib.rs +++ b/crates/flash/src/lib.rs @@ -12,11 +12,6 @@ cfg_feature! { mod cookie_store; pub use cookie_store::CookieStore; - - /// Helper function to create a new `CookieStore`. - pub fn cookie_store() -> CookieStore { - CookieStore::new() - } } cfg_feature! { @@ -24,11 +19,6 @@ cfg_feature! { mod session_store; pub use session_store::SessionStore; - - /// Helper function to create a new `SessionStore`. - pub fn session_store() -> SessionStore { - SessionStore::new() - } } /// Key for incoming flash messages in depot. @@ -266,8 +256,10 @@ mod tests { #[handler] pub async fn get_flash(depot: &mut Depot, _res: &mut Response) -> String { let mut body = String::new(); - for message in depot.incoming_flash().unwrap().iter() { - writeln!(body, "{} - {}", message.value, message.level).unwrap(); + if let Some(flash) = depot.incoming_flash() { + for message in flash.iter() { + writeln!(body, "{} - {}", message.value, message.level).unwrap(); + } } body } @@ -277,7 +269,7 @@ mod tests { async fn test_cookie_store() { let cookie_name = "my-custom-cookie-name".to_string(); let router = Router::new() - .hoop(cookie_store().with_name(&cookie_name).into_handler()) + .hoop(CookieStore::new().with_name(&cookie_name).into_handler()) .push(Router::with_path("get").get(get_flash)) .push(Router::with_path("set").get(set_flash)); let service = Service::new(router); @@ -286,7 +278,6 @@ mod tests { assert_eq!(respone.status_code(), Some(StatusCode::SEE_OTHER)); let cookie = respone.headers().get(SET_COOKIE).unwrap(); - println!("cookie: {:?}", cookie); assert!(cookie.to_str().unwrap().contains(&cookie_name)); let mut respone = TestClient::get("http://127.0.0.1:7878/get") @@ -318,7 +309,7 @@ mod tests { let session_name = "my-custom-session-name".to_string(); let router = Router::new() .hoop(session_handler) - .hoop(session_store().with_name(&session_name).into_handler()) + .hoop(SessionStore::new().with_name(&session_name).into_handler()) .push(Router::with_path("get").get(get_flash)) .push(Router::with_path("set").get(set_flash)); let service = Service::new(router); @@ -334,8 +325,6 @@ mod tests { .await; assert!(respone.take_string().await.unwrap().contains("Hey there!")); - let cookie = respone.headers().get(SET_COOKIE).unwrap(); - let mut respone = TestClient::get("http://127.0.0.1:7878/get") .add_header(COOKIE, cookie, true) .send(&service) diff --git a/crates/flash/src/session_store.rs b/crates/flash/src/session_store.rs index f9279d18a..792ed5517 100644 --- a/crates/flash/src/session_store.rs +++ b/crates/flash/src/session_store.rs @@ -1,4 +1,4 @@ -use salvo_core::{async_trait, Depot, Request, Response, Result}; +use salvo_core::{async_trait, Depot, Request, Response}; use salvo_session::SessionDepotExt; use super::{Flash, FlashHandler, FlashStore}; @@ -45,7 +45,7 @@ impl FlashStore for SessionStore { if let Err(e) = depot .session_mut() .expect("session must be exist") - .insert(&self.name, &serde_json::to_string(&flash).unwrap_or_default()) + .insert(&self.name, &flash) { tracing::error!(error = ?e, "save flash to session failed"); } diff --git a/examples/flash-cookie/src/main.rs b/examples/flash-cookie/src/main.rs index a6b67884d..c008f2943 100644 --- a/examples/flash-cookie/src/main.rs +++ b/examples/flash-cookie/src/main.rs @@ -1,20 +1,24 @@ +use std::fmt::Write; + use salvo::prelude::*; +use salvo_flash::{CookieStore, FlashDepotExt}; #[handler] -async fn hello_world() -> &'static str { - "Hello World" -} -#[handler] -async fn hello_world1() -> Result<&'static str, ()> { - Ok("Hello World1") -} -#[handler] -async fn hello_world2(res: &mut Response) { - res.render("Hello World2"); +pub async fn set_flash(depot: &mut Depot, res: &mut Response) { + let flash = depot.outgoing_flash_mut(); + flash.info("Hey there!").debug("How is it going?"); + res.render(Redirect::other("/get").unwrap()); } + #[handler] -async fn hello_world3(_req: &mut Request, res: &mut Response) { - res.render(Text::Plain("Hello World3")); +pub async fn get_flash(depot: &mut Depot, _res: &mut Response) -> String { + let mut body = String::new(); + if let Some(flash) = depot.incoming_flash() { + for message in flash.iter() { + writeln!(body, "{} - {}", message.value, message.level).unwrap(); + } + } + body } #[tokio::main] @@ -22,37 +26,9 @@ async fn main() { tracing_subscriber::fmt().init(); tracing::info!("Listening on http://127.0.0.1:7878"); - Server::new(TcpListener::bind("127.0.0.1:7878")).serve(route()).await; -} - -fn route() -> Router { - Router::new() - .get(hello_world) - .push(Router::with_path("hello1").get(hello_world1)) - .push(Router::with_path("hello2").get(hello_world2)) - .push(Router::with_path("hello3").get(hello_world3)) -} - -#[cfg(test)] -mod tests { - use salvo::prelude::*; - use salvo::test::{ResponseExt, TestClient}; - - #[tokio::test] - async fn test_hello_world() { - let service = Service::new(super::route()); - - async fn access(service: &Service, name: &str) -> String { - TestClient::get(format!("http://127.0.0.1:7878/{}", name)) - .send(service) - .await - .take_string() - .await - .unwrap() - } - - assert_eq!(access(&service, "hello1").await, "Hello World1"); - assert_eq!(access(&service, "hello2").await, "Hello World2"); - assert_eq!(access(&service, "hello3").await, "Hello World3"); - } + let router = Router::new() + .hoop(CookieStore::new().into_handler()) + .push(Router::with_path("get").get(get_flash)) + .push(Router::with_path("set").get(set_flash)); + Server::new(TcpListener::bind("127.0.0.1:7878")).serve(router).await; }