From 1ddf4f2bf165ef951e5d05f62cfabdf45799324d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 27 Mar 2022 21:18:24 +0100 Subject: [PATCH] actix: experiments: Files test example program More C&P from Actix docs. Signed-off-by: Ian Jackson --- Cargo.lock | 36 +++++++++++++++++++++++++++++++++++ daemon/Cargo.toml | 5 +++++ daemon/actix-files-example.rs | 26 +++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 daemon/actix-files-example.rs diff --git a/Cargo.lock b/Cargo.lock index e989edf9..68325fb6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/daemon/Cargo.toml b/daemon/Cargo.toml index 154fbe1c..9119531f 100644 --- a/daemon/Cargo.toml +++ b/daemon/Cargo.toml @@ -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 index 00000000..0c78934c --- /dev/null +++ b/daemon/actix-files-example.rs @@ -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 +} -- 2.30.2