Coder Social home page Coder Social logo

axum-messages's Introduction

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.6.1"

🤸 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!

axum-messages's People

Contributors

maxcountryman avatar hosmercury avatar

Stargazers

Geoff Seemueller avatar wangb avatar Manuel Mauro avatar Artur Corrêa Souza avatar Abhinandan avatar Nikolai Skvortsov avatar Justin ! avatar Pavel Zadorski avatar Brucel Qwe avatar Matthew Aylward  avatar vince avatar  avatar جاد avatar Paul Saunders avatar Sandalots avatar Lina avatar  avatar Serhii Potapov avatar  avatar

Watchers

 avatar  avatar  avatar

axum-messages's Issues

Support extracting messages in one pass?

(I'm curious for thoughts on this, could PR it if it sounds acceptable)

Messages right now locks its inner Mutex on every pass - could it possibly have an into_inner() method that locks once and returns the entire set? Adding on to this, could it have a method for checking if it's empty or not?

The general idea here is that the common rendering case, as far as I understand, is something akin to:

{% if !messages.is_empty() %}
<ul id="my-errors-list-wrapper-that-should-only-exist-if-not-empty">
    {% for message in messages.into_iter() %}
        <li>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}

Axum stop responding after updating to 0.5.0

something weird weird is happening after just updating axum-messages to 0.5.0
axum stop responding to any request
there is no compilation errors .. no tracing
just 500 server err
..

returning to 0.3.0
it works

tbh i am not using the latest tower sessions

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.