#web-framework #web #http #framework

axum-flash

注重用户体验和模块化的Web框架

10个版本 (破坏性更新)

0.8.0 2023年11月28日
0.7.0 2023年5月23日
0.6.0 2022年11月25日
0.4.0 2022年4月1日
0.1.1 2021年11月21日

2366Web编程

Download history 74/week @ 2024-03-11 83/week @ 2024-03-18 127/week @ 2024-03-25 175/week @ 2024-04-01 36/week @ 2024-04-08 64/week @ 2024-04-15 62/week @ 2024-04-22 108/week @ 2024-04-29 105/week @ 2024-05-06 55/week @ 2024-05-13 130/week @ 2024-05-20 75/week @ 2024-05-27 104/week @ 2024-06-03 57/week @ 2024-06-10 107/week @ 2024-06-17 59/week @ 2024-06-24

348 每月下载量
htmx-components 中使用

MIT 许可证

17KB
313

axum-flash

axum 提供一次性通知(又称闪存消息)。

Build status Crates.io Documentation

有关此包的更多信息,请参阅 包文档


lib.rs:

axum 提供一次性通知(又称闪存消息)。

示例

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

依赖项

~7MB
~130K SLoC