Refactor environment variables to use LazyLock for dynamic binding address and port
This commit is contained in:
@@ -19,6 +19,8 @@ services:
|
|||||||
|
|
||||||
environment:
|
environment:
|
||||||
- TZ="Europe/Rome"
|
- TZ="Europe/Rome"
|
||||||
|
- BIND_ADDR="0.0.0.0"
|
||||||
|
- BIND_PORT="80"
|
||||||
tty: true
|
tty: true
|
||||||
stdin_open: true
|
stdin_open: true
|
||||||
ports:
|
ports:
|
||||||
|
|||||||
22
src/main.rs
22
src/main.rs
@@ -8,15 +8,23 @@ use actix_web::{
|
|||||||
web::{self},
|
web::{self},
|
||||||
};
|
};
|
||||||
use serde_json::Value;
|
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 HTML_DIR: &str = "html/";
|
||||||
pub static LOG_DIR: &str = "logs/";
|
pub static LOG_DIR: &str = "logs/";
|
||||||
pub static DATA_STORAGE: &str = "storage/";
|
pub static DATA_STORAGE: &str = "storage/";
|
||||||
|
|
||||||
|
pub static BIND_ADDR: LazyLock<String> = LazyLock::new(|| env::var("BIND_ADDR").unwrap_or("127.0.0.1".into()));
|
||||||
|
pub static BIND_PORT: LazyLock<u16> =
|
||||||
|
LazyLock::new(|| env::var("BIND_PORT").ok().and_then(|s| s.parse().ok()).unwrap_or(8080));
|
||||||
|
|
||||||
|
pub static STATIC_PAGES: LazyLock<Vec<String>> = 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::{
|
use crate::{
|
||||||
api::{api_get_asset, api_upload},
|
api::{api_get_asset, api_upload},
|
||||||
logs::log_to_file,
|
logs::log_to_file,
|
||||||
@@ -43,7 +51,7 @@ async fn catch_all(req: HttpRequest, _payload: Option<web::Json<Value>>) -> acti
|
|||||||
let now = std::time::Instant::now();
|
let now = std::time::Instant::now();
|
||||||
|
|
||||||
let response = match req.uri().path() {
|
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;
|
let file_path = HTML_DIR.to_string() + path;
|
||||||
Ok(NamedFile::open(file_path)?.into_response(&req))
|
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(DATA_STORAGE);
|
||||||
let _ = fs::create_dir_all(LOG_DIR);
|
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 {
|
tokio::spawn(async {
|
||||||
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(60));
|
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(60));
|
||||||
loop {
|
loop {
|
||||||
@@ -78,7 +86,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.service(api_upload)
|
.service(api_upload)
|
||||||
.service(catch_all)
|
.service(catch_all)
|
||||||
})
|
})
|
||||||
.bind((BIND_ADDR, BIND_PORT))?
|
.bind((BIND_ADDR.clone(), *BIND_PORT))?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user