minimal session use example

This commit is contained in:
Joe Ardent 2024-03-03 15:12:18 -08:00
commit f0a8840f29
5 changed files with 1189 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

4
.rustfmt.toml Normal file
View File

@ -0,0 +1,4 @@
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
wrap_comments = true
edition = "2021"

1092
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

14
Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "princess"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = { version = "0.7", default-features = false, features = ["tokio", "http1"] }
lazy_static = "1"
rand = { version = "0.8", default-features = false, features = ["getrandom"] }
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = { version = "1", default-features = false }
time = { version = "0.3", default-features = false }
tokio = { version = "1", default-features = false, features = ["rt-multi-thread"] }
tower-sessions = { version = "0.10", default-features = false, features = ["axum-core", "memory-store"] }

78
src/main.rs Normal file
View File

@ -0,0 +1,78 @@
use std::net::SocketAddr;
use axum::{
response::{Html, IntoResponse, Redirect},
routing::get,
Router,
};
use serde::{Deserialize, Serialize};
use tokio::net::TcpListener;
use tower_sessions::{Expiry, MemoryStore, Session, SessionManagerLayer};
#[macro_use]
extern crate lazy_static;
lazy_static! {
static ref SIGNUP_KEY: String = format!("meow-{}", rand::random::<u128>());
}
#[tokio::main]
async fn main() {
let session_store = MemoryStore::default();
let session_layer = SessionManagerLayer::new(session_store)
.with_secure(false)
.with_expiry(Expiry::OnInactivity(time::Duration::hours(2)));
let app = Router::new()
.route("/signup", get(get_signup).post(post_signup))
.route("/payment_success/:receipt", get(payment_success))
.layer(session_layer)
.into_make_service();
let addr = SocketAddr::from(([127, 0, 0, 1], 4000));
let listener = TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
}
pub async fn get_signup() -> impl IntoResponse {
let html: Html<&str> =
"<p><form action='/signup' enctype='application/x-www-form-urlencoded' method='post'>
<input type='submit' value='Signup'></form></p>"
.into();
html.into_response()
}
pub async fn post_signup(session: Session) -> impl IntoResponse {
let user = TestData {
name: "yo yo".to_string(),
..Default::default()
};
session.insert(&SIGNUP_KEY, user).await.unwrap();
session.save().await.unwrap();
Redirect::to("http://localhost:4001").into_response()
}
pub async fn payment_success(session: Session) -> impl IntoResponse {
session.load().await.unwrap();
dbg!("loaded the session");
let user = if let Some(user) = session.get::<TestData>(&SIGNUP_KEY).await.unwrap_or(None) {
dbg!("loaded test data");
user
} else {
dbg!("could not load data from session");
TestData::default()
};
let html: Html<_> = format!("<p>Check out this test data:</p><pre>{user:?}</pre>").into();
html.into_response()
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
struct TestData {
name: String,
login: String,
email: String,
password: String,
thingy: bool,
}