update readme, add success and failure case instructions

This commit is contained in:
Joe Ardent 2024-03-04 13:18:42 -08:00
parent a9bd5ba201
commit a62b069373
3 changed files with 22 additions and 8 deletions

View file

@ -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"?

View file

@ -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::<TestData>(&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!("<p>Check out this test data:</p><pre>{user:?}</pre>").into();
html.into_response()
}

View file

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