1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2025-02-05 13:52:33 -06:00

Correctly output created link

This commit is contained in:
Solninja A 2025-01-14 17:20:41 +10:00
parent 7b52bd60da
commit 17d0df943b
3 changed files with 51 additions and 9 deletions

View file

@ -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::<u16>()
@ -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.")
}
}
// 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 || {
@ -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
}

View file

@ -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::<u16>()
.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,

View file

@ -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.