From 0ea0997a4f1c1df72cfab71e8ad14cd8e0e101eb Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 15 Apr 2021 11:41:33 +0200 Subject: [PATCH] Can serve user data files directly from the API --- Cargo.lock | 88 +++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 1 + config.yaml | 5 +++ src/data/config.rs | 3 ++ src/server.rs | 15 +++++--- 5 files changed, 103 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2b16798..0749853 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -60,6 +60,26 @@ dependencies = [ "trust-dns-resolver", ] +[[package]] +name = "actix-files" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c51e8a9146c12fce92a6e4c24b8c4d9b05268130bfd8d61bc587e822c32ce689" +dependencies = [ + "actix-service", + "actix-web", + "bitflags", + "bytes 0.5.6", + "derive_more", + "futures-core", + "futures-util", + "log", + "mime", + "mime_guess", + "percent-encoding", + "v_htmlescape", +] + [[package]] name = "actix-http" version = "2.2.0" @@ -595,6 +615,15 @@ dependencies = [ "libc", ] +[[package]] +name = "buf-min" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa17aa1cf56bdd6bb30518767d00e58019d326f3f05d8c3e0730b549d332ea83" +dependencies = [ + "bytes 0.5.6", +] + [[package]] name = "bufstream" version = "0.1.4" @@ -728,6 +757,7 @@ name = "comunic_server" version = "0.1.0" dependencies = [ "actix", + "actix-files", "actix-multipart", "actix-rt", "actix-web", @@ -774,7 +804,7 @@ checksum = "784ad0fbab4f3e9cef09f20e0aea6000ae08d2cb98ac4c0abc53df18803d702f" dependencies = [ "percent-encoding", "time 0.2.25", - "version_check", + "version_check 0.9.2", ] [[package]] @@ -1221,7 +1251,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" dependencies = [ "typenum", - "version_check", + "version_check 0.9.2", ] [[package]] @@ -1984,6 +2014,16 @@ dependencies = [ "libc", ] +[[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 = "ntapi" version = "0.3.6" @@ -2936,7 +2976,7 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2beb4d1860a61f571530b3f855a1b538d0200f7871c63331ecd6f17b1f014f8" dependencies = [ - "version_check", + "version_check 0.9.2", ] [[package]] @@ -3156,7 +3196,7 @@ dependencies = [ "standback", "stdweb", "time-macros", - "version_check", + "version_check 0.9.2", "winapi 0.3.9", ] @@ -3395,7 +3435,7 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" dependencies = [ - "version_check", + "version_check 0.9.2", ] [[package]] @@ -3458,12 +3498,50 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" +[[package]] +name = "v_escape" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3e0ab5fab1db278a9413d2ea794cb66f471f898c5b020c3c394f6447625d9d4" +dependencies = [ + "buf-min", + "v_escape_derive", +] + +[[package]] +name = "v_escape_derive" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c860ad1273f4eee7006cee05db20c9e60e5d24cba024a32e1094aa8e574f3668" +dependencies = [ + "nom", + "proc-macro2 1.0.24", + "quote 1.0.9", + "syn 1.0.60", +] + +[[package]] +name = "v_htmlescape" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f9a8af610ad6f7fc9989c9d2590d9764bc61f294884e9ee93baa58795174572" +dependencies = [ + "cfg-if 1.0.0", + "v_escape", +] + [[package]] name = "vcpkg" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b00bca6106a5e23f3eee943593759b7fcddb00554332e856d990c893966879fb" +[[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" diff --git a/Cargo.toml b/Cargo.toml index 1d2e6f9..1ca16b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ yaml-rust = "0.4.5" mysql = "20.1.0" actix = "0.10.0" actix-web = "3.3.2" +actix-files = "0.5.0" actix-rt = "1.1.1" actix-multipart = "0.3.0" actix-web-actors = "3.0.0" diff --git a/config.yaml b/config.yaml index 0e9c90b..ca12730 100644 --- a/config.yaml +++ b/config.yaml @@ -17,6 +17,11 @@ proxy: "127.0.0.1" storage-url: http://devweb.local/comunic/current/user_data/ storage-path: /home/pierre/Documents/projets_web/comunic/current/user_data/ +# Specify whether user data files should be made available under the user_data directory +# +# It is your responsibility to update storage-url accordingly +serve-storage-files: true + # URL where Comunic Terms of use are available terms-url: http://devweb.local/comunic/current/about.php?cgu privacy-policy-url: http://devweb.local/comunic/current/about.php?cgu&privacy diff --git a/src/data/config.rs b/src/data/config.rs index 94d7db9..7044b2f 100644 --- a/src/data/config.rs +++ b/src/data/config.rs @@ -37,6 +37,7 @@ pub struct Config { pub listen_address: String, pub storage_url: String, pub storage_path: String, + pub serve_storage_file: bool, pub terms_url: String, pub privacy_policy_url: String, pub play_store_url: String, @@ -122,6 +123,8 @@ impl Config { storage_url: Config::yaml_str(parsed, "storage-url"), storage_path: Config::yaml_str(parsed, "storage-path"), + serve_storage_file: Config::yaml_bool(parsed, "serve-storage-files"), + terms_url: Config::yaml_str(parsed, "terms-url"), privacy_policy_url: Config::yaml_str(parsed, "privacy-policy-url"), diff --git a/src/server.rs b/src/server.rs index 4966132..20dc72e 100644 --- a/src/server.rs +++ b/src/server.rs @@ -20,6 +20,7 @@ use crate::data::http_request_handler::HttpRequestHandler; use crate::helpers::{api_helper, requests_limit_helper}; use crate::routes::{get_routes, RequestResult, Route}; use crate::routes::Method::{GET, POST}; +use crate::utils::user_data_utils::user_data_path; /// Main server functions /// @@ -335,11 +336,17 @@ pub async fn start_server(conf: &Config) -> std::io::Result<()> { let addr = conf.server_listen_address(); println!("Start to listen on http://{}/", addr); - HttpServer::new(|| { - App::new() + let serve_storage_file = conf.serve_storage_file; - // User WebSocket route - .service(actix_web::web::resource("/ws").to(user_ws_controller::ws_route)) + HttpServer::new(move || { + let mut app = App::new(); + + if serve_storage_file { + app = app.service(actix_files::Files::new("/user_data", user_data_path("".as_ref()))); + } + + // User WebSocket route + app.service(actix_web::web::resource("/ws").to(user_ws_controller::ws_route)) // RTC Relay WebSocket route .service(actix_web::web::resource("/rtc_proxy/ws").to(rtc_relay_controller::open_ws))