Skip to content

Commit 127d0ce

Browse files
committed
Update poem example
Signed-off-by: Sunli <[email protected]>
1 parent e16e966 commit 127d0ce

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

example-projects/poem-example/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ tokio = { version = "1.13", features = ["macros", "rt-multi-thread"] }
99
tracing = "0.1"
1010
poem = { version = "1.0.23" }
1111
tracing-subscriber = "0.2"
12+
serde_json = "1.0"
1213

1314
[dev-dependencies]
1415
chrono = { version = "0.4", features = ["serde"] }
15-
serde_json = "1.0"
16+

example-projects/poem-example/src/main.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
1-
use cloudevents::Event;
1+
use cloudevents::{Event, EventBuilder, EventBuilderV10};
2+
use poem::error::InternalServerError;
23
use poem::listener::TcpListener;
34
use poem::middleware::Tracing;
4-
use poem::{get, handler, Endpoint, EndpointExt, Response, Route, Server};
5+
use poem::{get, handler, Endpoint, EndpointExt, Response, Result, Route, Server};
6+
use serde_json::json;
57

68
#[handler]
7-
async fn index_get() -> &'static str {
8-
"hello from cloudevents server"
9+
async fn get_event() -> Result<Event> {
10+
let event = EventBuilderV10::new()
11+
.id("1")
12+
.source("url://example_response/")
13+
.ty("example.ce")
14+
.data(
15+
"application/json",
16+
json!({
17+
"name": "John Doe",
18+
"age": 43,
19+
"phones": [
20+
"+44 1234567",
21+
"+44 2345678"
22+
]
23+
}),
24+
)
25+
.build()
26+
.map_err(InternalServerError)?;
27+
Ok(event)
928
}
1029

1130
#[handler]
12-
async fn index_post(event: Event) -> Event {
31+
async fn post_event(event: Event) -> Event {
1332
tracing::debug!("received cloudevent {}", &event);
1433
event
1534
}
1635

1736
fn echo_app() -> impl Endpoint<Output = Response> {
1837
Route::new()
19-
.at("/", get(index_get).post(index_post))
38+
.at("/", get(get_event).post(post_event))
2039
.with(Tracing)
2140
}
2241

0 commit comments

Comments
 (0)