Skip to content

Instantly share code, notes, and snippets.

@no-reply
Last active Jun 10, 2016
Embed
What would you like to do?
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