sdk-rust/cloudevents-sdk-warp
Jens Reimann dfe2bcce13
Fix tests after refactoring URIRef (#116)
Signed-off-by: Jens Reimann <jreimann@redhat.com>
2021-04-21 11:09:24 +02:00
..
src Fix tests after refactoring URIRef (#116) 2021-04-21 11:09:24 +02:00
tests Add support for `seanmonstar/warp` web server framework (#97) 2020-11-04 12:12:55 +01:00
Cargo.toml Tokio 1.0 release bumps! (#113) 2021-02-08 16:39:11 +01:00
README.md Add support for `seanmonstar/warp` web server framework (#97) 2020-11-04 12:12:55 +01:00

README.md

CloudEvents SDK Rust - WARP Crates badge Docs badge

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;
}