From 2c8f47c0cb125e9b32a756c50d0481baaca4d272 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Thu, 10 Apr 2025 12:45:49 -0500 Subject: [PATCH] new: Ability to disable frontend --- Makefile | 2 +- actix/src/main.rs | 14 ++++++++++---- actix/src/services.rs | 6 +++--- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 8f5f8ae..2595f88 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ docker-test: docker-local docker-stop docker run -p ${PORT}:${PORT} --name chhoto-url -e password="${PASSWORD}" -e public_mode="${PUBLIC_MODE}" \ -e site_url="${SITE_URL}" -e db_url="${DB_URL}" -e redirect_method="${REDIRECT_METHOD}" -e port="${PORT}"\ -e slug_style="${SLUG_STYLE}" -e slug_length="${SLUG_LENGTH}" -e cache_control_header="${CACHE_CONTROL_HEADER}"\ - -e api_key="${API_KEY}"\ + -e api_key="${API_KEY}" -e disable_frontend="${DISABLE_FRONTEND}"\ -d chhoto-url docker logs chhoto-url -f diff --git a/actix/src/main.rs b/actix/src/main.rs index 71d66fe..31d6575 100644 --- a/actix/src/main.rs +++ b/actix/src/main.rs @@ -40,6 +40,8 @@ async fn main() -> Result<()> { .ok() .filter(|s| !s.trim().is_empty()); + let disable_frontend = env::var("disable_frontend").is_ok_and(|s| s.trim() == "True"); + // If an API key is set, check the security if let Ok(key) = env::var("api_key") { if !auth::is_key_secure() { @@ -82,7 +84,7 @@ async fn main() -> Result<()> { // Actually start the server HttpServer::new(move || { - App::new() + let mut app = App::new() .wrap(middleware::Logger::default()) .wrap(middleware::Compress::default()) .wrap( @@ -108,9 +110,13 @@ async fn main() -> Result<()> { .service(services::delete_link) .service(services::login) .service(services::logout) - .service(services::expand) - .service(Files::new("/", "./resources/").index_file("index.html")) - .default_service(actix_web::web::get().to(services::error404)) + .service(services::expand); + + if !disable_frontend { + app = app.service(Files::new("/", "./resources/").index_file("index.html")); + } + + app.default_service(actix_web::web::get().to(services::error404)) }) // Hardcode the port the server listens to. Allows for more intuitive Docker Compose port management .bind(("0.0.0.0", port))? diff --git a/actix/src/services.rs b/actix/src/services.rs index 0eebfcb..f760a76 100644 --- a/actix/src/services.rs +++ b/actix/src/services.rs @@ -79,9 +79,9 @@ pub async fn add_link( // Return http:// if port == 80 { url = env::var("site_url") - .ok() - .filter(|s| !s.trim().is_empty()) - .unwrap_or(String::from("http://localhost")); + .ok() + .filter(|s| !s.trim().is_empty()) + .unwrap_or(String::from("http://localhost")); } // If the port is 443, remove the port from the returned URL (better for copying and pasting) // Return https://