Skip to content

Commit

Permalink
rework crate into a middleware
Browse files Browse the repository at this point in the history
This changes the structure of the crate such that it is now a middleware in addition to being an extractor. Doing so allows us to improve the ergonomics of the API such that calling `save` and awaiting a future is no longer needed.

Now applications will need to install the `MeessagesManagerLayer` after `tower-sessions` has been installed (either directly or via a middleware that wraps it).

Also note that the iterator impplementation has been updated to use `Message` directly. Fields of `Message` have been made public as well.
  • Loading branch information
maxcountryman committed Jan 18, 2024
1 parent 710053b commit 117a117
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 76 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Unreleased

# 0.2.0

**Breaking Changes**

- Rework crate into a middleware

This changes the structure of the crate such that it is now a middleware in addition to being an extractor. Doing so allows us to improve the ergonomics of the API such that calling `save` and awaiting a future is no longer needed.

Now applications will need to install the `MeessagesManagerLayer` after `tower-sessions` has been installed (either directly or via a middleware that wraps it).

Also note that the iterator impplementation has been updated to use `Message` directly. Fields of `Message` have been made public as well.

# 0.1.0

- Initial release :tada:
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "axum-messages"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
authors = ["Max Countryman <hello@maxcountryman.com>"]
categories = ["asynchronous", "network-programming", "web-programming"]
description = "🛎️ One-time notification messages for Axum."
homepage = "https://github.com/maxcountryman/axum-messages"
keywords = ["axum", "flash", "message", "messages"]
keywords = ["axum", "flash", "message", "messages", "notification"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/maxcountryman/axum-messages"
Expand All @@ -15,7 +15,9 @@ repository = "https://github.com/maxcountryman/axum-messages"
async-trait = "0.1.77"
axum-core = "0.4.3"
http = "1.0.0"
parking_lot = "0.12.1"
serde = { version = "1.0.195", features = ["derive"] }
tower = "0.4"
tower-sessions-core = "0.9.1"

[dev-dependencies]
Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ To use the crate in your project, add the following to your `Cargo.toml` file:

```toml
[dependencies]
axum-messages = "0.1.0"
axum-messages = "0.2.0"
```

## 🤸 Usage
Expand All @@ -49,7 +49,7 @@ use axum::{
routing::get,
Router,
};
use axum_messages::Messages;
use axum_messages::{Messages, MessagesManagerLayer};
use time::Duration;
use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer};

Expand All @@ -63,6 +63,7 @@ async fn main() {
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));
Expand All @@ -75,7 +76,7 @@ async fn main() {
async fn read_messages_handler(messages: Messages) -> impl IntoResponse {
let messages = messages
.into_iter()
.map(|(level, message)| format!("{:?}: {}", level, message))
.map(|message| format!("{:?}: {}", message.level, message))
.collect::<Vec<_>>()
.join(", ");

Expand All @@ -89,10 +90,7 @@ async fn read_messages_handler(messages: Messages) -> impl IntoResponse {
async fn set_messages_handler(messages: Messages) -> impl IntoResponse {
messages
.info("Hello, world!")
.debug("This is a debug message.")
.save()
.await
.unwrap();
.debug("This is a debug message.");

Redirect::to("/read-messages")
}
Expand Down
10 changes: 4 additions & 6 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use axum::{
routing::get,
Router,
};
use axum_messages::Messages;
use axum_messages::{Messages, MessagesManagerLayer};
use time::Duration;
use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer};

Expand All @@ -19,6 +19,7 @@ async fn main() {
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));
Expand All @@ -31,7 +32,7 @@ async fn main() {
async fn read_messages_handler(messages: Messages) -> impl IntoResponse {
let messages = messages
.into_iter()
.map(|(level, message)| format!("{:?}: {}", level, message))
.map(|message| format!("{:?}: {}", message.level, message))
.collect::<Vec<_>>()
.join(", ");

Expand All @@ -45,10 +46,7 @@ async fn read_messages_handler(messages: Messages) -> impl IntoResponse {
async fn set_messages_handler(messages: Messages) -> impl IntoResponse {
messages
.info("Hello, world!")
.debug("This is a debug message.")
.save()
.await
.unwrap();
.debug("This is a debug message.");

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

0 comments on commit 117a117

Please sign in to comment.