Created
December 19, 2016 00:04
-
-
Save ashleysommer/121391caff718d6103382c3c69972889 to your computer and use it in GitHub Desktop.
This file contains 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
use hyper::server::{self, Request, Response, Server, Service}; | |
use hyper::Error as HyperError; | |
use futures::{Poll, Future}; | |
use std::net::{Ipv4Addr,SocketAddr,SocketAddrV4}; | |
extern crate hyper; | |
extern crate futures; | |
pub struct FutureResponse(Box<Future<Item=Response, Error=HyperError>>); | |
impl Future for FutureResponse { | |
type Item = Response; | |
type Error = HyperError; | |
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { | |
self.0.poll() | |
} | |
} | |
pub struct TestService; | |
impl Service for TestService { | |
type Request = Request; | |
type Response = Response; | |
type Error = HyperError; | |
type Future = FutureResponse; | |
fn call(&self, request: Request) -> Self::Future { | |
let response = "Hello World"; | |
FutureResponse(futures::lazy(move || -> Result<Response, HyperError> { | |
let mut http_response = Response::new(); | |
http_response.set_body(response); | |
Ok(http_response) | |
}).boxed()) | |
} | |
} | |
fn main() { | |
let socket = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 3000)); | |
let mut server = Server::http(&socket).unwrap(); | |
let (listening, server_loop) = server.standalone(|| { | |
Ok(TestService) | |
}).unwrap(); | |
println!("Listening on http://{}", listening); | |
server_loop.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment