Skip to content

Instantly share code, notes, and snippets.

@ashleysommer
Created December 19, 2016 00:04
Show Gist options
  • Save ashleysommer/121391caff718d6103382c3c69972889 to your computer and use it in GitHub Desktop.
Save ashleysommer/121391caff718d6103382c3c69972889 to your computer and use it in GitHub Desktop.
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