From a9bd5ba201c74983c11ae76a1183b605798b7647 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 3 Mar 2024 16:20:33 -0800 Subject: [PATCH] add readme and stripe simulator --- README.md | 18 ++++++++++++++++++ src/{main.rs => bin/princess.rs} | 16 ++++++++-------- src/bin/stripe.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 README.md rename src/{main.rs => bin/princess.rs} (77%) create mode 100644 src/bin/stripe.rs diff --git a/README.md b/README.md new file mode 100644 index 0000000..2cfaace --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# What is this? + +I'm trying to replicate an issue I'm having with [Tower +Sessions](https://github.com/maxcountryman/tower-sessions), where the session seems to be missing +data upon initial return from Stripe, but if you manually enter the URL, it works. + +# How to run + +First, run `cargo run --bin=stripe &`; this will start the "Stripe" service listening on +localhost:4001. Then, run `cargo run --bin=princess` and visit http://localhost:4000/ and click the +buttons. At the end, you'll end up at a page at http://localhost:4000/success and see the test data +inserted in the initial get of `/`. + +# Why is it called "princess"? + +Because it's the baby version of [Queenie](https://git.kittenclause.com/nebkor/queen), a small +service using the same crates that sends users to Stripe and then receives a redirect from there +once a user has successfully paid. I just wanted to get a minimum test case. diff --git a/src/main.rs b/src/bin/princess.rs similarity index 77% rename from src/main.rs rename to src/bin/princess.rs index 53ceac0..eb135be 100644 --- a/src/main.rs +++ b/src/bin/princess.rs @@ -24,8 +24,8 @@ async fn main() { .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)) + .route("/", get(get_slash).post(post_slash)) + .route("/success", get(success)) .layer(session_layer) .into_make_service(); let addr = SocketAddr::from(([127, 0, 0, 1], 4000)); @@ -34,15 +34,15 @@ async fn main() { axum::serve(listener, app).await.unwrap(); } -pub async fn get_signup() -> impl IntoResponse { +pub async fn get_slash() -> impl IntoResponse { let html: Html<&str> = - "

-

" + "

Welcome to Princess

+

" .into(); html.into_response() } -pub async fn post_signup(session: Session) -> impl IntoResponse { +pub async fn post_slash(session: Session) -> impl IntoResponse { let user = TestData { name: "yo yo".to_string(), ..Default::default() @@ -51,10 +51,10 @@ pub async fn post_signup(session: Session) -> impl IntoResponse { session.insert(&SIGNUP_KEY, user).await.unwrap(); session.save().await.unwrap(); - Redirect::to("http://localhost:4001").into_response() + Redirect::to("http://localhost:4001/").into_response() } -pub async fn payment_success(session: Session) -> impl IntoResponse { +pub async fn success(session: Session) -> impl IntoResponse { session.load().await.unwrap(); dbg!("loaded the session"); let user = if let Some(user) = session.get::(&SIGNUP_KEY).await.unwrap_or(None) { diff --git a/src/bin/stripe.rs b/src/bin/stripe.rs new file mode 100644 index 0000000..4d9910e --- /dev/null +++ b/src/bin/stripe.rs @@ -0,0 +1,31 @@ +use std::net::SocketAddr; + +use axum::{ + response::{Html, IntoResponse, Redirect}, + routing::get, + Router, +}; +use tokio::net::TcpListener; + +#[tokio::main] +async fn main() { + let app = Router::new() + .route("/", get(get_slash).post(post_slash)) + .into_make_service(); + let addr = SocketAddr::from(([127, 0, 0, 1], 4001)); + let listener = TcpListener::bind(&addr).await.unwrap(); + + axum::serve(listener, app).await.unwrap(); +} + +pub async fn get_slash() -> impl IntoResponse { + let html: Html<&str> = + "

This is Stripe.

+

" + .into(); + html.into_response() +} + +pub async fn post_slash() -> impl IntoResponse { + Redirect::to("http://localhost:4000/success").into_response() +}