#web-framework #web #http #framework

axum-flash

Web framework that focuses on ergonomics and modularity

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

#2340 in Web programming

Download history 50/week @ 2024-07-20 154/week @ 2024-07-27 101/week @ 2024-08-03 52/week @ 2024-08-10 56/week @ 2024-08-17 43/week @ 2024-08-24 54/week @ 2024-08-31 59/week @ 2024-09-07 51/week @ 2024-09-14 90/week @ 2024-09-21 52/week @ 2024-09-28 46/week @ 2024-10-05 62/week @ 2024-10-12 74/week @ 2024-10-19 95/week @ 2024-10-26 106/week @ 2024-11-02

339 downloads per month
Used in htmx-components

MIT license

17KB
313 lines

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.


lib.rs:

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("/"),
    )
}

Dependencies

~6.5MB
~128K SLoC