From a62b069373eb3833e08cfd7879ab1167ac97b053 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 4 Mar 2024 13:18:42 -0800 Subject: [PATCH] update readme, add success and failure case instructions --- README.md | 14 +++++++++++--- src/bin/princess.rs | 14 ++++++++++---- src/bin/stripe.rs | 2 +- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2cfaace..5504fbb 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,18 @@ data upon initial return from Stripe, but if you manually enter the URL, it work # 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 +In order to demonstrate the issue, the "stripe" service must be run on a different origin than the +"princess" service. The following will set that up: + +`cargo run --bin=stripe & cargo run --bin=princess -- http://$(hostname):4001/` + +Now 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 `/`. +inserted in the initial get of `/`, or a message indicating failure. To see it succeed, run: + +`cargo run --bin=stripe & cargo run --bin=princess` + +and follow the buttons starting from http://localhost:4000/ again to the end. # Why is it called "princess"? diff --git a/src/bin/princess.rs b/src/bin/princess.rs index eb135be..d4927d4 100644 --- a/src/bin/princess.rs +++ b/src/bin/princess.rs @@ -51,19 +51,25 @@ pub async fn post_slash(session: Session) -> impl IntoResponse { session.insert(&SIGNUP_KEY, user).await.unwrap(); session.save().await.unwrap(); - Redirect::to("http://localhost:4001/").into_response() + let mut args = std::env::args(); + args.next(); + let stripe = args.next().unwrap_or("http://localhost:4001/".into()); + + Redirect::to(&stripe).into_response() } 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) { - dbg!("loaded test data"); user } else { - dbg!("could not load data from session"); TestData::default() }; + let user = if user == TestData::default() { + "Default TestData, session fetch failed.".to_string() + } else { + format!("Successful fetch from session: {user:?}") + }; let html: Html<_> = format!("

Check out this test data:

{user:?}
").into(); html.into_response() } diff --git a/src/bin/stripe.rs b/src/bin/stripe.rs index 4d9910e..2ab8a20 100644 --- a/src/bin/stripe.rs +++ b/src/bin/stripe.rs @@ -12,7 +12,7 @@ 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 addr = SocketAddr::from(([0, 0, 0, 0], 4001)); let listener = TcpListener::bind(&addr).await.unwrap(); axum::serve(listener, app).await.unwrap();