From f9e642275a4ab608e5b58aa184e64cf90d02467a Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 31 Mar 2024 00:38:42 -0500 Subject: [PATCH 1/6] new: Support uid slug --- actix/Cargo.lock | 10 ++++++++++ actix/Cargo.toml | 1 + actix/src/utils.rs | 34 ++++++++++++++++++++++++++-------- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/actix/Cargo.lock b/actix/Cargo.lock index 905044f..e252d65 100644 --- a/actix/Cargo.lock +++ b/actix/Cargo.lock @@ -481,6 +481,7 @@ dependencies = [ "actix-session", "actix-web", "env_logger", + "nanoid", "rand", "regex", "rusqlite", @@ -973,6 +974,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "nanoid" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ffa00dec017b5b1a8b7cf5e2c008bfda1aa7e0697ac1508b491fdf2622fb4d8" +dependencies = [ + "rand", +] + [[package]] name = "num-conv" version = "0.1.0" diff --git a/actix/Cargo.toml b/actix/Cargo.toml index a2d842e..6c3187d 100644 --- a/actix/Cargo.toml +++ b/actix/Cargo.toml @@ -31,3 +31,4 @@ regex = "1.10.3" rand = "0.8.5" actix-session = { version = "0.9.0", features = ["cookie-session"] } env_logger = "0.11.1" +nanoid = "0.4.0" diff --git a/actix/src/utils.rs b/actix/src/utils.rs index ba64e66..a7791dc 100644 --- a/actix/src/utils.rs +++ b/actix/src/utils.rs @@ -1,4 +1,7 @@ +use std::env; + use crate::database; +use nanoid::nanoid; use rand::seq::SliceRandom; use regex::Regex; use rusqlite::Connection; @@ -25,14 +28,21 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) { let chunks: Vec<&str> = req.split(';').collect(); let longlink = String::from(chunks[0]); + let style = env::var("slug-style").unwrap_or(String::from("pair")); + let len_str = env::var("slug-length").unwrap_or(String::from("8")); + let mut len: usize = len_str.parse().unwrap_or(8); + if len < 4 { + len = 4; + } + let mut shortlink; if chunks.len() > 1 { shortlink = chunks[1].to_string().to_lowercase(); if shortlink.is_empty() { - shortlink = random_name(); + shortlink = gen_link(style, len); } } else { - shortlink = random_name(); + shortlink = gen_link(style, len); } if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db).is_none() { @@ -53,7 +63,7 @@ pub fn delete_link(shortlink: String, db: &Connection) -> bool { } } -fn random_name() -> String { +fn gen_link(style: String, len: usize) -> String { #[rustfmt::skip] static ADJECTIVES: [&str; 108] = ["admiring", "adoring", "affectionate", "agitated", "amazing", "angry", "awesome", "beautiful", "blissful", "bold", "boring", "brave", "busy", "charming", "clever", "compassionate", "competent", "condescending", "confident", "cool", @@ -85,9 +95,17 @@ fn random_name() -> String { "taussig", "tesla", "tharp", "thompson", "torvalds", "tu", "turing", "varahamihira", "vaughan", "vaughn", "villani", "visvesvaraya", "volhard", "wescoff", "weierstrass", "wilbur", "wiles", "williams", "williamson", "wilson", "wing", "wozniak", "wright", "wu", "yalow", "yonath", "zhukovsky"]; - format!( - "{0}-{1}", - ADJECTIVES.choose(&mut rand::thread_rng()).unwrap(), - NAMES.choose(&mut rand::thread_rng()).unwrap() - ) + #[rustfmt::skip] + static CHARS: [char; 36] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', + 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; + + if style == "uid" { + nanoid!(len, &CHARS) + } else { + format!( + "{0}-{1}", + ADJECTIVES.choose(&mut rand::thread_rng()).unwrap(), + NAMES.choose(&mut rand::thread_rng()).unwrap() + ) + } } From 088cd594a5173b101ecc8e4d2a22a4957dc2d2b6 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 31 Mar 2024 00:45:29 -0500 Subject: [PATCH 2/6] chg: Change the env_var name --- actix/src/utils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actix/src/utils.rs b/actix/src/utils.rs index a7791dc..9236f10 100644 --- a/actix/src/utils.rs +++ b/actix/src/utils.rs @@ -28,8 +28,8 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) { let chunks: Vec<&str> = req.split(';').collect(); let longlink = String::from(chunks[0]); - let style = env::var("slug-style").unwrap_or(String::from("pair")); - let len_str = env::var("slug-length").unwrap_or(String::from("8")); + let style = env::var("slug_style").unwrap_or(String::from("pair")); + let len_str = env::var("slug_length").unwrap_or(String::from("8")); let mut len: usize = len_str.parse().unwrap_or(8); if len < 4 { len = 4; From 841f877ee8d40a6388f4cbee4f2e540a69816dbb Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 31 Mar 2024 00:51:10 -0500 Subject: [PATCH 3/6] docs: Include info about UID in docs --- README.md | 7 +++++-- compose.yaml | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aa74f67..f4df505 100644 --- a/README.md +++ b/README.md @@ -116,8 +116,11 @@ docker run -p 4567:4567 \ -d chhoto-url:latest ``` -You can also set the redirect method to Permanent 308 (default) or Temporary 307 by setting -the `redirect_method` variable to `TEMPORARY` or `PERMANENT` (it's matched exactly). +You can set the redirect method to Permanent 308 (default) or Temporary 307 by setting +the `redirect_method` variable to `TEMPORARY` or `PERMANENT` (it's matched exactly). By +default, the auto-generated links are adjective-name pairs. You can use UIDs by setting +the `slug_style` variable to `UID`. You can also set the length of those slug by setting +the `slug_length` variable. It defaults to 8, and a minimum of 4 is supported. ## Disable authentication If you do not define a password environment variable when starting the docker image, authentication diff --git a/compose.yaml b/compose.yaml index 06b453b..f7ac93e 100644 --- a/compose.yaml +++ b/compose.yaml @@ -21,6 +21,13 @@ services: # Pass the redirect method, if needed TEMPORARY and PERMANENT # are accepted values, defaults to PERMANENT # - redirect_method=TEMPORARY + + # By default, the auto-generated pairs are adjective-name pairs + # If you want UIDs, please change slug_style to UID + # Supported values for slug_style are "pair" and "UID" + # The length is 8 by default, and a minimum of 4 is allowed + # - slug_style=pair + # - slug_length=8 volumes: - db:/urls.sqlite networks: From 599b013fc997d3c0605f4c0853528f8b6303bb14 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 31 Mar 2024 00:51:32 -0500 Subject: [PATCH 4/6] chg: Change uid to UID --- actix/src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actix/src/utils.rs b/actix/src/utils.rs index 9236f10..af7c334 100644 --- a/actix/src/utils.rs +++ b/actix/src/utils.rs @@ -99,7 +99,7 @@ fn gen_link(style: String, len: usize) -> String { static CHARS: [char; 36] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; - if style == "uid" { + if style == "UID" { nanoid!(len, &CHARS) } else { format!( From 6c7ca8d0ac2ca6c027e15d7302b4dfd5ca749722 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 31 Mar 2024 00:59:05 -0500 Subject: [PATCH 5/6] fix: slug_style options are consistent now --- actix/src/utils.rs | 2 +- compose.yaml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/actix/src/utils.rs b/actix/src/utils.rs index af7c334..1575596 100644 --- a/actix/src/utils.rs +++ b/actix/src/utils.rs @@ -28,7 +28,7 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) { let chunks: Vec<&str> = req.split(';').collect(); let longlink = String::from(chunks[0]); - let style = env::var("slug_style").unwrap_or(String::from("pair")); + let style = env::var("slug_style").unwrap_or(String::from("Pair")); let len_str = env::var("slug_length").unwrap_or(String::from("8")); let mut len: usize = len_str.parse().unwrap_or(8); if len < 4 { diff --git a/compose.yaml b/compose.yaml index f7ac93e..18c725e 100644 --- a/compose.yaml +++ b/compose.yaml @@ -24,9 +24,9 @@ services: # By default, the auto-generated pairs are adjective-name pairs # If you want UIDs, please change slug_style to UID - # Supported values for slug_style are "pair" and "UID" + # Supported values for slug_style are "Pair" and "UID" # The length is 8 by default, and a minimum of 4 is allowed - # - slug_style=pair + # - slug_style=Pair # - slug_length=8 volumes: - db:/urls.sqlite From f6060eb649c0d7aeb7908b97a8a70bf91911029c Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 31 Mar 2024 00:59:58 -0500 Subject: [PATCH 6/6] build: Bumped version to 5.2.0 --- actix/Cargo.lock | 18 +++++++++--------- actix/Cargo.toml | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/actix/Cargo.lock b/actix/Cargo.lock index e252d65..fb9da0a 100644 --- a/actix/Cargo.lock +++ b/actix/Cargo.lock @@ -88,7 +88,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" dependencies = [ "quote", - "syn 2.0.55", + "syn 2.0.57", ] [[package]] @@ -217,7 +217,7 @@ dependencies = [ "actix-router", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.57", ] [[package]] @@ -475,7 +475,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chhoto-url" -version = "5.1.0" +version = "5.2.0" dependencies = [ "actix-files", "actix-session", @@ -1047,9 +1047,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pin-project-lite" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" [[package]] name = "pin-utils" @@ -1237,7 +1237,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.57", ] [[package]] @@ -1338,9 +1338,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.55" +version = "2.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "002a1b3dbf967edfafc32655d0f377ab0bb7b994aa1d32c8cc7e9b8bf3ebb8f0" +checksum = "11a6ae1e52eb25aab8f3fb9fca13be982a373b8f1157ca14b897a825ba4a2d35" dependencies = [ "proc-macro2", "quote", @@ -1680,7 +1680,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.57", ] [[package]] diff --git a/actix/Cargo.toml b/actix/Cargo.toml index 6c3187d..440ee82 100644 --- a/actix/Cargo.toml +++ b/actix/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "chhoto-url" -version = "5.1.0" +version = "5.2.0" edition = "2021" authors = ["Sayantan Santra