Skip to content

Commit

Permalink
url shortener use serde json operator
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaro00 committed Jan 9, 2025
1 parent 68cc8ca commit 9408571
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions rocket/url-shortener/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ use rocket::{
use url::Url;

struct AppState {
op: opendal::Operator,
op: shuttle_shared_db::SerdeJsonOperator,
}

// for showcasing SerdeJsonOperator
#[derive(serde::Serialize, serde::Deserialize)]
struct U {
inner: String,
}

#[get("/<id>")]
async fn redirect(id: String, state: &State<AppState>) -> Result<Redirect, status::Custom<String>> {
let url = state
let url: U = state
.op
.read(&id.to_string())
.read_serialized(&id)
.await
.map_err(|err| match err.kind() {
opendal::ErrorKind::NotFound => status::Custom(
Expand All @@ -26,7 +32,7 @@ async fn redirect(id: String, state: &State<AppState>) -> Result<Redirect, statu
_ => status::Custom(Status::InternalServerError, "something went wrong".into()),
})?;

Ok(Redirect::to(String::from_utf8(url.to_vec()).unwrap()))
Ok(Redirect::to(url.inner))
}

#[post("/", data = "<url>")]
Expand All @@ -45,7 +51,7 @@ async fn shorten<'r>(

state
.op
.write(&id.to_string(), url.into_bytes())
.write_serialized(&id.to_string(), &U { inner: url })
.await
.map_err(|_| status::Custom(Status::InternalServerError, "something went wrong".into()))?;

Expand All @@ -54,7 +60,7 @@ async fn shorten<'r>(

#[shuttle_runtime::main]
async fn main(
#[shuttle_shared_db::Postgres] op: opendal::Operator,
#[shuttle_shared_db::Postgres] op: shuttle_shared_db::SerdeJsonOperator,
) -> shuttle_rocket::ShuttleRocket {
let state = AppState { op };
let rocket = rocket::build()
Expand Down

0 comments on commit 9408571

Please sign in to comment.