1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 17:52:40 +01:00
actix-extras/examples/json/src/main.rs

42 lines
1.0 KiB
Rust
Raw Normal View History

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();
}