-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
glendc
committed
Oct 23, 2023
1 parent
9ae7b0f
commit 85b880e
Showing
12 changed files
with
212 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod shortlink; | ||
pub use shortlink::Shortlink; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#[derive(Debug, Clone)] | ||
pub struct Shortlink { | ||
id: String, | ||
long_url: String, | ||
_owner: String, | ||
} | ||
|
||
impl Shortlink { | ||
pub fn new(long_url: String, owner: String) -> Self { | ||
let id = nanoid::nanoid!(8); | ||
Self { | ||
id, | ||
long_url, | ||
_owner: owner, | ||
} | ||
} | ||
|
||
pub fn _owner(&self) -> &str { | ||
&self._owner | ||
} | ||
|
||
pub fn long_url(&self) -> &str { | ||
&self.long_url | ||
} | ||
|
||
pub fn id(&self) -> &str { | ||
&self.id | ||
} | ||
|
||
pub fn short_url(&self) -> String { | ||
format!("/{}", self.id,) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
use axum::{extract::Path, response::Redirect}; | ||
use std::sync::Arc; | ||
|
||
pub async fn get(Path(hash): Path<String>) -> Redirect { | ||
use axum::{ | ||
extract::{Path, State}, | ||
response::Redirect, | ||
}; | ||
|
||
pub async fn get( | ||
State(state): State<Arc<crate::router::State>>, | ||
Path(hash): Path<String>, | ||
) -> Redirect { | ||
match hash.as_str() { | ||
"code" => Redirect::permanent("https://github.com/plabayo/bucket"), | ||
"author" => Redirect::permanent("https://plabayo.tech"), | ||
"og-image" => Redirect::permanent( | ||
"https://upload.wikimedia.org/wikipedia/commons/3/3b/Sand_bucket.jpg", | ||
), | ||
hash => Redirect::temporary(&format!("https://{hash}")), | ||
hash => { | ||
if let Some(link) = state.storage.get_shortlink(hash).await { | ||
Redirect::temporary(link.long_url()) | ||
} else { | ||
Redirect::temporary("/") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
mod auth; | ||
pub use auth::{Auth, COOKIE_NAME}; | ||
|
||
mod storage; | ||
pub use storage::Storage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::{ | ||
collections::{HashMap, HashSet}, | ||
sync::{Arc, Mutex}, | ||
}; | ||
|
||
use crate::data::Shortlink; | ||
|
||
#[derive(Debug)] | ||
pub struct Storage { | ||
inner: Arc<Mutex<InnerStorage>>, | ||
} | ||
|
||
impl Clone for Storage { | ||
fn clone(&self) -> Self { | ||
Self { | ||
inner: self.inner.clone(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct InnerStorage { | ||
shortlinks: HashMap<String, Shortlink>, | ||
blocked_domains: HashSet<String>, | ||
} | ||
|
||
impl Storage { | ||
pub fn new() -> Self { | ||
Self { | ||
inner: Arc::new(Mutex::new(InnerStorage { | ||
shortlinks: HashMap::new(), | ||
blocked_domains: HashSet::new(), | ||
})), | ||
} | ||
} | ||
|
||
pub async fn is_domain_blocked(&self, domain: &str) -> bool { | ||
self.inner.lock().unwrap().blocked_domains.contains(domain) | ||
} | ||
|
||
pub async fn add_shortlink(&self, shortlink: &Shortlink) -> Result<(), String> { | ||
let id = shortlink.id().to_string(); | ||
self.inner | ||
.lock() | ||
.unwrap() | ||
.shortlinks | ||
.entry(id) | ||
.or_insert(shortlink.clone()); | ||
Ok(()) | ||
} | ||
|
||
pub async fn get_shortlink(&self, id: &str) -> Option<Shortlink> { | ||
self.inner.lock().unwrap().shortlinks.get(id).cloned() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters