From: Ian Jackson Date: Sat, 19 Mar 2022 17:01:24 +0000 (+0000) Subject: actix: experiments: More test example program, with not found handler X-Git-Tag: otter-1.0.0~123 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=5e7150c2fa7822990c9961404c2c31b59a605259;p=otter.git actix: experiments: More test example program, with not found handler Partially C&P from Actix docs. Signed-off-by: Ian Jackson --- diff --git a/daemon/actix-test.rs b/daemon/actix-test.rs index 75f3747c..120cc730 100644 --- a/daemon/actix-test.rs +++ b/daemon/actix-test.rs @@ -2,10 +2,14 @@ use actix_web::{get, web, App, HttpServer, Responder}; use actix_web::FromRequest; use actix_web::HttpRequest; +use actix_web::http::Method; +use actix_web::HttpResponse; use actix_web::dev::Payload; use std::convert::Infallible; +//use otter::imports::*; + //use futures::Future; #[get("/{id}/{name}/index.html")] @@ -36,11 +40,22 @@ async fn wombat(remain: Remain) -> impl Responder { format!("Hello {:?}", remain) } +use fehler::throws; +#[throws(actix_web::Error)] +async fn not_found_handler(method: Method) -> impl Responder { + match method { + Method::GET => HttpResponse::NotFound().body("Not found.") + , + _ => HttpResponse::MethodNotAllowed().finish(), + } +} + #[actix_web::main] // or #[tokio::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| App::new() .service(wombat) .service(index) + .default_service(web::to(not_found_handler)) ) .bind(("127.0.0.1", 8080))? .run()