10 releases (6 breaking)

0.7.0 Sep 4, 2024
0.6.1 Apr 15, 2024
0.6.0 Mar 20, 2024
0.5.0 Mar 9, 2024
0.1.0 Jan 17, 2024

#610 in Network programming

Download history 127/week @ 2024-07-20 173/week @ 2024-07-27 172/week @ 2024-08-03 156/week @ 2024-08-10 128/week @ 2024-08-17 264/week @ 2024-08-24 468/week @ 2024-08-31 344/week @ 2024-09-07 343/week @ 2024-09-14 586/week @ 2024-09-21 369/week @ 2024-09-28 346/week @ 2024-10-05 264/week @ 2024-10-12 390/week @ 2024-10-19 516/week @ 2024-10-26 680/week @ 2024-11-02

1,896 downloads per month

MIT license

24KB
305 lines

axum-messages

🛎️ One-time notification messages for Axum.

🎨 Overview

This crate provides one-time notification messages, or flash messages, for axum applications.

It's built on top of tower-sessions, so applications that already use tower-sessions can use this crate with minimal setup.

For an implementation that uses axum-extra cookies, please see axum-flash; axum-messages borrows from that crate, but simplifies the API by leveraging tower-sessions.

This crate's implementation is inspired by the Django messages framework.

📦 Install

To use the crate in your project, add the following to your Cargo.toml file:

[dependencies]
axum-messages = "0.7.0"

🤸 Usage

Example

use std::net::SocketAddr;

use axum::{
    response::{IntoResponse, Redirect},
    routing::get,
    Router,
};
use axum_messages::{Messages, MessagesManagerLayer};
use tower_sessions::{MemoryStore, SessionManagerLayer};

async fn set_messages_handler(messages: Messages) -> impl IntoResponse {
    messages
        .info("Hello, world!")
        .debug("This is a debug message.");

    Redirect::to("/read-messages")
}

async fn read_messages_handler(messages: Messages) -> impl IntoResponse {
    let messages = messages
        .into_iter()
        .map(|message| format!("{}: {}", message.level, message))
        .collect::<Vec<_>>()
        .join(", ");

    if messages.is_empty() {
        "No messages yet!".to_string()
    } else {
        messages
    }
}

#[tokio::main]
async fn main() {
    let session_store = MemoryStore::default();
    let session_layer = SessionManagerLayer::new(session_store).with_secure(false);

    let app = Router::new()
        .route("/", get(set_messages_handler))
        .route("/read-messages", get(read_messages_handler))
        .layer(MessagesManagerLayer)
        .layer(session_layer);

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
    axum::serve(listener, app.into_make_service())
        .await
        .unwrap();
}

You can find this example in the example directory.

🦺 Safety

This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% safe Rust.

🛟 Getting Help

We've put together a number of examples to help get you started. You're also welcome to open a discussion and ask additional questions you might have.

👯 Contributing

We appreciate all kinds of contributions, thank you!

Dependencies

~6–13MB
~158K SLoC