chiark / gitweb /
actix: experiments: Test program: CORS
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 26 Mar 2022 13:43:52 +0000 (13:43 +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>
Cargo.lock
daemon/Cargo.toml
daemon/actix-test.rs

index 68325fb6267287a66bb5cafcff9ce57b18bbeaad..b8da1555abc5054a35ffae242421ef1d8f69e08e 100644 (file)
@@ -19,6 +19,21 @@ dependencies = [
  "tokio-util 0.7.0",
 ]
 
+[[package]]
+name = "actix-cors"
+version = "0.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "414360eed71ba2d5435b185ba43ecbe281dfab5df3898286d6b7be8074372c92"
+dependencies = [
+ "actix-utils",
+ "actix-web",
+ "derive_more",
+ "futures-util",
+ "log 0.4.16",
+ "once_cell",
+ "smallvec",
+]
+
 [[package]]
 name = "actix-files"
 version = "0.6.0"
@@ -2892,6 +2907,7 @@ dependencies = [
 name = "otter-daemon"
 version = "0.7.3"
 dependencies = [
+ "actix-cors",
  "actix-files",
  "actix-web",
  "fehler",
index 9119531f9dbd62ad439ace053479e1010fb7edf9..9a1dbb74f4beb3e6441a7cccdedfe20c3137328c 100644 (file)
@@ -44,6 +44,7 @@ structopt="0.3"
 
 actix-web = "4"
 actix-files = "0.6"
+actix-cors = "0.6"
 
 rocket = { version="^0.4.6", features=["sse"] }
 rocket_contrib = { version="0.4", default-features=false, features=["tera_templates", "helmet", "json", "serve"] }
index a17d5c73a338eab2bd8e10ffac6cfb6732fecc24..27c71b66b60d99b05e89f7d7fa37b89293db73e5 100644 (file)
@@ -8,6 +8,7 @@ use actix_web::http::Method;
 use actix_web::HttpResponse;
 use actix_web::dev::Payload;
 use actix_web::middleware;
+use actix_cors::Cors;
 
 use std::convert::Infallible;
 
@@ -44,8 +45,15 @@ async fn wombat(remain: Remain) -> impl Responder {
     format!("Hello {:?}", remain)
 }
 
+fn update_cors() -> Cors {
+  Cors::default()
+      .allowed_methods([Method::GET])
+
+}
+
 //#[route("/wombat", method="GET", method="HEAD")]
-#[route("/foo", method="GET", method="HEAD")]
+#[route("/foo", method="GET", method="HEAD",
+        wrap = "update_cors()")]
 //#[get("/foo")]
 async fn foo() -> impl Responder {
   "foo\r\n"