iron/iron.github.io — pull request #8 by uniphil (2014-08-03 22:07:30+00:00)
Title: Make some of the examples work

I am a complete rust noob, so these changes probably suck, but I wanted to play with iron and most of the code examples are broken, which seems like an issue. I took at shot at making them work.

I wanted to update the one that uses BodyParser too, and got as far as this, by hacking at the (also broken) example from that crate, and got as far as this:
```rust
extern crate http;
extern crate iron;
extern crate bodyparser;
use iron::{Iron, Server, Chain, Request, Response, Alloy, FromFn, Status, Unwind};
use bodyparser::{BodyParser, Parsed};

fn log_json(_: &mut Request, _: &mut Response, alloy: &mut Alloy) -> Status {
let json = alloy.find::();
match json {
Some(&Parsed(ref parsed)) => println!("{}", parsed),
None => (),
}
Unwind
}

fn main() {
let mut server: Server = Iron::new();
server.chain.link(BodyParser::new()); // Add middleware to the server's stack
server.chain.link(FromFn::new(log_json));
server.listen(::std::io::net::ip::Ipv4Addr(127, 0, 0, 1), 3000);
}
```

However, I really wanted to make that code to something more like
```rust
fn log_json(_: &mut Request, res: &mut Response, alloy: &mut Alloy) -> Status {
let json = alloy.find::();
match json {
Some(&Parsed(ref parsed)) => res.serve(::http::status::Ok, format!("hello, {}", parsed.name)),
None => res.serve(::http::status::BadRequest, format!("blah...")),
}
Unwind
}
```
that took me down a road of learning about `serialize::json` which is still over my head and I can't figure out how to get `parsed` into my struct and... But an example that showed how to get access to the properties of the json object would let me start building useful things much more quickly. Probably an issue for iron/bodyparser I guess.