Skip to content

Commit 2586322

Browse files
Added actix-web example (cloudevents#38)
Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 8133e54 commit 2586322

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/target
1+
**/target
22

33
.idea
4-
Cargo.lock
4+
**/Cargo.lock

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ members = [
3535
".",
3636
"cloudevents-sdk-actix-web",
3737
"cloudevents-sdk-reqwest"
38+
]
39+
exclude = [
40+
"example-projects/actix-web-example"
3841
]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "actix-web-example"
3+
version = "0.1.0"
4+
authors = ["Francesco Guardiani <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
cloudevents-sdk = { path = "../.." }
9+
cloudevents-sdk-actix-web = { path = "../../cloudevents-sdk-actix-web" }
10+
actix-web = "2"
11+
actix-rt = "1"
12+
lazy_static = "1.4.0"
13+
bytes = "^0.5"
14+
futures = "^0.3"
15+
serde_json = "^1.0"
16+
url = { version = "^2.1" }
17+
env_logger = "0.7.1"
18+
19+
[workspace]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer};
2+
use cloudevents::EventBuilder;
3+
use url::Url;
4+
use std::str::FromStr;
5+
use serde_json::json;
6+
7+
#[post("/")]
8+
async fn post_event(req: HttpRequest, payload: web::Payload) -> Result<String, actix_web::Error> {
9+
let event = cloudevents_sdk_actix_web::request_to_event(&req, payload).await?;
10+
println!("Received Event: {:?}", event);
11+
Ok(format!("{:?}", event))
12+
}
13+
14+
#[get("/")]
15+
async fn get_event() -> Result<HttpResponse, actix_web::Error> {
16+
let payload = json!({"hello": "world"});
17+
18+
Ok(cloudevents_sdk_actix_web::event_to_response(
19+
EventBuilder::new()
20+
.id("0001")
21+
.ty("example.test")
22+
.source(Url::from_str("http://localhost/").unwrap())
23+
.data("application/json", payload)
24+
.extension("someint", "10")
25+
.build(),
26+
HttpResponse::Ok()
27+
).await?)
28+
}
29+
30+
#[actix_rt::main]
31+
async fn main() -> std::io::Result<()> {
32+
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
33+
env_logger::init();
34+
35+
HttpServer::new(|| {
36+
App::new()
37+
.wrap(actix_web::middleware::Logger::default())
38+
.service(post_event)
39+
.service(get_event)
40+
})
41+
.bind("127.0.0.1:8080")?
42+
.workers(1)
43+
.run()
44+
.await
45+
}

0 commit comments

Comments
 (0)