Skip to content

Instantly share code, notes, and snippets.

@no-reply
Last active June 10, 2016 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save no-reply/d560920568d58cc428790a9d030a0063 to your computer and use it in GitHub Desktop.
Save no-reply/d560920568d58cc428790a9d030a0063 to your computer and use it in GitHub Desktop.
Baby's First Rust Server
[package]
name = "hello_server"
version = "0.1.0"
authors = ["Tom Johnson <n0reply@uw.edu>"]
[dependencies]
hyper = "~0.9.7"
log = "*"
env_logger = "*"
#[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