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

Merge pull request #42 from SolninjaA/main

Correctly output created link
This commit is contained in:
Sayantan Santra 2025-01-17 23:28:30 -06:00 committed by GitHub
commit 1775f71347
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 60 additions and 9 deletions

View file

@ -30,6 +30,7 @@ async fn main() -> Result<()> {
.filter(|s| !s.trim().is_empty()) .filter(|s| !s.trim().is_empty())
.unwrap_or(String::from("urls.sqlite")); .unwrap_or(String::from("urls.sqlite"));
// Get the port environment variable
let port = env::var("port") let port = env::var("port")
.unwrap_or(String::from("4567")) .unwrap_or(String::from("4567"))
.parse::<u16>() .parse::<u16>()
@ -42,17 +43,42 @@ async fn main() -> Result<()> {
// If an API key is set, check the security // If an API key is set, check the security
if let Ok(key) = env::var("api_key") { if let Ok(key) = env::var("api_key") {
if !auth::is_key_secure() { 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 { } else {
eprintln!("Secure API key was provided.") 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 // 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}."); 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 // Actually start the server
HttpServer::new(move || { HttpServer::new(move || {
@ -86,6 +112,7 @@ async fn main() -> Result<()> {
.service(Files::new("/", "./resources/").index_file("index.html")) .service(Files::new("/", "./resources/").index_file("index.html"))
.default_service(actix_web::web::get().to(services::error404)) .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))? .bind(("0.0.0.0", port))?
.run() .run()
.await .await

View file

@ -11,7 +11,6 @@ use actix_web::{
Either, HttpRequest, HttpResponse, Responder, Either, HttpRequest, HttpResponse, Responder,
}; };
use std::env; use std::env;
// Serialize JSON data // Serialize JSON data
use serde::Serialize; use serde::Serialize;
@ -68,7 +67,7 @@ pub async fn add_link(
.unwrap_or(String::from("4567")) .unwrap_or(String::from("4567"))
.parse::<u16>() .parse::<u16>()
.expect("Supplied port is not an integer"); .expect("Supplied port is not an integer");
let url = format!( let mut url = format!(
"{}:{}", "{}:{}",
env::var("site_url") env::var("site_url")
.ok() .ok()
@ -76,6 +75,22 @@ pub async fn add_link(
.unwrap_or(String::from("http://localhost")), .unwrap_or(String::from("http://localhost")),
port 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 { let response = CreatedURL {
success: true, success: true,
error: false, error: false,

View file

@ -7,6 +7,9 @@ services:
restart: unless-stopped restart: unless-stopped
container_name: chhoto-url container_name: chhoto-url
ports: 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 - 4567:4567
environment: environment:
# Change if you want to mount the database somewhere else. # Change if you want to mount the database somewhere else.
@ -18,11 +21,17 @@ services:
# a copy of your database.) # a copy of your database.)
- db_url=/db/urls.sqlite - db_url=/db/urls.sqlite
# Change it in case you want to set the website name # Change this if your server URL is not "http://localhost"
# displayed in front of the shorturls, defaults to # This must not be surrounded by quotes. For example:
# the hostname you're accessing it from. # 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 # - 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 - password=TopSecretPass
# This needs to be set in order to use programs that use the JSON interface of Chhoto URL. # This needs to be set in order to use programs that use the JSON interface of Chhoto URL.