cloudevents/binding/actix/mod.rs
1//! This module integrates the [cloudevents-sdk](https://docs.rs/cloudevents-sdk) with [Actix web](https://docs.rs/actix-web/) to easily send and receive CloudEvents.
2//!
3//! To deserialize an HTTP request as CloudEvent:
4//!
5//! ```
6//! use cloudevents::Event;
7//! use actix_web::post;
8//!
9//! #[post("/")]
10//! async fn post_event(event: Event) -> Result<String, actix_web::Error> {
11//! println!("Received Event: {:?}", event);
12//! Ok(format!("{:?}", event))
13//! }
14//! ```
15//!
16//! For more complex applications, access the Payload directly:
17//!
18//! ```
19//! use cloudevents::binding::actix::HttpRequestExt;
20//! use actix_web::{HttpRequest, web, post};
21//!
22//! #[post("/")]
23//! async fn post_event(req: HttpRequest, payload: web::Payload) -> Result<String, actix_web::Error> {
24//! let event = req.to_event(payload).await?;
25//! println!("Received Event: {:?}", event);
26//! Ok(format!("{:?}", event))
27//! }
28//! ```
29//!
30//! To serialize a CloudEvent to an HTTP response:
31//!
32//! ```
33//! use actix_web::get;
34//! use cloudevents::{Event, EventBuilderV10, EventBuilder};
35//! use serde_json::json;
36//!
37//! #[get("/")]
38//! async fn get_event() -> Event {
39//! let payload = json!({"hello": "world"});
40//!
41//! EventBuilderV10::new()
42//! .id("0001")
43//! .ty("example.test")
44//! .source("http://localhost/")
45//! .data("application/json", payload)
46//! .extension("someint", "10")
47//! .build()
48//! .unwrap()
49//! }
50//! ```
51//!
52//! For more complex applications, use the HTTP response builder extension:
53//!
54//! ```
55//! use cloudevents::binding::actix::HttpResponseBuilderExt;
56//! use actix_web::{get, HttpResponse};
57//! use cloudevents::{EventBuilderV10, EventBuilder};
58//! use serde_json::json;
59//!
60//! #[get("/")]
61//! async fn get_event() -> Result<HttpResponse, actix_web::Error> {
62//! HttpResponse::Ok()
63//! .event(
64//! EventBuilderV10::new()
65//! .id("0001")
66//! .ty("example.test")
67//! .source("http://localhost/")
68//! .data("application/json", json!({"hello": "world"}))
69//! .build()
70//! .expect("No error while building the event"),
71//! )
72//! }
73//! ```
74
75#![deny(rustdoc::broken_intra_doc_links)]
76
77mod server_request;
78mod server_response;
79
80pub use server_request::request_to_event;
81pub use server_request::HttpRequestExt;
82pub use server_response::event_to_response;
83pub use server_response::HttpResponseBuilderExt;