I want to have one entry point on my server, that will redistribute connection to servers on the same machine. For that, I need to be able to redirect upgrade
request sent by the user.
So from user's perspective they connect to :3000
, but in reality it goes to :44405
.
I managed to do it in Node.js, where you can call handleUpgrade
on the server directly. However, I have no idea how to do this in warp.
This is my best attempt, using warp::redirect(). But this requires socket to allow for redirections, and additional bounce of 3XX from the server.
This code will redirect upgrade of :8080/test to :3000/socket on the same host as requested:
let test = warp::path("test")
.and(warp::ws())
.and(warp::header::<String>("host"))
.map(move |ws, host: String| {
let requested_host = Uri::from_str(&host).unwrap();
let host = requested_host.host().unwrap();
let redirection_addr = "ws://".to_owned() + host + ":3000/socket";
warp::redirect(Uri::from_str(&redirection_addr).unwrap())
});
rt.spawn(async move {
warp::serve(test).bind(([0, 0, 0, 0], 8080)).await;
});