1 use futures_util::{FutureExt, StreamExt};
4 // GET /hello/warp => 200 OK with body "Hello, warp!"
5 async fn hello(name: String, agent: String) -> Result<impl warp::Reply, warp::Rejection> {
6 Ok(format!("Hello, {} from {}!", name, agent))
9 // websocat ws://127.0.0.1:3030/ws-echo
10 async fn ws_echo_connected(websocket: warp::ws::WebSocket) {
11 // echo all messages back
12 let (tx, rx) = websocket.split();
13 rx.forward(tx).map(|result| {
14 if let Err(e) = result {
15 log::warn!("websocket error: {:?}", e);
22 env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
24 let hello = warp::path!("hello" / String)
25 .and(warp::header::<String>("user-agent"))
28 let ws_echo = warp::path("ws-echo")
30 .map(|ws: warp::ws::Ws| {
31 ws.on_upgrade(|websocket| { ws_echo_connected(websocket) })
36 .with(warp::log("warp-server"));
39 .run(([127, 0, 0, 1], 3030))