The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Set, Read, And Delete Cookies In axum

TODO: Pull subtitle into page object
Code
[package]
name = "axum_cookies"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.6" 
tokio = { version = "1", features = ["full"] }
tower-cookies = "0.9"
Code
use axum::response::IntoResponse;
use axum::routing::get;
use axum::{response::Html, Router};
use std::net::SocketAddr;
use tower_cookies::{Cookie, CookieManagerLayer, Cookies};

#[tokio::main]
async fn main() {
    let routes_all = Router::new()
        .route("/", get(set_cookie_handler))
        .route("/read-cookie", get(read_cookie_handler))
        .route("/delete-cookie", get(delete_cookie_handler))
        .layer(CookieManagerLayer::new());
    let addr = SocketAddr::from(([127, 0, 0, 1], 8484));
    axum::Server::bind(&addr)
        .serve(routes_all.into_make_service())
        .await
        .unwrap();
}

async fn set_cookie_handler(cookies: Cookies) -> impl IntoResponse {
    cookies.add(Cookie::new("delta", "echo"));
    Html("Cookie has been sent to browser".to_string())
}

async fn read_cookie_handler(cookies: Cookies) -> impl IntoResponse {
    match cookies.get("delta") {
        Some(cookie) => {
            dbg!(&cookie.value());
            Html(format!("Cookie value is: {}", cookie.value()))
        }
        None => Html(format!("No cookie found")),
    }
}

async fn delete_cookie_handler(cookies: Cookies) -> impl IntoResponse {
    cookies.remove(Cookie::new("delta", ""));
    Html("Cookie deleted".to_string())
}

// add CookieMangerLayer. Must be under
// other routes that want to use cookies

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Set, Read, And Delete Cookies In axum


-- code
-- name: Cargo.toml

[package]
name = "axum_cookies"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.6" 
tokio = { version = "1", features = ["full"] }
tower-cookies = "0.9"


-- code
-- name: src/main.rs

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

#[tokio::main]
async fn main() {
    let routes_all = Router::new()
        .route("/", get(set_cookie_handler))
        .route("/read-cookie", get(read_cookie_handler))
        .route("/delete-cookie", get(delete_cookie_handler))
        .layer(CookieManagerLayer::new());
    let addr = SocketAddr::from(([127, 0, 0, 1], 8484));
    axum::Server::bind(&addr)
        .serve(routes_all.into_make_service())
        .await
        .unwrap();
}

async fn set_cookie_handler(cookies: Cookies) -> impl IntoResponse {
    cookies.add(Cookie::new("delta", "echo"));
    Html("Cookie has been sent to browser".to_string())
}

async fn read_cookie_handler(cookies: Cookies) -> impl IntoResponse {
    match cookies.get("delta") {
        Some(cookie) => {
            dbg!(&cookie.value());
            Html(format!("Cookie value is: {}", cookie.value()))
        }
        None => Html(format!("No cookie found")),
    }
}

async fn delete_cookie_handler(cookies: Cookies) -> impl IntoResponse {
    cookies.remove(Cookie::new("delta", ""));
    Html("Cookie deleted".to_string())
}

// add CookieMangerLayer. Must be under
// other routes that want to use cookies


-- ref
-- url: https://docs.rs/tower-cookies/latest/tower_cookies/

-- ref
-- url: https://docs.rs/axum/latest/axum/index.html


-- ref
-- url: https://docs.rs/tower-cookies/latest/tower_cookies/struct.Cookies.html#method.remove



-- categories
-- Rust 
-- axum

-- metadata
-- date: 2023-09-08 23:19:50
-- id: 2v8tzudn
-- site: aws
-- type: post
-- status: scratch