43 lines
1.1 KiB
Rust
43 lines
1.1 KiB
Rust
use std::sync::Arc;
|
|
|
|
use axum::{extract::State, http::StatusCode, response::Json};
|
|
use serde::Deserialize;
|
|
use tracing::{error, info};
|
|
|
|
use cxcloud_common::event::Event;
|
|
|
|
use crate::AppState;
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct WebhookPayload {
|
|
#[serde(default = "default_type")]
|
|
pub r#type: String,
|
|
pub payload: serde_json::Value,
|
|
#[serde(default)]
|
|
pub metadata: std::collections::HashMap<String, String>,
|
|
}
|
|
|
|
fn default_type() -> String {
|
|
"webhook_event".to_string()
|
|
}
|
|
|
|
pub async fn webhook_handler(
|
|
State(state): State<Arc<AppState>>,
|
|
Json(body): Json<WebhookPayload>,
|
|
) -> Result<Json<serde_json::Value>, StatusCode> {
|
|
let mut event = Event::new("webhook", &body.r#type, body.payload);
|
|
event.metadata = body.metadata;
|
|
|
|
info!(event_id = %event.id, event_type = %event.r#type, "Received webhook event");
|
|
|
|
state.event_tx.send(event.clone()).await.map_err(|e| {
|
|
error!(error = %e, "Failed to send event to normalizer");
|
|
StatusCode::INTERNAL_SERVER_ERROR
|
|
})?;
|
|
|
|
Ok(Json(serde_json::json!({
|
|
"id": event.id,
|
|
"status": "accepted",
|
|
})))
|
|
}
|