-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df81543
commit 02b4cde
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// axum = "0.7.9" | ||
// svix = "1.42.0" | ||
// http-body-util = "0.1.2" | ||
|
||
use axum::{ | ||
body::Body, | ||
extract::Request, | ||
http, | ||
middleware::{self, Next}, | ||
response::{IntoResponse, Response}, | ||
routing::post, | ||
Router, | ||
}; | ||
|
||
use http::{HeaderMap, StatusCode}; | ||
|
||
use resend_rs::events::try_parse_event; | ||
use svix::webhooks::Webhook; | ||
|
||
#[allow(clippy::unwrap_used)] | ||
#[tokio::main] | ||
async fn main() { | ||
let app = Router::new() | ||
.route("/", post(handler)) | ||
.layer(middleware::from_fn(verify_middleware)); | ||
|
||
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") | ||
.await | ||
.unwrap(); | ||
|
||
println!("listening on {}", listener.local_addr().unwrap()); | ||
|
||
axum::serve(listener, app).await.unwrap(); | ||
} | ||
|
||
async fn handler(data: String) -> StatusCode { | ||
// Put your normal endpoint code here | ||
let event = try_parse_event(&data); | ||
println!("{event:?}"); | ||
|
||
StatusCode::OK | ||
} | ||
|
||
async fn verify_middleware( | ||
headers: HeaderMap, | ||
request: Request, | ||
next: Next, | ||
) -> Result<impl IntoResponse, Response> { | ||
let (parts, body) = request.into_parts(); | ||
|
||
let bytes = http_body_util::BodyExt::collect(body) | ||
.await | ||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())? | ||
.to_bytes(); | ||
|
||
let secret = "<YOUR RESEND SIGNING SECRET HERE>".to_string(); | ||
let wh = Webhook::new(&secret).expect("Invalid secret"); | ||
wh.verify(&bytes, &headers) | ||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())?; | ||
|
||
let request = Request::from_parts(parts, Body::from(bytes)); | ||
|
||
Ok(next.run(request).await) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters