Created
December 18, 2016 23:56
-
-
Save ashleysommer/67d7b450d61a92303df786e701f226ec to your computer and use it in GitHub Desktop.
Hyper/tokio minimal example, Hello World, Boxed futures::Finished
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"; | |
let mut http_response = Response::new(); | |
http_response.set_body(response); | |
FutureResponse(futures::finished(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