Я хочу написать простой микросервис с гиперссылкой:
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);
}
}
Я получаю ошибки компилятора:
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`
|
Как переписать код, чтобы функция route
была ApiServer
s функция-член?