commit f324944d6bd08bd1193bc0c92e130c0c90ce0891
parent 7576e672f5cd45029cf0382777fca47a3e829ac1
Author: Charlie Stanton <charlie@shtanton.com>
Date: Wed, 5 Aug 2020 10:48:36 +0100
Adds actix web server
Diffstat:
3 files changed, 121 insertions(+), 2 deletions(-)
diff --git a/web-server/Cargo.lock b/web-server/Cargo.lock
@@ -35,6 +35,27 @@ dependencies = [
]
[[package]]
+name = "actix-files"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "193b22cb1f7b4ff12a4eb2415d6d19e47e44ea93e05930b30d05375ea29d3529"
+dependencies = [
+ "actix-http",
+ "actix-service",
+ "actix-web",
+ "bitflags",
+ "bytes",
+ "derive_more",
+ "futures-core",
+ "futures-util",
+ "log",
+ "mime",
+ "mime_guess",
+ "percent-encoding",
+ "v_htmlescape",
+]
+
+[[package]]
name = "actix-http"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -878,6 +899,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
+name = "mime_guess"
+version = "2.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212"
+dependencies = [
+ "mime",
+ "unicase",
+]
+
+[[package]]
name = "miniz_oxide"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -940,6 +971,16 @@ dependencies = [
]
[[package]]
+name = "nom"
+version = "4.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
+dependencies = [
+ "memchr",
+ "version_check 0.1.5",
+]
+
+[[package]]
name = "num-integer"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1427,6 +1468,15 @@ dependencies = [
]
[[package]]
+name = "unicase"
+version = "2.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
+dependencies = [
+ "version_check 0.9.2",
+]
+
+[[package]]
name = "unicode-bidi"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1468,6 +1518,49 @@ dependencies = [
]
[[package]]
+name = "v_escape"
+version = "0.7.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "660b101c07b5d0863deb9e7fb3138777e858d6d2a79f9e6049a27d1cc77c6da6"
+dependencies = [
+ "v_escape_derive",
+]
+
+[[package]]
+name = "v_escape_derive"
+version = "0.5.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c2ca2a14bc3fc5b64d188b087a7d3a927df87b152e941ccfbc66672e20c467ae"
+dependencies = [
+ "nom",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "v_htmlescape"
+version = "0.4.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e33e939c0d8cf047514fb6ba7d5aac78bc56677a6938b2ee67000b91f2e97e41"
+dependencies = [
+ "cfg-if",
+ "v_escape",
+]
+
+[[package]]
+name = "version_check"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
+
+[[package]]
+name = "version_check"
+version = "0.9.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
+
+[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1477,6 +1570,7 @@ checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
name = "web-server"
version = "0.1.0"
dependencies = [
+ "actix-files",
"actix-rt",
"actix-web",
]
diff --git a/web-server/Cargo.toml b/web-server/Cargo.toml
@@ -9,3 +9,4 @@ edition = "2018"
[dependencies]
actix-web = "2.0"
actix-rt = "1.0"
+actix-files = "0.2.0"
diff --git a/web-server/src/main.rs b/web-server/src/main.rs
@@ -1,3 +1,27 @@
-fn main() {
- println!("Hello, world!");
+use actix_web::{web, App, HttpRequest, HttpServer, Result};
+use actix_files::NamedFile;
+use std::path::PathBuf;
+
+struct AppState {
+ htmldir: String,
+}
+
+async fn file(req: HttpRequest, data: web::Data<AppState>) -> Result<NamedFile> {
+ let filename = req.match_info().query("filename");
+ let path: PathBuf = [data.htmldir.as_str(), filename].iter().collect();
+ Ok(NamedFile::open(path)?)
+}
+
+#[actix_rt::main]
+async fn main() -> std::io::Result<()> {
+ let args: Vec<_> = std::env::args().collect();
+ if args.len() < 3 {
+ println!("Usage: web-server htmldir port");
+ return Ok(());
+ }
+ let htmldir = args[1].to_string();
+ HttpServer::new(move || App::new().data(AppState {htmldir: htmldir.clone()}).route("/{filename:.*}", web::get().to(file)))
+ .bind(format!("127.0.0.1:{}", args[2]))?
+ .run()
+ .await
}