Crate axum_standardwebhooks

Crate axum_standardwebhooks 

Source
Expand description

Integration of the standardwebhooks crate with the Axum web framework.

This crate provides an extractor for Axum that verifies webhook requests according to the Standard Webhooks specification.

§Example

use axum::{Router, routing::post, Json};
use axum_standardwebhooks::{StandardWebhook, SharedWebhook, Webhook};
use serde_json::Value;
use std::sync::Arc;
use axum::extract::FromRef;

async fn webhook_handler(StandardWebhook(Json(payload)): StandardWebhook<Json<Value>>) -> String {
    // The webhook signature has been verified, and we can safely use the payload
    format!("Received webhook: {}", payload)
}

#[derive(Clone)]
struct AppState {
    webhook: SharedWebhook,
}

impl FromRef<AppState> for SharedWebhook {
    fn from_ref(state: &AppState) -> Self {
        state.webhook.clone()
    }
}

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/webhooks", post(webhook_handler))
        .with_state(AppState {
            webhook: SharedWebhook::new(Webhook::new("whsec_C2FVsBQIhrscChlQIMV+b5sSYspob7oD").unwrap()),
        });

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

Structs§

SharedWebhook
A thread-safe wrapper around Webhook to make it shareable between Axum handlers.
StandardWebhook
An extractor that verifies a webhook request and extracts the inner payload.
Webhook

Enums§

StandardWebhookRejection
Represents the ways in which webhook verification and extraction can fail. Represents the ways in which webhook verification and extraction can fail.
WebhookError