+use futures_util::{FutureExt, StreamExt};
use warp::Filter;
#[tokio::main]
async fn main() {
+ env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
+
// GET /hello/warp => 200 OK with body "Hello, warp!"
let hello = warp::path!("hello" / String)
.and(warp::header::<String>("user-agent"))
.map(|name, agent| format!("Hello, {} from {}!", name, agent));
- warp::serve(hello)
+ // websocat ws://127.0.0.1:3030/ws-echo
+ let echo = warp::path("ws-echo")
+ .and(warp::ws())
+ .map(|ws: warp::ws::Ws| {
+ ws.on_upgrade(|websocket| {
+ // echo all messages back
+ let (tx, rx) = websocket.split();
+ rx.forward(tx).map(|result| {
+ if let Err(e) = result {
+ log::warn!("websocket error: {:?}", e);
+ }
+ })
+ })
+ });
+
+ let api = hello
+ .or(echo)
+ .with(warp::log("warp-server"));
+
+ warp::serve(api)
.run(([127, 0, 0, 1], 3030))
.await;
}