-
SummaryI give up on this, so time to ask for help... I've boiled my problem down to this; I can't make heads of the difference between into_make_service_with_connect_info (that works) and into_make_service as below: use axum::{handler::HandlerWithoutStateExt, http::Request, response::IntoResponse};
use hyper::body::Incoming;
use hyper_util::rt::{TokioExecutor, TokioIo};
use std::net::SocketAddr;
use tower_service::Service;
#[tokio::main]
async fn main() {
let addr = SocketAddr::from(([127, 0, 0, 1], 7878));
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
tracing::debug!("listening on {}", listener.local_addr().unwrap());
let (cnx, clnt_addr) = listener.accept().await.unwrap();
// this works
let mut make_service = handler.into_make_service_with_connect_info::<SocketAddr>();
let tower_service = make_service.call(clnt_addr).await.unwrap();
// this doesn't work
//let tower_service = handler.into_make_service();
let hyper_service = hyper::service::service_fn(move |request: Request<Incoming>| {
tower_service.clone().call(request)
});
let stream = TokioIo::new(cnx);
let _ = hyper_util::server::conn::auto::Builder::new(TokioExecutor::new())
.serve_connection(stream, hyper_service)
.await;
}
async fn handler() -> impl IntoResponse {
"ok"
} (I'm dong this because ultimately I have a connection I've already accepted at this point and just wanted to redirect from http to https, like https://github.com/daxpedda/axum-server-dual-protocol -- I was using something based on it with openssl also kludged in, but with the new version I thought it'd be easier to redo differently... and it all was smooth except this bit; I've got http and https working in much less code than before, just can't make the redirection without this, and it took me a couple of hours to realize it'd work with into_make_service_with_connect_info...) While I'm here, if there's a better way of handling just a single request (this seems a bit heavyweight...), I'll be happy to do it some other way; but I'd like to understand how this is supposed to work first. Thanks! axum version0.7.2 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Nevermind, into_make_service also needs to use
I'd swear I tried that too, but must have had another typo somewhere... I'll just say I find this all very confusing and errors not helping much when you're not familiar with tower types :/ If there's a better way to answer just one query please feel free to give an extra reply, otherwise I've at least got my original problem fixed so I'll mark this answered. |
Beta Was this translation helpful? Give feedback.
Nevermind, into_make_service also needs to use
call
a first time, it just doesn't use the argument so passing()
or anything else works.I'd swear I tried that too, but must have had another typo somewhere... I'll just say I find this all very confusing and errors not helping much when you're not familiar with tower types :/
If there's a better way to answer just one query please feel free to give an extra reply, otherwise I've at least got my original problem fixed so I'll mark this answered.