Skip to content

Instantly share code, notes, and snippets.

@PetrGlad
Last active September 5, 2017 10:07
Show Gist options
  • Save PetrGlad/6e3acbf25b12b282539f102e73dd3bee to your computer and use it in GitHub Desktop.
Save PetrGlad/6e3acbf25b12b282539f102e73dd3bee to your computer and use it in GitHub Desktop.
Rust lab
[package]
name = "rustlab"
version = "0.1.0"
authors = ["Petr Gladkikh <[email protected]>"]
[dependencies]
hyper = "0.9.11"
json = "*"
extern crate hyper;
use std::io::{self, Read};
//use std::thread;
use hyper::server::{Server, Request, Response};
use hyper::status::StatusCode;
//use hyper::client::Client;
fn hello(mut req: Request, mut res: Response) {
match req.method {
hyper::Post => {
io::copy(&mut req, &mut res.start().unwrap()).unwrap();
},
hyper::Get => {
res.send("asdasdfasdf".as_bytes()).unwrap(); // why without unwrap it says "unused but must be used"
},
_ => *res.status_mut() = StatusCode::MethodNotAllowed
}
}
fn main() {
// thread::spawn(|| {
// println!("Getting Zalando");
// let client = Client::new();
// let mut res = client.get("http://zalando.de").send().unwrap();
// println!("res_status={}", res.status);
// let mut buffer = String::new();
// let body_size = res.read_to_string(&mut buffer).unwrap();
// println!("body length: {}", body_size);
// println!("body: {}", buffer);
// });
// let mut buffer = String::new();
// io::empty().read_to_string(&mut buffer).unwrap();
// assert!(buffer.is_empty());
println!("Starting server");
Server::http("0.0.0.0:10010").unwrap()
.handle(hello).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment