Last active
June 10, 2016 05:22
-
-
Save no-reply/d560920568d58cc428790a9d030a0063 to your computer and use it in GitHub Desktop.
Baby's First Rust Server
This file contains hidden or 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
[package] | |
name = "hello_server" | |
version = "0.1.0" | |
authors = ["Tom Johnson <[email protected]>"] | |
[dependencies] | |
hyper = "~0.9.7" | |
log = "*" | |
env_logger = "*" |
This file contains hidden or 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
#[macro_use] extern crate log; | |
extern crate env_logger; | |
extern crate hyper; | |
use hyper::server::{Server, Request, Response}; | |
use hyper::status::StatusCode; | |
use hyper::method::Method; | |
fn hello(req: Request, res: Response) { | |
match req.method { | |
Method::Head | Method::Options => { | |
info!("{}", req.method.to_string()); | |
res.send(b"").unwrap() | |
}, | |
Method::Get => { | |
info!("{} {} Hi!", res.status().to_string(), req.method.to_string()); | |
res.send(b"Hello World!").unwrap() | |
}, | |
_ => { | |
error!("{}", req.method.to_string()); | |
*res.status_mut() = StatusCode::MethodNotAllowed | |
}, | |
} | |
} | |
fn main() { | |
env_logger::init().unwrap(); | |
Server::http("0.0.0.0:2345").unwrap().handle(hello).unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment