Skip to content

Commit

Permalink
Merge pull request arlyon#509 from arlyon/fix/repair-axum
Browse files Browse the repository at this point in the history
Fix: Repaired the Axum example
  • Loading branch information
arlyon authored Feb 21, 2024
2 parents f09de3d + 6cdf50a commit 19f4551
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ rocket = { version = "0.4", optional = true }
async-std = { version = "1.10.0", features = ["attributes"] }
httpmock = "0.6.6"
tokio = { version = "1.24.1", features = ["rt", "macros"] }
axum = { version = "0.6.18", features = ["macros"] }
axum = { version = "0.7.4", features = ["macros"] }
async-trait = "0.1"
actix-web = "4.2.1"

Expand Down
23 changes: 11 additions & 12 deletions examples/webhook-axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
//! stripe trigger checkout.session.completed
//! ```
use std::net::SocketAddr;

use axum::{
async_trait,
body::Body,
extract::FromRequest,
http::{Request, StatusCode},
response::{IntoResponse, Response},
Expand All @@ -30,25 +30,24 @@ async fn main() {
// build our application with a route
let app = Router::new().route("/stripe_webhooks", post(handle_webhook));

// run our app with hyper
// `axum::Server` is a re-export of `hyper::Server`
let addr = SocketAddr::from(([127, 0, 0, 1], 4242));
println!("listening on {}", addr);
axum::Server::bind(&addr).serve(app.into_make_service()).await.unwrap();
let listener = tokio::net::TcpListener::bind("127.0.0.1:4242").await.unwrap();

println!("listening on {}", listener.local_addr().unwrap());

axum::serve(listener, app).await.unwrap();
}

struct StripeEvent(Event);

#[async_trait::async_trait]
impl<S, B> FromRequest<S, B> for StripeEvent
#[async_trait]
impl<S> FromRequest<S> for StripeEvent
where
String: FromRequest<S, B>,
B: Send + 'static,
String: FromRequest<S>,
S: Send + Sync,
{
type Rejection = Response;

async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
let signature = if let Some(sig) = req.headers().get("stripe-signature") {
sig.to_owned()
} else {
Expand Down

0 comments on commit 19f4551

Please sign in to comment.