From 2586322a68fbc99006d4e856e306369dd500f425 Mon Sep 17 00:00:00 2001 From: Francesco Guardiani Date: Mon, 18 May 2020 20:57:37 +0200 Subject: [PATCH] Added actix-web example (#38) Signed-off-by: Francesco Guardiani --- .gitignore | 4 +- Cargo.toml | 3 ++ example-projects/actix-web-example/Cargo.toml | 19 ++++++++ .../actix-web-example/src/main.rs | 45 +++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 example-projects/actix-web-example/Cargo.toml create mode 100644 example-projects/actix-web-example/src/main.rs diff --git a/.gitignore b/.gitignore index 9c1a063..a6f3b3e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/target +**/target .idea -Cargo.lock +**/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml index 6d7d15c..9d01ff4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,4 +35,7 @@ members = [ ".", "cloudevents-sdk-actix-web", "cloudevents-sdk-reqwest" +] +exclude = [ + "example-projects/actix-web-example" ] \ No newline at end of file diff --git a/example-projects/actix-web-example/Cargo.toml b/example-projects/actix-web-example/Cargo.toml new file mode 100644 index 0000000..628cd19 --- /dev/null +++ b/example-projects/actix-web-example/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "actix-web-example" +version = "0.1.0" +authors = ["Francesco Guardiani "] +edition = "2018" + +[dependencies] +cloudevents-sdk = { path = "../.." } +cloudevents-sdk-actix-web = { path = "../../cloudevents-sdk-actix-web" } +actix-web = "2" +actix-rt = "1" +lazy_static = "1.4.0" +bytes = "^0.5" +futures = "^0.3" +serde_json = "^1.0" +url = { version = "^2.1" } +env_logger = "0.7.1" + +[workspace] \ No newline at end of file diff --git a/example-projects/actix-web-example/src/main.rs b/example-projects/actix-web-example/src/main.rs new file mode 100644 index 0000000..c19475f --- /dev/null +++ b/example-projects/actix-web-example/src/main.rs @@ -0,0 +1,45 @@ +use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer}; +use cloudevents::EventBuilder; +use url::Url; +use std::str::FromStr; +use serde_json::json; + +#[post("/")] +async fn post_event(req: HttpRequest, payload: web::Payload) -> Result { + let event = cloudevents_sdk_actix_web::request_to_event(&req, payload).await?; + println!("Received Event: {:?}", event); + Ok(format!("{:?}", event)) +} + +#[get("/")] +async fn get_event() -> Result { + let payload = json!({"hello": "world"}); + + Ok(cloudevents_sdk_actix_web::event_to_response( + EventBuilder::new() + .id("0001") + .ty("example.test") + .source(Url::from_str("http://localhost/").unwrap()) + .data("application/json", payload) + .extension("someint", "10") + .build(), + HttpResponse::Ok() + ).await?) +} + +#[actix_rt::main] +async fn main() -> std::io::Result<()> { + std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info"); + env_logger::init(); + + HttpServer::new(|| { + App::new() + .wrap(actix_web::middleware::Logger::default()) + .service(post_event) + .service(get_event) + }) + .bind("127.0.0.1:8080")? + .workers(1) + .run() + .await +}