diff --git a/docker-compose.yaml b/docker-compose.yaml index b2c5792..5544f6a 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -19,6 +19,8 @@ services: environment: - TZ="Europe/Rome" + - BIND_ADDR="0.0.0.0" + - BIND_PORT="80" tty: true stdin_open: true ports: diff --git a/src/main.rs b/src/main.rs index b3bbe75..be46197 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,15 +8,23 @@ use actix_web::{ web::{self}, }; use serde_json::Value; -use std::{fs, path::PathBuf}; +use std::{env, fs, path::PathBuf, sync::LazyLock}; -pub static BIND_ADDR: &str = "0.0.0.0"; -pub static BIND_PORT: u16 = 80; -pub static STATIC_PAGES: &[&str] = &["index.html", "style.css", "view.html", "logo.png"]; pub static HTML_DIR: &str = "html/"; pub static LOG_DIR: &str = "logs/"; pub static DATA_STORAGE: &str = "storage/"; +pub static BIND_ADDR: LazyLock = LazyLock::new(|| env::var("BIND_ADDR").unwrap_or("127.0.0.1".into())); +pub static BIND_PORT: LazyLock = + LazyLock::new(|| env::var("BIND_PORT").ok().and_then(|s| s.parse().ok()).unwrap_or(8080)); + +pub static STATIC_PAGES: LazyLock> = LazyLock::new(|| { + fs::read_dir(HTML_DIR) + .unwrap() + .filter_map(|entry| entry.ok().and_then(|e| e.file_name().to_str().map(|s| s.to_string()))) + .collect() +}); + use crate::{ api::{api_get_asset, api_upload}, logs::log_to_file, @@ -43,7 +51,7 @@ async fn catch_all(req: HttpRequest, _payload: Option>) -> acti let now = std::time::Instant::now(); let response = match req.uri().path() { - path if STATIC_PAGES.contains(&&path[1..]) => { + path if STATIC_PAGES.contains(&path[1..].into()) => { let file_path = HTML_DIR.to_string() + path; Ok(NamedFile::open(file_path)?.into_response(&req)) } @@ -59,7 +67,7 @@ async fn main() -> std::io::Result<()> { let _ = fs::create_dir_all(DATA_STORAGE); let _ = fs::create_dir_all(LOG_DIR); - println!("Starting server at http://{}:{}/", BIND_ADDR, BIND_PORT); + println!("Starting server at http://{}:{}/", *BIND_ADDR, *BIND_PORT); tokio::spawn(async { let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(60)); loop { @@ -78,7 +86,7 @@ async fn main() -> std::io::Result<()> { .service(api_upload) .service(catch_all) }) - .bind((BIND_ADDR, BIND_PORT))? + .bind((BIND_ADDR.clone(), *BIND_PORT))? .run() .await }