1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2024-12-26 07:38:36 -06:00

Merge pull request #12 from SinTan1729/nanoid

Support uid slugs
This commit is contained in:
Sayantan Santra 2024-03-31 01:01:33 -05:00 committed by GitHub
commit 3441d3ae90
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 59 additions and 20 deletions

View file

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

28
actix/Cargo.lock generated
View file

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

View file

@ -1,6 +1,6 @@
[package]
name = "chhoto-url"
version = "5.1.0"
version = "5.2.0"
edition = "2021"
authors = ["Sayantan Santra <sayantan[dot]santra689[at]gmail[dot]com"]
license = "mit"
@ -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"

View file

@ -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"];
#[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()
)
}
}

View file

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