-
Notifications
You must be signed in to change notification settings - Fork 0
/
url-shortener.rs
52 lines (41 loc) · 1.56 KB
/
url-shortener.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use rand::{distributions::Alphanumeric, Rng};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
struct UrlShortener {
url_map: Arc<Mutex<HashMap<String, String>>>,
base_url: String,
}
impl UrlShortener {
fn new(base_url: &str) -> Self {
UrlShortener {
url_map: Arc::new(Mutex::new(HashMap::new())),
base_url: base_url.to_string(),
}
}
fn shorten(&self, original_url: &str) -> String {
let mut rng = rand::thread_rng();
let short_id: String = (0..8).map(|_| rng.sample(Alphanumeric) as char).collect();
let short_url = format!("{}/{}", self.base_url, short_id);
let mut url_map = self.url_map.lock().unwrap();
url_map.insert(short_id.clone(), original_url.to_string());
short_url
}
fn resolve(&self, short_url: &str) -> Option<String> {
let short_id = short_url.split('/').last().unwrap_or("");
let url_map = self.url_map.lock().unwrap();
url_map.get(short_id).cloned()
}
}
fn main() {
let base_url = "http://short.ly";
let shortener = UrlShortener::new(base_url);
let original_url = "https://www.example.com/very/long/url";
let short_url = shortener.shorten(original_url);
println!("Original URL: {}", original_url);
println!("Shortened URL: {}", short_url);
if let Some(resolved_url) = shortener.resolve(&short_url) {
println!("Resolved URL: {}", resolved_url);
} else {
println!("Shortened URL could not be resolved.");
}
}