From db8417d919b486e7f39dcea899c59201836cd07f Mon Sep 17 00:00:00 2001 From: SolomonTechnology <7052987f-303c-47f1-9584-e8b227ba3a60@happy-mochi.com> Date: Tue, 14 Jan 2025 00:25:49 +1000 Subject: [PATCH 1/6] Improve documentation for the "port" and "site_url" env variables --- compose.yaml | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/compose.yaml b/compose.yaml index 7236653..33e76bd 100644 --- a/compose.yaml +++ b/compose.yaml @@ -7,6 +7,8 @@ services: restart: unless-stopped container_name: chhoto-url ports: + # If you changed the "port" environment variable, adjust accordingly + # The number after the colon should match the "port" environment variable - 4567:4567 environment: # Change if you want to mount the database somewhere else. @@ -18,11 +20,17 @@ services: # a copy of your database.) - db_url=/db/urls.sqlite - # Change it in case you want to set the website name - # displayed in front of the shorturls, defaults to - # the hostname you're accessing it from. + # Change this if your server URL is not "http://localhost" + # This must not be surrounded by quotes. For example: + # site_url="https://www.example.com" incorrect + # site_url=https://www.example.com correct + # This is important to ensure Chhoto URL outputs the shortened link with the correct URL. # - site_url=https://www.example.com + # Change this if you are running Chhoto URL on a port which is not 4567. + # This is important to ensure Chhoto URL outputs the shortened link with the correct port. + # - port=4567 + - password=TopSecretPass # This needs to be set in order to use programs that use the JSON interface of Chhoto URL. From 7b52bd60da658e331d0006325808def8620c753a Mon Sep 17 00:00:00 2001 From: SolomonTechnology <7052987f-303c-47f1-9584-e8b227ba3a60@happy-mochi.com> Date: Tue, 14 Jan 2025 00:33:01 +1000 Subject: [PATCH 2/6] Rewording --- compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compose.yaml b/compose.yaml index 33e76bd..c418884 100644 --- a/compose.yaml +++ b/compose.yaml @@ -8,7 +8,7 @@ services: container_name: chhoto-url ports: # If you changed the "port" environment variable, adjust accordingly - # The number after the colon should match the "port" environment variable + # Both numbers before and after the colon should match the "port" variable - 4567:4567 environment: # Change if you want to mount the database somewhere else. From 17d0df943b4a6d85cefe3ec329893226a3df958a Mon Sep 17 00:00:00 2001 From: Solninja A Date: Tue, 14 Jan 2025 17:20:41 +1000 Subject: [PATCH 3/6] Correctly output created link --- actix/src/main.rs | 37 ++++++++++++++++++++++++++++++++----- actix/src/services.rs | 19 +++++++++++++++++-- compose.yaml | 4 ++-- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/actix/src/main.rs b/actix/src/main.rs index f9dd507..d496a69 100644 --- a/actix/src/main.rs +++ b/actix/src/main.rs @@ -30,6 +30,7 @@ async fn main() -> Result<()> { .filter(|s| !s.trim().is_empty()) .unwrap_or(String::from("urls.sqlite")); + // Get the port environment variable let port = env::var("port") .unwrap_or(String::from("4567")) .parse::() @@ -42,18 +43,43 @@ async fn main() -> Result<()> { // If an API key is set, check the security if let Ok(key) = env::var("api_key") { if !auth::is_key_secure() { - eprintln!("API key is insecure! Please change it. Current key is: {}. Generated secure key which you may use: {}", key, auth::gen_key()) + eprintln!("WARN: API key is insecure! Please change it. Current key is: {}. Generated secure key which you may use: {}", key, auth::gen_key()) } else { eprintln!("Secure API key was provided.") } } - // Tell the user that the server has started, and where it is listening to, rather than simply outputting nothing - eprintln!("Server has started at 0.0.0.0 on port {port}."); + // If the site_url env variable exists if let Some(site_url) = env::var("site_url").ok().filter(|s| !s.trim().is_empty()) { - eprintln!("Configured Site URL is: {site_url}."); + // Get first and last characters of the site_url + let mut chars = site_url.chars(); + let first = chars.next(); + let last = chars.next_back(); + let url = chars.as_str(); + // If the site_url is encapsulated by quotes (i.e. invalid) + if first == Option::from('"') || first == Option::from('\'') && first == last { + // Set the site_url without the quotes + env::set_var("site_url", url); + eprintln!("WARN: The site_url environment variable is encapsulated by quotes. Automatically adjusting to {}", url); + + // Tell the user what URI the server will respond with + eprintln!("INFO: Public URI is: {url}:{port}.") + } else { + // No issues + eprintln!("INFO: Configured Site URL is: {site_url}."); + + // Tell the user what URI the server will respond with + eprintln!("INFO: Public URI is: {site_url}:{port}.") + } + } else { + // Site URL is not configured + eprintln!("WARN: The site_url environment variable is not configured. Defaulting to http://localhost"); + eprintln!("INFO: Public URI is: http://localhost:{port}.") } + // Tell the user that the server has started, and where it is listening to, rather than simply outputting nothing + eprintln!("Server has started at 0.0.0.0 on port 4567."); + // Actually start the server HttpServer::new(move || { App::new() @@ -86,7 +112,8 @@ async fn main() -> Result<()> { .service(Files::new("/", "./resources/").index_file("index.html")) .default_service(actix_web::web::get().to(services::error404)) }) - .bind(("0.0.0.0", port))? + // Hardcode the port the server listens to. Allows for more intuitive Docker Compose port management + .bind(("0.0.0.0", 4567))? .run() .await } diff --git a/actix/src/services.rs b/actix/src/services.rs index dfd44e9..0eebfcb 100644 --- a/actix/src/services.rs +++ b/actix/src/services.rs @@ -11,7 +11,6 @@ use actix_web::{ Either, HttpRequest, HttpResponse, Responder, }; use std::env; - // Serialize JSON data use serde::Serialize; @@ -68,7 +67,7 @@ pub async fn add_link( .unwrap_or(String::from("4567")) .parse::() .expect("Supplied port is not an integer"); - let url = format!( + let mut url = format!( "{}:{}", env::var("site_url") .ok() @@ -76,6 +75,22 @@ pub async fn add_link( .unwrap_or(String::from("http://localhost")), port ); + // If the port is 80, remove the port from the returned URL (better for copying and pasting) + // Return http:// + if port == 80 { + url = env::var("site_url") + .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:// + if port == 443 { + url = env::var("site_url") + .ok() + .filter(|s| !s.trim().is_empty()) + .unwrap_or(String::from("https://localhost")); + } let response = CreatedURL { success: true, error: false, diff --git a/compose.yaml b/compose.yaml index c418884..d1c9852 100644 --- a/compose.yaml +++ b/compose.yaml @@ -8,7 +8,7 @@ services: container_name: chhoto-url ports: # If you changed the "port" environment variable, adjust accordingly - # Both numbers before and after the colon should match the "port" variable + # The number before the colon should match the "port" variable - 4567:4567 environment: # Change if you want to mount the database somewhere else. @@ -36,7 +36,7 @@ services: # This needs to be set in order to use programs that use the JSON interface of Chhoto URL. # You will get a warning if this is insecure, and a generated value will be output # You may use that value if you can't think of a secure key - # - api_key=SECURE_API_KEY + - api_key=SECURE_API_KEY # Pass the redirect method, if needed. TEMPORARY and PERMANENT # are accepted values, defaults to PERMANENT. From fc785c3eef9f4168f548f7d322e3b0e618cb0b46 Mon Sep 17 00:00:00 2001 From: Solninja A Date: Tue, 14 Jan 2025 18:16:37 +1000 Subject: [PATCH 4/6] Re-comment the API key in compose.yaml --- compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compose.yaml b/compose.yaml index d1c9852..cb88eae 100644 --- a/compose.yaml +++ b/compose.yaml @@ -36,7 +36,7 @@ services: # This needs to be set in order to use programs that use the JSON interface of Chhoto URL. # You will get a warning if this is insecure, and a generated value will be output # You may use that value if you can't think of a secure key - - api_key=SECURE_API_KEY + # - api_key=SECURE_API_KEY # Pass the redirect method, if needed. TEMPORARY and PERMANENT # are accepted values, defaults to PERMANENT. From 10477632855438dc035f82195724011ee3687663 Mon Sep 17 00:00:00 2001 From: Solninja A Date: Sat, 18 Jan 2025 12:52:19 +1000 Subject: [PATCH 5/6] chg: Bind server to port env variable --- actix/src/main.rs | 4 ++-- compose.yaml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/actix/src/main.rs b/actix/src/main.rs index d496a69..71d66fe 100644 --- a/actix/src/main.rs +++ b/actix/src/main.rs @@ -78,7 +78,7 @@ async fn main() -> Result<()> { } // Tell the user that the server has started, and where it is listening to, rather than simply outputting nothing - eprintln!("Server has started at 0.0.0.0 on port 4567."); + eprintln!("Server has started at 0.0.0.0 on port {port}."); // Actually start the server HttpServer::new(move || { @@ -113,7 +113,7 @@ async fn main() -> Result<()> { .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", 4567))? + .bind(("0.0.0.0", port))? .run() .await } diff --git a/compose.yaml b/compose.yaml index cb88eae..c418884 100644 --- a/compose.yaml +++ b/compose.yaml @@ -8,7 +8,7 @@ services: container_name: chhoto-url ports: # If you changed the "port" environment variable, adjust accordingly - # The number before the colon should match the "port" variable + # Both numbers before and after the colon should match the "port" variable - 4567:4567 environment: # Change if you want to mount the database somewhere else. From 0b1224f8e53debbc0d91fc21d7223b91c2f28552 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Fri, 17 Jan 2025 23:27:33 -0600 Subject: [PATCH 6/6] docs: Improve clarification of the port variables --- compose.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compose.yaml b/compose.yaml index c418884..cf1060d 100644 --- a/compose.yaml +++ b/compose.yaml @@ -8,7 +8,8 @@ services: container_name: chhoto-url ports: # If you changed the "port" environment variable, adjust accordingly - # Both numbers before and after the colon should match the "port" variable + # The number AFTER the colon should match the "port" variable and the number + # before the colon is the port where you would access the container from outside. - 4567:4567 environment: # Change if you want to mount the database somewhere else.