event_notification/
producer.rs

1use crate::Error;
2use crate::Event;
3use axum::{Json, Router, routing::post};
4use tokio::sync::mpsc;
5
6/// Handles incoming events from the producer.
7pub async fn handle_event(
8    Json(event): Json<Event>,
9    tx: mpsc::Sender<Event>,
10) -> Result<(), axum::http::StatusCode> {
11    tx.send(event)
12        .await
13        .map_err(|_| axum::http::StatusCode::INTERNAL_SERVER_ERROR)?;
14    Ok(())
15}
16
17/// Starts the producer server.
18pub async fn start_producer(tx: mpsc::Sender<Event>) -> Result<(), Error> {
19    let app = Router::new().route("/event", post(|event| handle_event(event, tx)));
20    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
21    axum::serve(listener, app).await?;
22    Ok(())
23}