Add a basic HTTP server
This commit is contained in:
38
src/pot.rs
Normal file
38
src/pot.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
extern crate hyper;
|
||||
extern crate futures;
|
||||
|
||||
use self::futures::future::Future;
|
||||
|
||||
use self::hyper::header::ContentLength;
|
||||
use self::hyper::server::{Http, Request, Response, Service};
|
||||
|
||||
struct Pot;
|
||||
|
||||
const PHRASE: &'static str = "Hello, World!";
|
||||
|
||||
impl Service for Pot {
|
||||
// boilerplate hooking up hyper's server types
|
||||
type Request = Request;
|
||||
type Response = Response;
|
||||
type Error = hyper::Error;
|
||||
// The future representing the eventual Response your call will
|
||||
// resolve to. This can change to whatever Future you need.
|
||||
type Future = Box<Future<Item=Self::Response, Error=Self::Error>>;
|
||||
|
||||
fn call(&self, _req: Request) -> Self::Future {
|
||||
// We're currently ignoring the Request
|
||||
// And returning an 'ok' Future, which means it's ready
|
||||
// immediately, and build a Response with the 'PHRASE' body.
|
||||
Box::new(futures::future::ok(
|
||||
Response::new()
|
||||
.with_header(ContentLength(PHRASE.len() as u64))
|
||||
.with_body(PHRASE)
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn boil(addr: &str) {
|
||||
let sockaddr = addr.parse().unwrap();
|
||||
let server = Http::new().bind(&sockaddr, || Ok(Pot)).unwrap();
|
||||
server.run().unwrap();
|
||||
}
|
Reference in New Issue
Block a user