]> piware.de Git - learn-rust.git/blobdiff - warp-server/src/main.rs
warp-server: Move handlers and filters into submodules
[learn-rust.git] / warp-server / src / main.rs
index a89c7a6fd205e4703d04ed7f38a9232799dbd69a..b23f2af1d39e2af95dc304b96264ed33dc9e457c 100644 (file)
@@ -1,13 +1,98 @@
-use warp::Filter;
+mod handlers {
+    use futures_util::{FutureExt, StreamExt, SinkExt};
 
-#[tokio::main]
-async fn main() {
     // 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));
+    pub async fn hello(name: String, agent: String) -> Result<impl warp::Reply, warp::Rejection> {
+        Ok(format!("Hello, {} from {}!", name, agent))
+    }
+
+    // websocat ws://127.0.0.1:3030/ws-echo
+    pub async fn ws_echo_connected(websocket: warp::ws::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);
+            }
+        }).await;
+    }
+
+    // websocat ws://127.0.0.1:3030/ws-rev
+    pub async fn ws_rev_connected(websocket: warp::ws::WebSocket) {
+        // echo all messages back
+        tokio::task::spawn(async {
+            let (mut tx, mut rx) = websocket.split();
+            while let Some(message) = rx.next().await {
+                let msg = match message {
+                    Ok(msg) =>  msg,
+                    Err(e) => {
+                        log::error!("websocket error: {}", e);
+                        break;
+                    }
+                };
+                log::info!("ws_rev_connected: got message: {:?}", msg);
+
+                if msg.is_close() {
+                    break;
+                }
+                if msg.is_text() {
+                    let text = msg.to_str().unwrap();
+                    let rev = text.chars().rev().collect::<String>();
+                    if let Err(e) = tx.send(warp::ws::Message::text(rev)).await {
+                        // disconnected
+                        log::info!("peer disconnected: {}", e);
+                        break;
+                    }
+                }
+                if msg.is_binary() {
+                    let mut rev = msg.into_bytes();
+                    rev.reverse();
+                    if let Err(e) = tx.send(warp::ws::Message::binary(rev)).await {
+                        // disconnected
+                        log::info!("peer disconnected: {}", e);
+                        break;
+                    }
+                }
+            }
+            log::info!("ws_rev_connected ended");
+        });
+    }
+}
+
+mod filters {
+    use warp::Filter;
+    use super::handlers;
 
-    warp::serve(hello)
+    pub fn hello() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
+        warp::path!("hello" / String)
+            .and(warp::header::<String>("user-agent"))
+            .and_then(handlers::hello)
+    }
+
+    pub fn ws_echo() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
+        warp::path("ws-echo")
+            .and(warp::ws())
+            .map(|ws: warp::ws::Ws| { ws.on_upgrade(handlers::ws_echo_connected) })
+    }
+
+    pub fn ws_rev() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
+        warp::path("ws-rev")
+            .and(warp::ws())
+            .map(|ws: warp::ws::Ws| { ws.on_upgrade(handlers::ws_rev_connected) })
+    }
+
+    pub fn api() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
+        hello()
+            .or(ws_echo())
+            .or(ws_rev())
+            .with(warp::log("warp-server"))
+    }
+}
+
+#[tokio::main]
+async fn main() {
+    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
+    warp::serve(filters::api())
         .run(([127, 0, 0, 1], 3030))
         .await;
 }