16 releases (10 breaking)

0.11.0 Jan 1, 2025
0.10.0 Nov 27, 2023
0.9.0 Feb 12, 2023
0.8.0 Nov 25, 2022
0.3.0 Nov 18, 2021

#89 in Web programming

Download history 19594/week @ 2024-09-23 20858/week @ 2024-09-30 17879/week @ 2024-10-07 18082/week @ 2024-10-14 21861/week @ 2024-10-21 18903/week @ 2024-10-28 23427/week @ 2024-11-04 20345/week @ 2024-11-11 22602/week @ 2024-11-18 19598/week @ 2024-11-25 25241/week @ 2024-12-02 22704/week @ 2024-12-09 25261/week @ 2024-12-16 6853/week @ 2024-12-23 12278/week @ 2024-12-30 24593/week @ 2025-01-06

69,759 downloads per month
Used in 57 crates (23 directly)

MIT license

26KB
453 lines

License Crates.io Docs.rs

tower-cookies

A cookie manager middleware built on top of tower.

Example

With axum:

use axum::{routing::get, Router};
use std::net::SocketAddr;
use tower_cookies::{Cookie, CookieManagerLayer, Cookies};

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/", get(handler))
        .layer(CookieManagerLayer::new());

    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();
}

async fn handler(cookies: Cookies) -> &'static str {
    cookies.add(Cookie::new("hello_world", "hello_world"));

    "Check your cookies."
}

A complete CRUD cookie example in examples/counter.rs

Safety

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

Contributing

We appreciate all kinds of contributions, thank you!

Note on README

Most of the readme is automatically copied from the crate documentation by cargo-sync-readme. This way the readme is always in sync with the docs and examples are tested.

So if you find a part of the readme you'd like to change between <!-- cargo-sync-readme start --> and <!-- cargo-sync-readme end --> markers, don't edit README.md directly, but rather change the documentation on top of src/lib.rs and then synchronize the readme with:

cargo sync-readme

(make sure the cargo command is installed):

cargo install cargo-sync-readme

If you have rusty-hook installed the changes will apply automatically on commit.

License

This project is licensed under the MIT license.

Dependencies

~2.5–7.5MB
~59K SLoC