Skip to content

Commit

Permalink
custom id works
Browse files Browse the repository at this point in the history
  • Loading branch information
DrewMcArthur committed Jan 21, 2024
1 parent be628ab commit 558e079
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use routes::{

#[derive(Clone, Debug, Serialize, Deserialize)]
struct Target {
id: Option<String>,
url: String,
pw_hash: Option<String>,
}
Expand Down
5 changes: 4 additions & 1 deletion src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ pub(crate) async fn stats(req: Request, ctx: RouteContext<()>) -> Result<Respons
)
}
}
Err(e) => Response::error(e.to_string(), 500),
Err(e) => Response::error(
format!("error fetching data from store: {}", e.to_string()),
500,
),
}
} else {
Response::redirect(Url::parse(format!("{}/stats", origin).as_str())?)
Expand Down
18 changes: 14 additions & 4 deletions src/store.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use uuid::Uuid;
use worker::{kv::KvStore, Error, Result, RouteContext};
use worker::{console_log, kv::KvStore, Error, Result, RouteContext};

use crate::Target;

Expand All @@ -22,11 +22,12 @@ pub(crate) async fn create_new_target(ctx: &RouteContext<()>, target: &Target) -
.kv(TRACKED_URLS_STORE)
.expect("couldn't get URLS kv store");

let new_id = match ctx.param("id") {
let new_id = match &target.id {
Some(id) => id.to_owned(),
None => gen_unused_id(&kv).await.expect("could not generate new id"),
};

console_log!("creating new target with id={}: {:?}", new_id, target);
kv.put(&new_id, target.clone())
.expect("couldn't put new target")
.execute()
Expand All @@ -48,8 +49,17 @@ pub(crate) async fn get_redirect_count(ctx: &RouteContext<()>, id: &str) -> Resu
}

pub(crate) async fn get_target_by_id(ctx: &RouteContext<()>, id: &str) -> Result<Target> {
let kv = ctx.kv(TRACKED_URLS_STORE)?;
match kv.get(id).json::<Target>().await? {
console_log!("fetching target by id: {}", id);
let kv = ctx
.kv(TRACKED_URLS_STORE)
.expect("couldn't get URLS kv store");

match kv
.get(id)
.json::<Target>()
.await
.expect("couldn't parse target from kvstore")
{
Some(target) => Ok(target),
None => Err(Error::Internal("ID Not Found in our System".into())),
}
Expand Down
6 changes: 6 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ pub(crate) async fn get_target(req: Request) -> Result<Target> {
FormEntry::File(_) => return Err(bad_url_format_error),
};

let id = match data.get("id") {
Some(FormEntry::Field(s)) => Some(s),
_ => None,
};

let pw = match data.get("password") {
Some(FormEntry::Field(s)) => Some(gen_hash(s)),
_ => None,
};

Ok(Target {
id: id,
url: new_url.clone(),
pw_hash: pw,
})
Expand Down

0 comments on commit 558e079

Please sign in to comment.