chiark / gitweb /
actix: experiments: More test example program
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 26 Mar 2022 13:39:50 +0000 (13:39 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Mar 2022 23:50:26 +0000 (00:50 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
daemon/actix-test.rs

index 42022d1df87a4ac0bd1882c24c33b9b7798f9159..75f3747cd84d0601e1da7fbb675b56463fae50e9 100644 (file)
@@ -1,5 +1,12 @@
 
 use actix_web::{get, web, App, HttpServer, Responder};
+use actix_web::FromRequest;
+use actix_web::HttpRequest;
+use actix_web::dev::Payload;
+
+use std::convert::Infallible;
+
+//use futures::Future;
 
 #[get("/{id}/{name}/index.html")]
 async fn index(params: web::Path<(u32, String)>) -> impl Responder {
@@ -7,10 +14,35 @@ async fn index(params: web::Path<(u32, String)>) -> impl Responder {
     format!("Hello {}! id:{}", name, id)
 }
 
+#[derive(Debug, Default)]
+struct Remain {
+  #[allow(dead_code)]
+  q: Option<String>,
+}
+
+impl FromRequest for Remain {
+  type Future = futures::future::Ready<Result<Remain, Infallible>>;
+  type Error = Infallible;
+  fn from_request(req: &HttpRequest, _: &mut Payload)
+                  -> Self::Future {
+    let q = req.uri().query().map(ToOwned::to_owned);
+    let r = Remain { q };
+    futures::future::ready(Ok(r))
+  }
+}
+
+#[get("/wombat")]
+async fn wombat(remain: Remain) -> impl Responder {
+    format!("Hello {:?}", remain)
+}
+
 #[actix_web::main] // or #[tokio::main]
 async fn main() -> std::io::Result<()> {
-    HttpServer::new(|| App::new().service(index))
-        .bind(("127.0.0.1", 8080))?
-        .run()
-        .await
+  HttpServer::new(|| App::new()
+                  .service(wombat)
+                  .service(index)
+  )
+    .bind(("127.0.0.1", 8080))?
+    .run()
+    .await
 }