programatik29 / axum-server

High level server designed to be used with axum framework.
MIT License
174 stars 60 forks source link

`axum::extract::ConnectInfo` is not supported #12

Closed avalon1610 closed 3 years ago

avalon1610 commented 3 years ago

for use axum::extract::ConnectInfo, we need into_make_service_with_connect_info.

axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
    .serve(
        app.into_make_service_with_connect_info::<SocketAddr, _>()
    )
    .await
    .expect("server failed");

which is not supported in axum-server for now.

use axum::{
    handler::get,
    Router,
};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(|| async { "Hello, World!" }));

    axum_server::bind("127.0.0.1:3000")
        .serve(app.into_make_service_with_connect_info::<SocketAddr, _>())
        .await
        .unwrap();
}

we got

error[E0271]: type mismatch resolving `<IntoMakeServiceWithConnectInfo<Route<axum::handler::OnMethod<[closure@src\main.rs:10:44: 10:72], _, (), EmptyRouter>, EmptyRouter<_>>, SocketAddr> as tower_service::Service<Request<axum::body::Body>>>::Response == Response<_>`
  --> src\main.rs:13:10
   |
10 |     let app = Router::new().route("/", get(|| async { "Hello, World!" }));
   |                                            ----------------------------
   |                                            |        |
   |                                            |        the expected `async` block
   |                                            the expected closure
...
13 |         .serve(app.into_make_service_with_connect_info::<SocketAddr, _>())
   |          ^^^^^ expected struct `AddExtension`, found struct `Response`
   | 
  ::: C:\Users\25386\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\future\mod.rs:61:43
   |
61 | pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
   |                                           ------------------------------- the expected opaque type
   |
   = note: expected struct `AddExtension<Route<axum::handler::OnMethod<[closure@src\main.rs:10:44: 10:72], _, (), EmptyRouter>, EmptyRouter<_>>, ConnectInfo<_>>`
              found struct `Response<_>`

error[E0277]: the trait bound `IntoMakeServiceWithConnectInfo<Route<axum::handler::OnMethod<[closure@src\main.rs:10:44: 10:72], _, (), EmptyRouter>, EmptyRouter<_>>, SocketAddr>: Clone` is not satisfied
  --> src\main.rs:13:16
   |
13 |         .serve(app.into_make_service_with_connect_info::<SocketAddr, _>())
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `IntoMakeServiceWithConnectInfo<Route<axum::handler::OnMethod<[closure@src\main.rs:10:44: 10:72], _, (), EmptyRouter>, EmptyRouter<_>>, SocketAddr>`

error[E0277]: the trait bound `SocketAddr: Connected<Request<axum::body::Body>>` is not satisfied
  --> src\main.rs:13:16
   |
13 |         .serve(app.into_make_service_with_connect_info::<SocketAddr, _>())
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Connected<Request<axum::body::Body>>` is not implemented for `SocketAddr`
   |
   = help: the following implementations were found:
             <SocketAddr as Connected<&hyper::server::tcp::addr_stream::AddrStream>>
   = note: required because of the requirements on the impl of `tower_service::Service<Request<axum::body::Body>>` for `IntoMakeServiceWithConnectInfo<Route<axum::handler::OnMethod<[closure@src\main.rs:10:44: 10:72], _, (), EmptyRouter>, EmptyRouter<_>>, SocketAddr>`

error: aborting due to 3 previous errors
programatik29 commented 3 years ago

MakeService is not required to serve services. SocketAddr is added by Request extensions so you can get it like:

use axum::{extract::Extension, handler::get, Router};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(handler));

    axum_server::bind("127.0.0.1:3000")
        .serve(app)
        .await
        .unwrap();
}

async fn handler(Extension(addr): Extension<SocketAddr>) -> String {
    format!("addr: {}", addr)
}

I should probably document it better.

programatik29 commented 3 years ago

Added this example.

3tieto commented 9 months ago

why I get error "500 : Missing request extension: Extension of type core::net::socket_addr::SocketAddr was not found. Perhaps you forgot to add it? See axum::Extension."

programatik29 commented 9 months ago

@3tieto The example you are looking is from 0.2 version of this crate. Check this up to date example.