chiark / gitweb /
actix: experiments: Files test example program
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Mar 2022 20:18:24 +0000 (21:18 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Mar 2022 23:50:26 +0000 (00:50 +0100)
More C&P from Actix docs.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Cargo.lock
daemon/Cargo.toml
daemon/actix-files-example.rs [new file with mode: 0644]

index e989edf9b08c994c21a2dbbc74229cb2db4239cb..68325fb6267287a66bb5cafcff9ce57b18bbeaad 100644 (file)
@@ -19,6 +19,29 @@ dependencies = [
  "tokio-util 0.7.0",
 ]
 
+[[package]]
+name = "actix-files"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d81bde9a79336aa51ebed236e91fc1a0528ff67cfdf4f68ca4c61ede9fd26fb5"
+dependencies = [
+ "actix-http",
+ "actix-service",
+ "actix-utils",
+ "actix-web",
+ "askama_escape",
+ "bitflags",
+ "bytes",
+ "derive_more",
+ "futures-core",
+ "http-range",
+ "log 0.4.16",
+ "mime 0.3.16",
+ "mime_guess",
+ "percent-encoding 2.1.0",
+ "pin-project-lite",
+]
+
 [[package]]
 name = "actix-http"
 version = "3.0.2"
@@ -330,6 +353,12 @@ version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "bbf56136a5198c7b01a49e3afcbef6cf84597273d298f54432926024107b0109"
 
+[[package]]
+name = "askama_escape"
+version = "0.10.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341"
+
 [[package]]
 name = "async-trait"
 version = "0.1.52"
@@ -1835,6 +1864,12 @@ dependencies = [
  "pin-project-lite",
 ]
 
+[[package]]
+name = "http-range"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573"
+
 [[package]]
 name = "httparse"
 version = "1.6.0"
@@ -2857,6 +2892,7 @@ dependencies = [
 name = "otter-daemon"
 version = "0.7.3"
 dependencies = [
+ "actix-files",
  "actix-web",
  "fehler",
  "futures",
index 154fbe1c3320bc699ee41731b871ce36e160a631..9119531f9dbd62ad439ace053479e1010fb7edf9 100644 (file)
@@ -23,6 +23,10 @@ path="main.rs"
 name="actix-test"
 path="actix-test.rs"
 
+[[bin]]
+name="actix-files-example"
+path="actix-files-example.rs"
+
 
 [dependencies]
 
@@ -39,6 +43,7 @@ serde_with="1"
 structopt="0.3"
 
 actix-web = "4"
+actix-files = "0.6"
 
 rocket = { version="^0.4.6", features=["sse"] }
 rocket_contrib = { version="0.4", default-features=false, features=["tera_templates", "helmet", "json", "serve"] }
diff --git a/daemon/actix-files-example.rs b/daemon/actix-files-example.rs
new file mode 100644 (file)
index 0000000..0c78934
--- /dev/null
@@ -0,0 +1,26 @@
+use actix_files::Files;
+use actix_web::{middleware::Logger, App, HttpServer};
+use otter::imports::*;
+
+#[actix_web::main]
+async fn main() -> std::io::Result<()> {
+    env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
+
+    log::info!("starting HTTP server at http://localhost:8080");
+
+    HttpServer::new(|| {
+        App::new()
+            // We allow the visitor to see an index of the images at `/images`.
+            .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
+            // resolved in the order they are defined. If this would be placed before the `/images`
+            // path then the service for the static images would never be reached.
+            .service(Files::new("/", "./static/root/").index_file("index.html"))
+            // Enable the logger.
+            .wrap(Logger::default())
+    })
+    .bind(("127.0.0.1", 8080))?
+    .run()
+    .await
+}