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/actix/Cargo.lock b/actix/Cargo.lock index 905044f..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,12 +475,13 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chhoto-url" -version = "5.1.0" +version = "5.2.0" dependencies = [ "actix-files", "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" @@ -1037,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" @@ -1227,7 +1237,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.57", ] [[package]] @@ -1328,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", @@ -1670,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 a2d842e..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 (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() + ) + } } diff --git a/compose.yaml b/compose.yaml index 06b453b..18c725e 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: