add readme and stripe simulator

This commit is contained in:
Joe Ardent 2024-03-03 16:20:33 -08:00
parent f0a8840f29
commit a9bd5ba201
3 changed files with 57 additions and 8 deletions

18
README.md Normal file
View file

@ -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.

View file

@ -24,8 +24,8 @@ async fn main() {
.with_expiry(Expiry::OnInactivity(time::Duration::hours(2))); .with_expiry(Expiry::OnInactivity(time::Duration::hours(2)));
let app = Router::new() let app = Router::new()
.route("/signup", get(get_signup).post(post_signup)) .route("/", get(get_slash).post(post_slash))
.route("/payment_success/:receipt", get(payment_success)) .route("/success", get(success))
.layer(session_layer) .layer(session_layer)
.into_make_service(); .into_make_service();
let addr = SocketAddr::from(([127, 0, 0, 1], 4000)); let addr = SocketAddr::from(([127, 0, 0, 1], 4000));
@ -34,15 +34,15 @@ async fn main() {
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();
} }
pub async fn get_signup() -> impl IntoResponse { pub async fn get_slash() -> impl IntoResponse {
let html: Html<&str> = let html: Html<&str> =
"<p><form action='/signup' enctype='application/x-www-form-urlencoded' method='post'> "<p>Welcome to Princess</p><p><form action='/' enctype='application/x-www-form-urlencoded' method='post'>
<input type='submit' value='Signup'></form></p>" <input type='submit' value='go to stripe'></form></p>"
.into(); .into();
html.into_response() html.into_response()
} }
pub async fn post_signup(session: Session) -> impl IntoResponse { pub async fn post_slash(session: Session) -> impl IntoResponse {
let user = TestData { let user = TestData {
name: "yo yo".to_string(), name: "yo yo".to_string(),
..Default::default() ..Default::default()
@ -51,10 +51,10 @@ pub async fn post_signup(session: Session) -> impl IntoResponse {
session.insert(&SIGNUP_KEY, user).await.unwrap(); session.insert(&SIGNUP_KEY, user).await.unwrap();
session.save().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(); session.load().await.unwrap();
dbg!("loaded the session"); dbg!("loaded the session");
let user = if let Some(user) = session.get::<TestData>(&SIGNUP_KEY).await.unwrap_or(None) { let user = if let Some(user) = session.get::<TestData>(&SIGNUP_KEY).await.unwrap_or(None) {

31
src/bin/stripe.rs Normal file
View file

@ -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> =
"<p>This is Stripe.</p><p><form action='/' enctype='application/x-www-form-urlencoded' method='post'>
<input type='submit' value='back to princess'></form></p>"
.into();
html.into_response()
}
pub async fn post_slash() -> impl IntoResponse {
Redirect::to("http://localhost:4000/success").into_response()
}