#http #web #framework

axum-flash

Web framework that focuses on ergonomics and modularity

8 releases (5 breaking)

0.6.0 Nov 25, 2022
0.6.0-rc.1 Sep 3, 2022
0.5.0 Aug 23, 2022
0.4.0 Apr 1, 2022
0.1.1 Nov 21, 2021

#662 in Web programming

Download history 91/week @ 2022-08-14 82/week @ 2022-08-21 125/week @ 2022-08-28 114/week @ 2022-09-04 95/week @ 2022-09-11 95/week @ 2022-09-18 66/week @ 2022-09-25 85/week @ 2022-10-02 78/week @ 2022-10-09 59/week @ 2022-10-16 89/week @ 2022-10-23 86/week @ 2022-10-30 39/week @ 2022-11-06 16/week @ 2022-11-13 38/week @ 2022-11-20 69/week @ 2022-11-27

163 downloads per month

MIT license

16KB
312 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 {
    // Thek 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("/"),
    )
}
# async {
# axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
# };

Dependencies

~8–14MB
~241K SLoC