From e248f3b983cd2968206ead48fbc2c9a986ca5e91 Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Tue, 14 Jan 2020 01:33:14 -0800 Subject: [PATCH] Serve web and swagger static files --- Cargo.lock | 63 ++++++++++++++++++++++++++++++ Cargo.toml | 4 +- src/main.rs | 10 ++--- src/service/actix/mod.rs | 16 ++++++++ src/service/actix/server.rs | 34 +++++++++------- src/service/actix/tests/mod.rs | 20 ++++++++++ src/service/actix/tests/swagger.rs | 21 ++++++++++ src/service/actix/tests/web.rs | 12 ++++++ src/service/rocket/server.rs | 20 +++++----- 9 files changed, 169 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 76cadb3..bf17dae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,25 @@ dependencies = [ "trust-dns-resolver 0.18.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "actix-files" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "actix-http 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-service 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "derive_more 0.99.2 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "mime 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "mime_guess 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "v_htmlescape 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "actix-http" version = "1.0.1" @@ -1720,6 +1739,15 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "nom" +version = "4.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "notify" version = "4.0.14" @@ -2017,6 +2045,8 @@ dependencies = [ name = "polaris" version = "0.11.0" dependencies = [ + "actix-files 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-http 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "actix-rt 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "actix-web 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "anyhow 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3229,6 +3259,34 @@ name = "uuid" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "v_escape" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "v_escape_derive 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "v_escape_derive" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "v_htmlescape" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "v_escape 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "vcpkg" version = "0.2.8" @@ -3363,6 +3421,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum actix-codec 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "09e55f0a5c2ca15795035d90c46bd0e73a5123b72f68f12596d6ba5282051380" "checksum actix-connect 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2b61480a8d30c94d5c883d79ef026b02ad6809931b0a4bb703f9545cd8c986" +"checksum actix-files 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "301482841d3d74483a446ead63cb7d362e187d2c8b603f13d91995621ea53c46" "checksum actix-http 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c16664cc4fdea8030837ad5a845eb231fb93fc3c5c171edfefb52fad92ce9019" "checksum actix-macros 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "21705adc76bbe4bc98434890e73a89cd00c6015e5704a60bb6eea6c3b72316b6" "checksum actix-router 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "9d7a10ca4d94e8c8e7a87c5173aba1b97ba9a6563ca02b0e1cd23531093d3ec8" @@ -3543,6 +3602,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum mp3-duration 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9b7cc3eebd1d49a86505733254d1dfd53fbefe4ab65d86afd6f84d3245cd604f" "checksum native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4b2df1a4c22fd44a62147fd8f13dd0f95c9d8ca7b2610299b2a2f9cf8964274e" "checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" +"checksum nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6" "checksum notify 4.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "199628fc33b21bc767baa057490b00b382ecbae030803a7b36292422d15b778b" "checksum num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cf4825417e1e1406b3782a8ce92f4d53f26ec055e3622e1881ca8e9f5f9e08db" "checksum num-bigint 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f9c3f34cdd24f334cb265d9bf8bfa8a241920d026916785747a92f0e55541a1a" @@ -3701,6 +3761,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "75b414f6c464c879d7f9babf951f23bc3743fb7313c081b2e6ca719067ea9d61" "checksum uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)" = "90dbc611eb48397705a6b0f6e917da23ae517e4d127123d2cf7674206627d32a" "checksum uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" +"checksum v_escape 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)" = "660b101c07b5d0863deb9e7fb3138777e858d6d2a79f9e6049a27d1cc77c6da6" +"checksum v_escape_derive 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c2ca2a14bc3fc5b64d188b087a7d3a927df87b152e941ccfbc66672e20c467ae" +"checksum v_htmlescape 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e33e939c0d8cf047514fb6ba7d5aac78bc56677a6938b2ee67000b91f2e97e41" "checksum vcpkg 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3fc439f2794e98976c88a2a2dafce96b930fe8010b0a256b3c2199a773933168" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" "checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" diff --git a/Cargo.toml b/Cargo.toml index 8d02554..0d36348 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,12 @@ edition = "2018" default = ["service-actix"] ui = [] profile-index = ["flame", "flamer"] -service-actix = ["actix-rt", "actix-web"] +service-actix = ["actix-files", "actix-http", "actix-rt", "actix-web"] service-rocket = ["rocket", "rocket_contrib"] [dependencies] +actix-files = { version = "0.2", optional = true } +actix-http = { version = "1.0", optional = true } actix-web = { version = "2.0", optional = true } actix-rt = { version = "1.0", optional = true } anyhow = "1.0" diff --git a/src/main.rs b/src/main.rs index 9689ec2..88f1a62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -223,11 +223,11 @@ fn main() -> Result<()> { service::server::run( port, Some(auth_secret.as_slice()), - &api_url, - &web_url, - &web_dir_path, - &swagger_url, - &swagger_dir_path, + api_url, + web_url, + web_dir_path, + swagger_url, + swagger_dir_path, db.clone(), command_sender, )?; diff --git a/src/service/actix/mod.rs b/src/service/actix/mod.rs index 6f99944..39540f4 100644 --- a/src/service/actix/mod.rs +++ b/src/service/actix/mod.rs @@ -1,4 +1,20 @@ +use actix_files as fs; +use actix_web::web; +use std::path::Path; + pub mod server; #[cfg(test)] mod tests; + +fn configure_app( + cfg: &mut web::ServiceConfig, + web_url: &str, + web_dir_path: &Path, + swagger_url: &str, + swagger_dir_path: &Path, +) { + // TODO logging + cfg.service(fs::Files::new(swagger_url, swagger_dir_path).index_file("index.html")) + .service(fs::Files::new(web_url, web_dir_path).index_file("index.html")); +} diff --git a/src/service/actix/server.rs b/src/service/actix/server.rs index dc393b6..9b82631 100644 --- a/src/service/actix/server.rs +++ b/src/service/actix/server.rs @@ -1,4 +1,4 @@ -use actix_web::{web, App, HttpResponse, HttpServer, Responder}; +use actix_web::{App, HttpServer}; use anyhow::*; use std::path::PathBuf; use std::sync::Arc; @@ -6,27 +6,31 @@ use std::sync::Arc; use crate::db::DB; use crate::index::CommandSender; -async fn index() -> impl Responder { - HttpResponse::Ok().body("hello world!") -} - #[actix_rt::main] pub async fn run( port: u16, auth_secret: Option<&[u8]>, - api_url: &str, - web_url: &str, - web_dir_path: &PathBuf, - swagger_url: &str, - swagger_dir_path: &PathBuf, + api_url: String, + web_url: String, + web_dir_path: PathBuf, + swagger_url: String, + swagger_dir_path: PathBuf, db: Arc, command_sender: Arc, ) -> Result<()> { - let app = App::new(); - - HttpServer::new(|| App::new().route("/", web::get().to(index))) - .bind(format!("127.0.0.1:{}", port))? - .run(); + HttpServer::new(move || { + App::new().configure(|cfg| { + super::configure_app( + cfg, + &web_url, + web_dir_path.as_path(), + &swagger_url, + swagger_dir_path.as_path(), + ) + }) + }) + .bind(format!("127.0.0.1:{}", port))? + .run(); Ok(()) } diff --git a/src/service/actix/tests/mod.rs b/src/service/actix/tests/mod.rs index 8b13789..d3b07a4 100644 --- a/src/service/actix/tests/mod.rs +++ b/src/service/actix/tests/mod.rs @@ -1 +1,21 @@ +use std::path::PathBuf; +mod swagger; +mod web; + +fn configure_test_app(cfg: &mut actix_web::web::ServiceConfig) { + let web_url = "/"; + let web_dir_path = PathBuf::from("web"); + + let swagger_url = "swagger"; + let mut swagger_dir_path = PathBuf::from("docs"); + swagger_dir_path.push("swagger"); + + super::configure_app( + cfg, + web_url, + web_dir_path.as_path(), + swagger_url, + swagger_dir_path.as_path(), + ); +} diff --git a/src/service/actix/tests/swagger.rs b/src/service/actix/tests/swagger.rs index e69de29..f717ae8 100644 --- a/src/service/actix/tests/swagger.rs +++ b/src/service/actix/tests/swagger.rs @@ -0,0 +1,21 @@ +use actix_web::dev::Service; +use actix_web::test::TestRequest; +use actix_web::{test, App}; + +#[actix_rt::test] +async fn test_index() { + let app = App::new().configure(super::configure_test_app); + let mut service = test::init_service(app).await; + let req = TestRequest::get().uri("/swagger").to_request(); + let resp = service.call(req).await.unwrap(); + assert!(resp.status().is_success()); +} + +#[actix_rt::test] +async fn test_index_with_trailing_slash() { + let app = App::new().configure(super::configure_test_app); + let mut service = test::init_service(app).await; + let req = TestRequest::get().uri("/swagger/").to_request(); + let resp = service.call(req).await.unwrap(); + assert!(resp.status().is_success()); +} diff --git a/src/service/actix/tests/web.rs b/src/service/actix/tests/web.rs index e69de29..898e9d1 100644 --- a/src/service/actix/tests/web.rs +++ b/src/service/actix/tests/web.rs @@ -0,0 +1,12 @@ +use actix_web::dev::Service; +use actix_web::test::TestRequest; +use actix_web::{test, App}; + +#[actix_rt::test] +async fn test_index() { + let app = App::new().configure(super::configure_test_app); + let mut service = test::init_service(app).await; + let req = TestRequest::get().uri("/").to_request(); + let resp = service.call(req).await.unwrap(); + assert!(resp.status().is_success()); +} diff --git a/src/service/rocket/server.rs b/src/service/rocket/server.rs index 0a07c73..05dbda7 100644 --- a/src/service/rocket/server.rs +++ b/src/service/rocket/server.rs @@ -51,22 +51,22 @@ pub fn get_server( pub fn run( port: u16, auth_secret: Option<&[u8]>, - api_url: &str, - web_url: &str, - web_dir_path: &PathBuf, - swagger_url: &str, - swagger_dir_path: &PathBuf, + api_url: String, + web_url: String, + web_dir_path: PathBuf, + swagger_url: String, + swagger_dir_path: PathBuf, db: Arc, command_sender: Arc, ) -> Result<()> { let server = get_server( port, auth_secret, - api_url, - web_url, - web_dir_path, - swagger_url, - swagger_dir_path, + &api_url, + &web_url, + &web_dir_path, + &swagger_url, + &swagger_dir_path, db, command_sender, )?;