10 releases (breaking)

0.8.0 Nov 28, 2023
0.7.0 May 23, 2023
0.6.0 Nov 25, 2022
0.4.0 Apr 1, 2022
0.1.1 Nov 21, 2021

#2205 in Asynchronous

Download history 94/week @ 2024-12-18 4/week @ 2024-12-25 45/week @ 2025-01-01 48/week @ 2025-01-08 95/week @ 2025-01-15 87/week @ 2025-01-22 37/week @ 2025-01-29 80/week @ 2025-02-05 64/week @ 2025-02-12 50/week @ 2025-02-19 91/week @ 2025-02-26 28/week @ 2025-03-05 71/week @ 2025-03-12 29/week @ 2025-03-19 52/week @ 2025-03-26 42/week @ 2025-04-02

200 downloads per month
Used in htmx-components

MIT license

17KB
313 lines

One-time notifications (aka flash messages) for axum.

Example

use axum::{
    response::{IntoResponse, Redirect},
    extract::FromRef,
    routing::get,
    Router,
};
use axum_flash::{IncomingFlashes, Flash, Key};

#[derive(Clone)]
struct AppState {
    flash_config: axum_flash::Config,
}

let app_state = AppState {
    // The key should probably come from configuration
    flash_config: axum_flash::Config::new(Key::generate()),
};

// Our state type must implement this trait. That is how the config
// is passed to axum-flash in a type safe way.
impl FromRef<AppState> for axum_flash::Config {
    fn from_ref(state: &AppState) -> axum_flash::Config {
        state.flash_config.clone()
    }
}

let app = Router::new()
    .route("/", get(root))
    .route("/set-flash", get(set_flash))
    .with_state(app_state);

async fn root(flashes: IncomingFlashes) -> IncomingFlashes {
    for (level, text) in &flashes {
        // ...
    }

    // The flashes must be returned so the cookie is removed
    flashes
}

async fn set_flash(flash: Flash) -> (Flash, Redirect) {
    (
        // The flash must be returned so the cookie is set
        flash.debug("Hi from flash!"),
        Redirect::to("/"),
    )
}

axum-flash

One-time notifications (aka flash messages) for axum.

Build status Crates.io Documentation

More information about this crate can be found in the crate documentation.

Dependencies

~7MB
~137K SLoC