chiark / gitweb /
actix: experiments: Files test with not found handler
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 19 Mar 2022 18:54:20 +0000 (18:54 +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-files-example.rs
daemon/actix-test.rs

index 0c78934cdef307d4fd3194be00206e64c0b5fbf4..d4daa1ede3cb96e5875ecc56b034cce97b7dd1d3 100644 (file)
@@ -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
index 120cc7300e740318bd3d5d386bce015b997ff232..9feb7118226184d2905d3703832d53733dfe54f9 100644 (file)
@@ -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))?