Added actix-web example (#38)
Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
parent
8133e54dc0
commit
2586322a68
|
@ -1,4 +1,4 @@
|
||||||
/target
|
**/target
|
||||||
|
|
||||||
.idea
|
.idea
|
||||||
Cargo.lock
|
**/Cargo.lock
|
||||||
|
|
|
@ -36,3 +36,6 @@ members = [
|
||||||
"cloudevents-sdk-actix-web",
|
"cloudevents-sdk-actix-web",
|
||||||
"cloudevents-sdk-reqwest"
|
"cloudevents-sdk-reqwest"
|
||||||
]
|
]
|
||||||
|
exclude = [
|
||||||
|
"example-projects/actix-web-example"
|
||||||
|
]
|
|
@ -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]
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue