Added actix-web example (#38)

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
Francesco Guardiani 2020-05-18 20:57:37 +02:00 committed by GitHub
parent 8133e54dc0
commit 2586322a68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 2 deletions

4
.gitignore vendored
View File

@ -1,4 +1,4 @@
/target
**/target
.idea
Cargo.lock
**/Cargo.lock

View File

@ -36,3 +36,6 @@ members = [
"cloudevents-sdk-actix-web",
"cloudevents-sdk-reqwest"
]
exclude = [
"example-projects/actix-web-example"
]

View File

@ -0,0 +1,19 @@
[package]
name = "actix-web-example"
version = "0.1.0"
authors = ["Francesco Guardiani <francescoguard@gmail.com>"]
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]

View File

@ -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<String, actix_web::Error> {
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<HttpResponse, actix_web::Error> {
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
}