Signed-off-by: Jens Reimann <jreimann@redhat.com> |
||
---|---|---|
.. | ||
src | ||
tests | ||
Cargo.toml | ||
README.md |
README.md
CloudEvents SDK Rust - WARP

Integration of CloudEvents SDK with Warp - Web Server Framework.
Look at CloudEvents SDK README for more info.
Using this crate you can extract CloudEvent from requests and write CloudEvents to responses.
To echo events:
use cloudevents_sdk_warp::{filter, reply};
use warp::Filter;
#[tokio::main]
async fn main() {
let routes = warp::any()
// extracting event from request
.and(filter::to_event())
// returning event back
.map(|event| reply::from_event(event));
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}
Executing http
request:
curl -v \
-H "ce-specversion: 1.0" \
-H "ce-id: 2" \
-H "ce-type: example.event" \
-H "ce-source: url://example_response/" \
-H "content-type: application/json" \
-X POST -d '{ "age": 43, "name": "John Doe", "phones": ["+44 1234567","+44 2345678"] }' \
http://localhost:3030/
Should produce response similar to:
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3030 (#0)
> POST / HTTP/1.1
> Host: localhost:3030
> User-Agent: curl/7.64.1
> Accept: */*
> ce-specversion: 1.0
> ce-id: 2
> ce-type: example.event
> ce-source: url://example_response/
> content-type: application/json
> Content-Length: 74
>
* upload completely sent off: 74 out of 74 bytes
< HTTP/1.1 200 OK
< ce-specversion: 1.0
< ce-id: 2
< ce-type: example.event
< ce-source: url://example_response/
< content-type: application/json
< content-length: 74
< date: Mon, 02 Nov 2020 13:33:40 GMT
<
* Connection #0 to host localhost left intact
{ "age": 43, "name": "John Doe", "phones": ["+44 1234567","+44 2345678"] }
To create event inside request handlers and send them as responses:
#[tokio::main]
async fn main() {
let routes = warp::any().map(|| {
let event = EventBuilderV10::new()
.id("1")
.source(url::Url::parse("url://example_response/").unwrap())
.ty("example.ce")
.data(
mime::APPLICATION_JSON.to_string(),
json!({
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
}),
)
.build();
match event {
Ok(event) => Ok(reply::from_event(event)),
Err(e) => Ok(warp::reply::with_status(
e.to_string(),
StatusCode::INTERNAL_SERVER_ERROR,
)
.into_response()),
}
});
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}