Skip to content

Instantly share code, notes, and snippets.

@aisrael
Created June 29, 2019 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aisrael/317016bdb598b918d11bf7c7e37961dd to your computer and use it in GitHub Desktop.
Save aisrael/317016bdb598b918d11bf7c7e37961dd to your computer and use it in GitHub Desktop.
Not working using latest hyper (0.12.31) and MakeService
use futures::future;
use hyper::rt::Future;
use hyper::service::{MakeService, Service};
use hyper::{Body, Request, Response, Server, StatusCode};
/// The service
pub struct TestService {}
impl Service for TestService {
type ReqBody = Body;
type ResBody = Body;
type Error = hyper::Error;
type Future = Box<Future<Item = Response<Body>, Error = hyper::Error> + Send>;
fn call(&mut self, _req: Request<Self::ReqBody>) -> Self::Future {
Box::new(future::ok(
Response::builder()
.status(StatusCode::OK)
.body("Ok".into())
.unwrap(),
))
}
}
/// The server
pub struct TestServer {}
impl MakeService<TestServer> for TestServer {
type ReqBody = Body;
type ResBody = Body;
type Error = hyper::Error;
type MakeError = hyper::Error;
type Future = Box<Future<Item = Self::Service, Error = Self::MakeError> + Send>;
type Service = TestService;
fn make_service(&mut self, _ctx: TestServer) -> Self::Future {
Box::new(future::ok(TestService {}))
}
}
impl TestServer {
pub fn new() -> TestServer {
TestServer {}
}
pub fn start(self) {
// This is our socket address...
let addr = ([127, 0, 0, 1], 4000).into();
let server = Server::bind(&addr)
// Line 55 gives us
// ```
// main.rs(51, 14): expected an `Fn<()>` closure, found `TestServer`
// ```
.serve(self)
.map_err(|e| eprintln!("server error: {}", e));
// Run this server for... forever!
hyper::rt::run(server);
}
}
fn main() {
let test = TestServer::new();
test.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment