Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Add key generator to use as extensions (#16)
Browse files Browse the repository at this point in the history
* Add generator for shortened url

* Use shortener generator for POST endpoint
  • Loading branch information
adrianEffe authored Aug 15, 2023
1 parent 731187a commit c58bf96
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 15 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ chrono = { version = "0.4.21", features = ["serde"] }
tracing = "0.1.37"
tracing-subscriber = "0.3.17"
tower-http = { version = "0.4.3", features = ["trace"] }
md5 = "0.7.0"
base62 = "2.0.2"

[dependencies.sqlx]
version = "0.7.1"
Expand Down
8 changes: 8 additions & 0 deletions Tests/key_generator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use briefly::key_generator::generate;

#[test]
fn generate_seven_chars_key() {
let url = "https://www.rust-lang.org";
let generated = generate(url);
assert_eq!(generated, "e2XSHcR".to_string())
}
9 changes: 9 additions & 0 deletions src/key_generator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use base62::encode;
use md5::compute;

pub fn generate(value: &str) -> String {
let hash = compute(value);
let hash_bytes: [u8; 16] = hash.into();
let hash_u128: u128 = u128::from_le_bytes(hash_bytes);
encode(hash_u128)[..7].to_string()
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod configuration;
pub mod key_generator;
pub mod model;
pub mod request_tracing;
pub mod routes;
Expand Down
52 changes: 37 additions & 15 deletions src/routes/full_url.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
use crate::{model::UrlRequestModel, schema::CreateShortUrlSchema, AppState};
use crate::{
key_generator::generate, model::UrlRequestModel, schema::CreateShortUrlSchema, AppState,
};
use axum::{extract::State, Json};
use chrono::Utc;
use sqlx::Error;
use std::sync::Arc;
use uuid::Uuid;

pub async fn full_url(
State(data): State<Arc<AppState>>,
Json(payload): Json<CreateShortUrlSchema>,
) {
let query_result = sqlx::query_as!(
let mut retry_count = 3;

while retry_count > 0 {
let query_result = insert_in_db(&payload.url, State(data.clone())).await;

match query_result {
Ok(note) => {
println!("okay received testing: {:?}", note);
break;
}
Err(e) => {
println!("failed with error: {:?}", e);
//TODO : - For now assume is failing because of error code 23505, that stands for
//duplicate key
retry_count += 1;
}
}
}
}

async fn insert_in_db(
url: &str,
State(data): State<Arc<AppState>>,
) -> Result<UrlRequestModel, Error> {
let uuid = Uuid::new_v4();
let modifier = uuid.to_string() + url;
let shortened = generate(&modifier);

sqlx::query_as!(
UrlRequestModel,
"INSERT INTO briefly (id, url, extension, created_at) VALUES ($1, $2, $3, $4) RETURNING *",
Uuid::new_v4(),
payload.url.to_string(),
Uuid::new_v4().to_string(), // TODO: - handle extension to be shortened url extension
uuid,
url.to_string(),
shortened,
Utc::now()
)
.fetch_one(&data.db)
.await;

match query_result {
Ok(note) => {
println!("okay received testing: {:?}", note);
}
Err(e) => {
println!("{:?}", e);
}
}
.await
}

0 comments on commit c58bf96

Please sign in to comment.