1

I want to write a simple microservice with hyper:

use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};
use hyper::{Method, StatusCode};
use std::{convert::Infallible, net::SocketAddr};

struct ApiServer {}

impl ApiServer {
    pub async fn route(&mut self, req: Request<Body>) -> Result<Response<Body>, Infallible> {
        let mut response = Response::new(Body::empty());
        *response.body_mut() = req.into_body();

        Ok(response)
    }
}

#[tokio::main]
async fn main() {
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    let mut api = ApiServer {};

    let make_svc = make_service_fn(|_conn| async {
        Ok::<_, Infallible>(service_fn(|req| async { api.route(req) }))
    });

    let server = Server::bind(&addr).serve(make_svc);

    if let Err(e) = server.await {
        eprintln!("server error: {}", e);
    }
}

I get compiler errors:

error[E0271]: type mismatch resolving `<impl std::future::Future as std::future::Future>::Output == std::result::Result<http::response::Response<_>, _>`
  --> src/master/bin/main.rs:46:38
   |
46 |     let server = Server::bind(&addr).serve(make_svc);
   |                                      ^^^^^ expected opaque type, found enum `std::result::Result`
   |

How do I rewrite the code to leave route function be a ApiServers member function?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
milo
  • 1,220
  • 3
  • 17
  • 33

1 Answers1

2

The Rust error message is reversed from what you might expect: serve() expects a Result but found an opaque type. You need to await the return value of api.route(req). Fixing other compile errors, the final code is:

use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};
use std::{convert::Infallible, net::SocketAddr};

#[derive(Clone, Copy)]
struct ApiServer {}

impl ApiServer {
    pub async fn route(&mut self, req: Request<Body>) -> Result<Response<Body>, Infallible> {
        let mut response = Response::new(Body::empty());
        *response.body_mut() = req.into_body();

        Ok(response)
    }
}

#[tokio::main]
async fn main() {
    let mut api = ApiServer {};

    let make_svc = make_service_fn(move |_conn| async move {
        Ok::<_, Infallible>(service_fn(move |req| async move { api.route(req).await }))
    });

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

    let server = Server::bind(&addr).serve(make_svc);

    if let Err(e) = server.await {
        eprintln!("server error: {}", e);
    }
}

See:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • Thanks, it's working now. But I can't comprehend though. `move |x| async move` syntax looks frightening... – milo Apr 29 '20 at 14:14
  • @milo My personal way is add move keyword when compiler tell you to add move keyword :p – Stargateur Apr 29 '20 at 14:28
  • And finally I got it! We pass closure with async block to `make_service_fn` and `service_fn` (not just closure and moving context into it). And since MakeServiceFn struct must be `Copy`able and `Clone`able I must make `ApiServer` `Copy`able and `Clone`able as well. Wuh, despite I used to be a C++ developer Rust makes me cry sometimes :) – milo Apr 30 '20 at 09:11