From fd93dc99014c301f7b4b9b669485a7c64d9f591a Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 26 Mar 2022 13:39:50 +0000 Subject: [PATCH] actix: experiments: More test example program Signed-off-by: Ian Jackson --- daemon/actix-test.rs | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/daemon/actix-test.rs b/daemon/actix-test.rs index 42022d1d..75f3747c 100644 --- a/daemon/actix-test.rs +++ b/daemon/actix-test.rs @@ -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, +} + +impl FromRequest for Remain { + type Future = futures::future::Ready>; + 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 } -- 2.30.2