From db9509e509e80da739543bfc8b9cebaed2881b6e Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 19 Mar 2022 18:54:20 +0000 Subject: [PATCH] actix: experiments: Files test with not found handler Signed-off-by: Ian Jackson --- daemon/actix-files-example.rs | 7 +++++++ daemon/actix-test.rs | 18 +++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/daemon/actix-files-example.rs b/daemon/actix-files-example.rs index 0c78934c..d4daa1ed 100644 --- a/daemon/actix-files-example.rs +++ b/daemon/actix-files-example.rs @@ -1,7 +1,13 @@ use actix_files::Files; use actix_web::{middleware::Logger, App, HttpServer}; +use actix_web::{get, Responder, HttpResponse}; use otter::imports::*; +#[get("/wombat")] +async fn wombat() -> impl Responder { + HttpResponse::Ok().body("WOMBAT\n") +} + #[actix_web::main] async fn main() -> std::io::Result<()> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); @@ -11,6 +17,7 @@ async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() // We allow the visitor to see an index of the images at `/images`. + .service(wombat) .service(Files::new("/images", "static/images/").show_files_listing()) // Serve a tree of static files at the web root and specify the index file. // Note that the root path should always be defined as the last item. The paths are diff --git a/daemon/actix-test.rs b/daemon/actix-test.rs index 120cc730..9feb7118 100644 --- a/daemon/actix-test.rs +++ b/daemon/actix-test.rs @@ -1,5 +1,7 @@ -use actix_web::{get, web, App, HttpServer, Responder}; +#![allow(unused_imports)] + +use actix_web::{get, head, web, route, App, HttpServer, Responder}; use actix_web::FromRequest; use actix_web::HttpRequest; use actix_web::http::Method; @@ -35,16 +37,25 @@ impl FromRequest for Remain { } } +//#[route("/wombat", method="GET", method="HEAD")] #[get("/wombat")] async fn wombat(remain: Remain) -> impl Responder { format!("Hello {:?}", remain) } +//#[route("/wombat", method="GET", method="HEAD")] +#[route("/foo", method="GET", method="HEAD")] +//#[get("/foo")] +async fn foo() -> impl Responder { + "foo\r\n" +} + 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.") + Method::GET | Method::HEAD => HttpResponse::NotFound() + .content_type("text/plain; charset=utf-8") + .body("Not found.") , _ => HttpResponse::MethodNotAllowed().finish(), } @@ -55,6 +66,7 @@ async fn main() -> std::io::Result<()> { HttpServer::new(|| App::new() .service(wombat) .service(index) + .service(foo) .default_service(web::to(not_found_handler)) ) .bind(("127.0.0.1", 8080))? -- 2.30.2