Last active
August 29, 2015 14:24
-
-
Save AlecTaylor/1dad38de5f143247e75a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Combining: https://github.com/iron/body-parser/blob/master/examples/bodyparser.rs | |
// with: https://github.com/iron/router/blob/master/examples/simple.rs | |
extern crate iron; | |
extern crate router; | |
extern crate bodyparser; | |
extern crate persistent; | |
extern crate rustc_serialize; | |
use iron::prelude::*; | |
//use iron::{Iron, Request, Response, IronResult, Chain}; | |
use iron::status; | |
use router::{Router}; | |
//use persistent::Read; | |
#[derive(Debug, Clone, RustcDecodable)] | |
struct MyStructure { | |
a: String, | |
b: Option<String>, | |
} | |
//const MAX_BODY_LENGTH: usize = 1024 * 1024 * 10; | |
fn main() { | |
/* | |
let mut chain = Chain::new(log_body); | |
chain.link_before(Read::<bodyparser::MaxBodyLength>::one(MAX_BODY_LENGTH)); | |
*/ | |
let mut router = Router::new(); | |
router.get("/:query", | req: &mut Request | { | |
let body = req.get::<bodyparser::Raw>(); | |
match body { | |
Ok(Some(body)) => println!("Read body:\n{}", body), | |
Ok(None) => println!("No body"), | |
Err(err) => println!("Error: {:?}", err) | |
} | |
let json_body = req.get::<bodyparser::Json>(); | |
match json_body { | |
Ok(Some(json_body)) => println!("Parsed body:\n{}", json_body), | |
Ok(None) => println!("No body"), | |
Err(err) => println!("Error: {:?}", err) | |
} | |
let struct_body = req.get::<bodyparser::Struct<MyStructure>>(); | |
match struct_body { | |
Ok(Some(struct_body)) => println!("Parsed body:\n{:?}", struct_body), | |
Ok(None) => println!("No body"), | |
Err(err) => println!("Error: {:?}", err) | |
} | |
let ref query = req.extensions.get::<Router>() | |
.unwrap().find("query").unwrap_or("/"); | |
println!("Path = {}", query); | |
Ok(Response::with((status::Ok, *query))) | |
}); | |
let serve_on = "localhost:3000"; | |
println!("Starting server at {}", serve_on); | |
Iron::new(router).http(serve_on).unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment