diff --git a/actix/src/main.rs b/actix/src/main.rs index f9dd507..71d66fe 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,17 +43,42 @@ 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.") } } + // If the site_url env variable exists + if let Some(site_url) = env::var("site_url").ok().filter(|s| !s.trim().is_empty()) { + // 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 {port}."); - if let Some(site_url) = env::var("site_url").ok().filter(|s| !s.trim().is_empty()) { - eprintln!("Configured Site URL is: {site_url}."); - } // Actually start the server HttpServer::new(move || { @@ -86,6 +112,7 @@ async fn main() -> Result<()> { .service(Files::new("/", "./resources/").index_file("index.html")) .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))? .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 7236653..cf1060d 100644 --- a/compose.yaml +++ b/compose.yaml @@ -7,6 +7,9 @@ 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" 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. @@ -18,11 +21,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.