From e5702b3685c550dc301ff1483965779014edbd13 Mon Sep 17 00:00:00 2001 From: peterrabbit <peterrabbit@msi.home> Date: Wed, 31 Aug 2022 19:40:41 +0200 Subject: [PATCH] ip serve static files --- Cargo.lock | 1 + Cargo.toml | 1 + default_static/favicon.ico | Bin 0 -> 7758 bytes src/app/config.rs | 4 +--- src/app/state.rs | 5 +++-- src/main.rs | 9 ++++++--- src/service/files.rs | 11 +++++++++++ src/service/mod.rs | 1 + src/static_files.rs | 5 +++-- 9 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 default_static/favicon.ico create mode 100644 src/service/files.rs diff --git a/Cargo.lock b/Cargo.lock index ac6b541..d06439e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -517,6 +517,7 @@ dependencies = [ name = "cms_rust" version = "0.1.0" dependencies = [ + "actix-files", "actix-web", "actix-web-lab", "dirs", diff --git a/Cargo.toml b/Cargo.toml index c86923c..5bebde7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ dirs = "4.0" structopt = "0.3" env_logger = "0.9" actix-web-lab = "0.17.0" +actix-files = "0.6.2" diff --git a/default_static/favicon.ico b/default_static/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..6c215345b05c43669984f26ba69e8d1e708608e2 GIT binary patch literal 7758 zcmchbdrTZf9LMKM31XU7E4EP*q-xO=(^OhoUkGi3wpg^eK0p%)kCs;ph&<$>cQglQ zPN4_f9nXUcP>W(O5NjI}OG3rO`bza56Rn9Dt%<2N{9&{;jpKLjZs{@X?(N(GOTPT} zH}l)w@8@P`cC&=&@RzWW;D0Q6B8HH4gpfy}1Skz+?MuBK-23b!P#@F})j_3DE|djr zfwn;pK`SA`v_hv_YFk3*ho>0mDntcz4YfZ&pF^Kj<>?0<Zx*c(y6_?u-x(3ke%DCo zYe<A)UTt~yOVsv8AqB_IMY5*(H0#!`CI`It$7CoBm3eyRUs9E31B*}?7aL6u>`7u{ z(G?0qec@rrzZBhzU{BL265<~w`sY0i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avz&#|5%LtB%5b-5i3(sth2t5ls4RhbvB%ez07W@}MqPBvn2gi3JEd0BP!ub4# z_N2{m_{hFY8yGH%aJbC#-%VsVrqLE1eeYe%MBu#)7+fM8!<GuA`-ru7Ly;@Pmj5$5 z7|bFZa_=Va{uh+rbK=;tj)DKhz;INA<Ab)>`SG|4h8;Qkcln=o+4sSaD#F3Nn@oaX zea0TmuMtbe2Zr^K9b)t367&<KxEA}pn{;*>ipjp@txM)DloPkr<+Eps4%z@ELOD<c z<bcNTO#cF7aCoGpn(W*j)Mr9X_9&f-fC#0HWQ`}1`SCwrev6O@)_=nnXMJ(T7pHE= zFw~ilv&c3+>U`}|Bt~x?(M^#Vb^tay;|bd0t0~?(oR=NP&dVaZE`?7I7vclHJlPkG X-l+5N&!XI$dw-8vl(U>RCbRfIt=8lG literal 0 HcmV?d00001 diff --git a/src/app/config.rs b/src/app/config.rs index 42ce778..97e519b 100644 --- a/src/app/config.rs +++ b/src/app/config.rs @@ -1,6 +1,5 @@ use crate::app::AppArgs; use std::path::PathBuf; -use structopt::StructOpt; #[derive(Clone)] pub enum AppContext { @@ -22,8 +21,7 @@ pub struct AppConfig { } impl AppConfig { - pub fn new() -> Self { - let app_args = AppArgs::from_args(); + pub fn new(app_args: AppArgs) -> Self { let exec_path = std::env::current_exe().unwrap(); let exec_name = exec_path .file_name() diff --git a/src/app/state.rs b/src/app/state.rs index 8d0dc75..eb498c1 100644 --- a/src/app/state.rs +++ b/src/app/state.rs @@ -1,4 +1,5 @@ -use crate::app::{AppConfig, AppPreferences}; +use crate::app::{AppArgs, AppConfig, AppPreferences}; +use structopt::StructOpt; #[derive(Clone)] pub struct AppState { @@ -11,7 +12,7 @@ pub struct AppState { impl AppState { pub fn new() -> Self { AppState { - config: AppConfig::new(), + config: AppConfig::new(AppArgs::from_args()), preferences: AppPreferences {}, } } diff --git a/src/main.rs b/src/main.rs index c996be1..ca8ab2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod static_files; mod testing; mod tls_config; mod website; +use actix_files::Files; use actix_web::{App, HttpServer}; use actix_web_lab::middleware::RedirectHttps; use app::AppState; @@ -29,8 +30,8 @@ async fn main() -> std::io::Result<()> { let port_tls = app_state.config.port_tls; let srv_conf = tls_config(&app_state.config); - - let static_files_manager = + let static_dir = static_files_manager.dir.clone(); + let mut_static_files_manager = actix_web::web::Data::new(std::sync::Mutex::new(static_files_manager)); let app_state = actix_web::web::Data::new(std::sync::Mutex::new(app_state)); @@ -41,8 +42,10 @@ async fn main() -> std::io::Result<()> { .wrap(actix_web::middleware::Compress::default()) .wrap(RedirectHttps::default().to_port(port_tls)) .app_data(actix_web::web::Data::clone(&app_state)) - .app_data(actix_web::web::Data::clone(&static_files_manager)) + .app_data(actix_web::web::Data::clone(&mut_static_files_manager)) .app_data(actix_web::web::Data::new(website.clone())) + .service(Files::new("/static/", &static_dir)) + .service(service::files::favicon) .service(service::page) }) .bind(format!("{}:{}", host, port))? diff --git a/src/service/files.rs b/src/service/files.rs new file mode 100644 index 0000000..4800bfd --- /dev/null +++ b/src/service/files.rs @@ -0,0 +1,11 @@ +use crate::static_files::StaticFilesManager; +use actix_files::NamedFile; +use actix_web::{get, web, Responder}; + +#[get("/favicon.ico")] +pub async fn favicon( + static_files_manager: web::Data<std::sync::Mutex<StaticFilesManager>>, +) -> impl Responder { + let static_files_manager = static_files_manager.lock().unwrap(); + NamedFile::open(static_files_manager.dir.join("default").join("favicon.ico")) +} diff --git a/src/service/mod.rs b/src/service/mod.rs index f5f67f8..b27ee73 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -1,2 +1,3 @@ +pub mod files; mod page; pub use page::*; diff --git a/src/static_files.rs b/src/static_files.rs index 4da405f..7f33f48 100644 --- a/src/static_files.rs +++ b/src/static_files.rs @@ -1,9 +1,10 @@ use crate::app::AppState; use std::path::{Path, PathBuf}; +#[derive(Clone)] pub struct StaticFilesManager { - dir: PathBuf, - index: Vec<String>, + pub dir: PathBuf, + pub index: Vec<String>, } impl StaticFilesManager { -- GitLab