Skip to content

Commit

Permalink
Add support for flash message #158
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Sep 28, 2022
1 parent bff3def commit 10e1eb0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
6 changes: 3 additions & 3 deletions crates/flash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["cookie_store"]
cookie_store = ["salvo_core/cookie", "dep:serde_json"]
session_store = ["dep:salvo-session"]
default = ["cookie-store"]
cookie-store = ["salvo_core/cookie", "dep:serde_json"]
session-store = ["dep:salvo-session"]

[dependencies]
async-session = { workspace = true, optional = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/flash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use serde::{Deserialize, Serialize};
mod cfg;

cfg_feature! {
#![feature = "cookie_store"]
#![feature = "cookie-store"]

mod cookie_store;
pub use cookie_store::CookieStore;
}

cfg_feature! {
#![feature = "session_store"]
#![feature = "session-store"]

mod session_store;
pub use session_store::SessionStore;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "example-flash-cookie"
name = "example-flash-cookie-store"
version = "0.1.0"
edition = "2021"
publish = false
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions examples/flash-session-store/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "example-flash-session-store"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
salvo = { path = "../../crates/salvo" }
salvo-flash = { default-features = false, features = ["session-store"], path = "../../crates/flash" }
salvo-session = { path = "../../crates/session" }
tokio = { version = "1", features = ["macros"] }
tracing = "0.1"
tracing-subscriber = "0.3"
41 changes: 41 additions & 0 deletions examples/flash-session-store/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::fmt::Write;

use salvo::prelude::*;
use salvo_flash::{SessionStore, FlashDepotExt};

#[handler]
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]
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]
async fn main() {
tracing_subscriber::fmt().init();

tracing::info!("Listening on http://127.0.0.1:7878");
let session_handler = salvo_session::SessionHandler::builder(
salvo_session::MemoryStore::new(),
b"secretabsecretabsecretabsecretabsecretabsecretabsecretabsecretab",
)
.build()
.unwrap();
let router = Router::new()
.hoop(session_handler)
.hoop(SessionStore::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;
}

0 comments on commit 10e1eb0

Please sign in to comment.