2017-12-21 00:12:43 +01:00
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_web;
|
|
|
|
extern crate bytes;
|
|
|
|
extern crate futures;
|
|
|
|
extern crate env_logger;
|
|
|
|
extern crate serde_json;
|
|
|
|
#[macro_use] extern crate serde_derive;
|
|
|
|
|
|
|
|
use actix_web::*;
|
|
|
|
use bytes::BytesMut;
|
|
|
|
use futures::Stream;
|
2017-12-21 00:45:26 +01:00
|
|
|
use futures::future::{Future, ok, err};
|
2017-12-21 00:12:43 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct MyObj {
|
|
|
|
name: String,
|
|
|
|
number: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index(mut req: HttpRequest) -> Result<Box<Future<Item=HttpResponse, Error=Error>>> {
|
2017-12-21 00:45:26 +01:00
|
|
|
// check content-type
|
2017-12-21 00:12:43 +01:00
|
|
|
if req.content_type() != "application/json" {
|
|
|
|
return Err(error::ErrorBadRequest("wrong content-type").into())
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Box::new(
|
2017-12-21 00:45:26 +01:00
|
|
|
req.payload_mut() // <- load request body
|
2017-12-21 00:12:43 +01:00
|
|
|
.readany()
|
|
|
|
.fold(BytesMut::new(), |mut body, chunk| {
|
|
|
|
body.extend(chunk);
|
2017-12-21 00:45:26 +01:00
|
|
|
ok::<_, error::PayloadError>(body)
|
2017-12-21 00:12:43 +01:00
|
|
|
})
|
|
|
|
.map_err(|e| Error::from(e))
|
2017-12-21 00:45:26 +01:00
|
|
|
.and_then(|body| { // <- body is loaded, now we can deserialize json
|
2017-12-21 00:12:43 +01:00
|
|
|
match serde_json::from_slice::<MyObj>(&body) {
|
|
|
|
Ok(obj) => {
|
2017-12-21 00:45:26 +01:00
|
|
|
println!("model: {:?}", obj); // <- do something with payload
|
2017-12-21 00:12:43 +01:00
|
|
|
ok(httpcodes::HTTPOk.response()) // <- send response
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
err(error::ErrorBadRequest(e).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
|
|
|
let _ = env_logger::init();
|
|
|
|
let sys = actix::System::new("json-example");
|
|
|
|
|
|
|
|
HttpServer::new(|| {
|
|
|
|
Application::new()
|
|
|
|
// enable logger
|
|
|
|
.middleware(middlewares::Logger::default())
|
|
|
|
.resource("/", |r| r.method(Method::POST).f(index))})
|
|
|
|
.bind("127.0.0.1:8080").unwrap()
|
|
|
|
.start();
|
|
|
|
|
|
|
|
println!("Started http server: 127.0.0.1:8080");
|
|
|
|
let _ = sys.run();
|
|
|
|
}
|