From b773410c50243ca9105946251be3228879e64c12 Mon Sep 17 00:00:00 2001 From: monsterkrampe Date: Wed, 31 Aug 2022 15:59:05 +0200 Subject: [PATCH] Serve static files from './assets' directory --- Cargo.lock | 55 ++++++++++++++++++++++++++++++++++++++++++++++ server/Cargo.toml | 1 + server/src/main.rs | 29 ++++++++++++++---------- 3 files changed, 73 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f5c573a..2dc2ad3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,6 +34,29 @@ dependencies = [ "smallvec", ] +[[package]] +name = "actix-files" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d832782fac6ca7369a70c9ee9a20554623c5e51c76e190ad151780ebea1cf689" +dependencies = [ + "actix-http", + "actix-service", + "actix-utils", + "actix-web", + "askama_escape", + "bitflags", + "bytes", + "derive_more", + "futures-core", + "http-range", + "log", + "mime", + "mime_guess", + "percent-encoding", + "pin-project-lite", +] + [[package]] name = "actix-http" version = "3.3.1" @@ -220,6 +243,7 @@ name = "adf-bdd-server" version = "0.3.0" dependencies = [ "actix-cors", + "actix-files", "actix-web", "adf_bdd", "env_logger 0.9.3", @@ -313,6 +337,12 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70033777eb8b5124a81a1889416543dddef2de240019b674c81285a2635a7e1e" +[[package]] +name = "askama_escape" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" + [[package]] name = "assert_cmd" version = "2.0.10" @@ -878,6 +908,12 @@ dependencies = [ "itoa", ] +[[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.8.0" @@ -1071,6 +1107,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1730,6 +1776,15 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.13" diff --git a/server/Cargo.toml b/server/Cargo.toml index ddedc58..41ed9e0 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -15,6 +15,7 @@ description = "Offer Solving ADFs as a service" adf_bdd = { version="0.3.1", path="../lib", features = ["frontend"] } actix-web = "4" actix-cors = "0.6" +actix-files = "0.6" env_logger = "0.9" log = "0.4" serde = "1" diff --git a/server/src/main.rs b/server/src/main.rs index 3012fc4..31f4139 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,21 +1,17 @@ -use actix_web::{get, http, post, web, App, HttpResponse, HttpServer, Responder}; +use actix_files as fs; +use actix_web::{post, web, App, HttpServer, Responder}; use serde::{Deserialize, Serialize}; use std::collections::{HashMap, HashSet}; #[cfg(feature = "cors_for_local_development")] use actix_cors::Cors; +#[cfg(feature = "cors_for_local_development")] +use actix_web::http; use adf_bdd::adf::Adf; -use adf_bdd::datatypes::BddNode; use adf_bdd::datatypes::Var; use adf_bdd::parser::AdfParser; -#[get("/")] -async fn root() -> impl Responder { - // TODO: this should serve the static files for the react frontend - HttpResponse::Ok().body("Hello world!") -} - #[derive(Serialize)] // This is a DTO for the graph output struct DoubleLabeledGraph { @@ -170,15 +166,24 @@ async fn main() -> std::io::Result<()> { ]) .max_age(3600); - App::new().wrap(cors).service(root).service(solve) + App::new() + .wrap(cors) + .service(solve) + // this mus be last to not override anything + .service(fs::Files::new("/", "./assets").index_file("index.html")) }) - .bind(("127.0.0.1", 8080))? + .bind(("0.0.0.0", 8080))? .run() .await; #[cfg(not(feature = "cors_for_local_development"))] - let server = HttpServer::new(|| App::new().service(root).service(solve)) - .bind(("127.0.0.1", 8080))? + let server = HttpServer::new(|| { + App::new() + .service(solve) + // this mus be last to not override anything + .service(fs::Files::new("/", "./assets").index_file("index.html")) + }) + .bind(("0.0.0.0", 8080))? .run() .await;