2017-12-21 00:12:43 +01:00
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_web;
|
|
|
|
extern crate futures;
|
|
|
|
extern crate env_logger;
|
|
|
|
extern crate serde_json;
|
|
|
|
#[macro_use] extern crate serde_derive;
|
|
|
|
|
|
|
|
use actix_web::*;
|
2017-12-21 05:30:54 +01:00
|
|
|
use futures::Future;
|
2017-12-21 00:12:43 +01:00
|
|
|
|
2017-12-21 01:05:07 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2017-12-21 00:12:43 +01:00
|
|
|
struct MyObj {
|
|
|
|
name: String,
|
|
|
|
number: i32,
|
|
|
|
}
|
|
|
|
|
2017-12-21 05:30:54 +01:00
|
|
|
fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
|
|
|
req.json().from_err()
|
|
|
|
.and_then(|val: MyObj| {
|
|
|
|
println!("model: {:?}", val);
|
|
|
|
Ok(httpcodes::HTTPOk.build().json(val)?) // <- send response
|
|
|
|
})
|
|
|
|
.responder()
|
2017-12-21 00:12:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|