chiark / gitweb /
actix: experiments: More test example program, with not found handler
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 19 Mar 2022 17:01:24 +0000 (17:01 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Mar 2022 23:50:26 +0000 (00:50 +0100)
Partially C&P from Actix docs.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
daemon/actix-test.rs

index 75f3747cd84d0601e1da7fbb675b56463fae50e9..120cc7300e740318bd3d5d386bce015b997ff232 100644 (file)
@@ -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()