From 0182fa749ea4b5e5ef4502fa2e2624d430aa8e0f Mon Sep 17 00:00:00 2001 From: supercmmetry Date: Thu, 29 Jul 2021 11:34:00 +0530 Subject: [PATCH 1/3] feat(curl): add curl support Signed-off-by: supercmmetry --- src/api/routes/paste.rs | 25 ++++++++++++++++++++++--- src/utils/domain.rs | 5 +++++ src/utils/mod.rs | 1 + 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 src/utils/domain.rs diff --git a/src/api/routes/paste.rs b/src/api/routes/paste.rs index 59056d0..eb8b61a 100644 --- a/src/api/routes/paste.rs +++ b/src/api/routes/paste.rs @@ -6,7 +6,7 @@ use rocket::response::status::Custom; use rocket_contrib::json::Json; use serde_json::Value; -use crate::api::catchers::{forbidden, internal_server_error, not_found, unprocessable_entity}; +use crate::{api::catchers::{forbidden, internal_server_error, not_found, unprocessable_entity}, utils::domain::get_domain}; use crate::api::guards::db::DbConn; use crate::core::paste::{entity::Paste, service::{create_paste, fetch_paste, update_paste}}; use crate::core::users::service::{create_or_fetch_user, fetch_user}; @@ -130,6 +130,25 @@ fn update(mut paste: Json, conn: DbConn, mut ck: Cookies) -> Custom Rocket { - rocket.mount("/api/paste", routes![create, fetch, update]) +#[post("/", data = "")] +fn anonymous(input: String, conn: DbConn) -> String { + let mut new_paste = Paste { + id: Some(phonetic_key::get_random_id()), + belongs_to: None, + content: input, + is_url: None, + }; + + dbg!(new_paste.id.clone()); + + match create_paste(&mut new_paste, &conn) { + Ok(_) => format!("{}/{}", get_domain(), new_paste.id.unwrap()), + Err(_) => String::from("Internal Server Error"), + } +} + +pub fn fuel(rocket: Rocket) -> Rocket { + rocket + .mount("/api/paste", routes![create, fetch, update]) + .mount("/", routes![anonymous]) } diff --git a/src/utils/domain.rs b/src/utils/domain.rs new file mode 100644 index 0000000..7686d6e --- /dev/null +++ b/src/utils/domain.rs @@ -0,0 +1,5 @@ +use std::env; + +pub fn get_domain() -> String { + env::var("KATBIN_DOMAIN_NAME").unwrap().parse::().expect("domain name") +} \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs index d4351c6..80e02cf 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -2,3 +2,4 @@ pub mod db; pub mod errors; pub mod phonetic_key; pub mod users; +pub mod domain; -- 2.45.2 From 36ca95e2e382a6fe6ed2fb2c9528c5c3148b7f8b Mon Sep 17 00:00:00 2001 From: supercmmetry Date: Thu, 29 Jul 2021 11:39:42 +0530 Subject: [PATCH 2/3] chore: fix warnings and cargo fmt Signed-off-by: supercmmetry --- src/api/routes/paste.rs | 61 ++++++++++++++++++++++---------------- src/api/routes/user.rs | 6 +--- src/core/paste/entity.rs | 2 +- src/core/paste/postgres.rs | 4 ++- src/core/users/entity.rs | 2 +- src/core/users/service.rs | 2 +- src/main.rs | 4 +-- src/utils/db.rs | 5 +++- src/utils/domain.rs | 7 +++-- src/utils/errors.rs | 2 +- src/utils/mod.rs | 2 +- 11 files changed, 55 insertions(+), 42 deletions(-) diff --git a/src/api/routes/paste.rs b/src/api/routes/paste.rs index eb8b61a..bbe8aef 100644 --- a/src/api/routes/paste.rs +++ b/src/api/routes/paste.rs @@ -1,18 +1,28 @@ use std::ops::DerefMut; use diesel::result::Error; -use rocket::{http::{Cookies, Status}, response::status, Rocket}; use rocket::response::status::Custom; +use rocket::{ + http::{Cookies, Status}, + response::status, + Rocket, +}; use rocket_contrib::json::Json; use serde_json::Value; -use crate::{api::catchers::{forbidden, internal_server_error, not_found, unprocessable_entity}, utils::domain::get_domain}; use crate::api::guards::db::DbConn; -use crate::core::paste::{entity::Paste, service::{create_paste, fetch_paste, update_paste}}; +use crate::core::paste::{ + entity::Paste, + service::{create_paste, fetch_paste, update_paste}, +}; +use crate::core::users::entity::User; use crate::core::users::service::{create_or_fetch_user, fetch_user}; use crate::utils::phonetic_key; use crate::utils::users::get_session_id; -use crate::core::users::entity::User; +use crate::{ + api::catchers::{forbidden, internal_server_error, not_found, unprocessable_entity}, + utils::domain::get_domain, +}; #[post("/", data = "")] fn create(mut paste: Json, conn: DbConn, mut ck: Cookies) -> Custom> { @@ -53,12 +63,10 @@ fn fetch(id: String, conn: DbConn, mut ck: Cookies) -> Custom> { let r_user = fetch_user(user_id, &conn); let user = match r_user { Ok(user) => user, - Err(e) => { - match e.downcast_ref::() { - Some(Error::NotFound) => User::new(), - _ => return internal_server_error(), - } - } + Err(e) => match e.downcast_ref::() { + Some(Error::NotFound) => User::new(), + _ => return internal_server_error(), + }, }; let paste = match fetch_paste(id, &conn) { @@ -73,17 +81,20 @@ fn fetch(id: String, conn: DbConn, mut ck: Cookies) -> Custom> { let belongs_to = paste.belongs_to.as_ref().unwrap(); - return if user.id == *belongs_to { - Custom(Status::Ok, Json(json!({ - "id": paste.id, - "belongs_to": *belongs_to, - "is_url": paste.is_url.unwrap(), - "content": paste.content, - "is_owner": true - }))) + if user.id == *belongs_to { + Custom( + Status::Ok, + Json(json!({ + "id": paste.id, + "belongs_to": *belongs_to, + "is_url": paste.is_url.unwrap(), + "content": paste.content, + "is_owner": true + })), + ) } else { Custom(Status::Ok, Json(json!(paste))) - }; + } } #[patch("/", data = "")] @@ -107,7 +118,7 @@ fn update(mut paste: Json, conn: DbConn, mut ck: Cookies) -> Custom paste.belongs_to, - Err(_) => return internal_server_error() + Err(_) => return internal_server_error(), }; if new_paste.belongs_to.is_some() { @@ -116,9 +127,9 @@ fn update(mut paste: Json, conn: DbConn, mut ck: Cookies) -> Custom status::Custom( Status::Created, Json(json!({ - "msg": "Successfully created paste", - "paste_id": new_paste.id - })), + "msg": "Successfully created paste", + "paste_id": new_paste.id + })), ), Err(_) => internal_server_error(), } @@ -149,6 +160,6 @@ fn anonymous(input: String, conn: DbConn) -> String { pub fn fuel(rocket: Rocket) -> Rocket { rocket - .mount("/api/paste", routes![create, fetch, update]) - .mount("/", routes![anonymous]) + .mount("/api/paste", routes![create, fetch, update]) + .mount("/", routes![anonymous]) } diff --git a/src/api/routes/user.rs b/src/api/routes/user.rs index b30cae5..60b02ef 100644 --- a/src/api/routes/user.rs +++ b/src/api/routes/user.rs @@ -1,11 +1,7 @@ use anyhow::Result; -use rocket::{ - http::Status, - response::status, - Rocket, -}; use rocket::http::Cookies; use rocket::response::status::Custom; +use rocket::{http::Status, response::status, Rocket}; use rocket_contrib::json::Json; use serde_json::Value; diff --git a/src/core/paste/entity.rs b/src/core/paste/entity.rs index 28f4d75..abf5101 100644 --- a/src/core/paste/entity.rs +++ b/src/core/paste/entity.rs @@ -1,7 +1,7 @@ use crate::schema::pastes; -#[table_name = "pastes"] #[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)] +#[table_name = "pastes"] pub struct Paste { pub id: Option, pub belongs_to: Option, diff --git a/src/core/paste/postgres.rs b/src/core/paste/postgres.rs index 34df526..f9da1d6 100644 --- a/src/core/paste/postgres.rs +++ b/src/core/paste/postgres.rs @@ -16,7 +16,9 @@ pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result { pub fn update_paste(paste: &Paste, conn: &PgConnection) -> Result { use crate::schema::pastes::dsl::*; - let updated_user = diesel::update(pastes.filter(id.eq(paste.id.as_ref().unwrap()))).set(paste).get_result(conn)?; + let updated_user = diesel::update(pastes.filter(id.eq(paste.id.as_ref().unwrap()))) + .set(paste) + .get_result(conn)?; Ok(updated_user) } diff --git a/src/core/users/entity.rs b/src/core/users/entity.rs index 484ae38..cc8c3bc 100644 --- a/src/core/users/entity.rs +++ b/src/core/users/entity.rs @@ -1,7 +1,7 @@ use crate::schema::users; +#[derive(Default, AsChangeset, Serialize, Deserialize, Queryable, Insertable)] #[table_name = "users"] -#[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)] pub struct User { pub id: String, pub username: Option, diff --git a/src/core/users/service.rs b/src/core/users/service.rs index 2f593b2..469d9c2 100644 --- a/src/core/users/service.rs +++ b/src/core/users/service.rs @@ -19,7 +19,7 @@ pub fn create_or_fetch_user(id: String, conn: &PgConnection) -> Result { Err(err) => match err.downcast_ref::() { Some(Error::NotFound) => { let new_user = User { - id: id.clone(), + id, username: None, password: None, activated: Some(false), diff --git a/src/main.rs b/src/main.rs index 326151a..2985e15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,8 +15,6 @@ extern crate serde_json; extern crate slog; use slog::{Drain, Logger}; -use slog_async; -use slog_term; pub mod api; pub mod core; @@ -40,7 +38,7 @@ fn main() { let drain = slog_async::Async::new(drain).build().fuse(); let logger = slog::Logger::root(drain, o!()); - // run_migrations(&logger); + run_migrations(&logger); let mut rocket = rocket::ignite(); diff --git a/src/utils/db.rs b/src/utils/db.rs index dc20059..19e698a 100644 --- a/src/utils/db.rs +++ b/src/utils/db.rs @@ -7,7 +7,10 @@ pub type Pool = r2d2::Pool>; pub fn pool() -> Pool { let manager = ConnectionManager::::new(database_url()); - let pool_size = env::var("KATBIN_POOL_SIZE").unwrap().parse::().expect("pool size"); + let pool_size = env::var("KATBIN_POOL_SIZE") + .unwrap() + .parse::() + .expect("pool size"); Pool::builder().max_size(pool_size).build(manager).unwrap() } diff --git a/src/utils/domain.rs b/src/utils/domain.rs index 7686d6e..385f568 100644 --- a/src/utils/domain.rs +++ b/src/utils/domain.rs @@ -1,5 +1,8 @@ use std::env; pub fn get_domain() -> String { - env::var("KATBIN_DOMAIN_NAME").unwrap().parse::().expect("domain name") -} \ No newline at end of file + env::var("KATBIN_DOMAIN_NAME") + .unwrap() + .parse::() + .expect("domain name") +} diff --git a/src/utils/errors.rs b/src/utils/errors.rs index e546037..be33794 100644 --- a/src/utils/errors.rs +++ b/src/utils/errors.rs @@ -64,7 +64,7 @@ impl Error { } fn set_msg(mut self) -> Self { - self.msg = match self.code.clone() { + self.msg = match self.code { ErrorCode::InvalidCredentials => "invalid credentials were provided".to_string(), ErrorCode::MultipleAuthToken => { "multiple authorization tokens were provided".to_string() diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 80e02cf..adfa174 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,5 +1,5 @@ pub mod db; +pub mod domain; pub mod errors; pub mod phonetic_key; pub mod users; -pub mod domain; -- 2.45.2 From f80f467408596d518ba35822776aee44eb652863 Mon Sep 17 00:00:00 2001 From: supercmmetry Date: Thu, 29 Jul 2021 13:30:44 +0530 Subject: [PATCH 3/3] fix(curl): use /api instead of / Signed-off-by: supercmmetry --- src/api/routes/paste.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/routes/paste.rs b/src/api/routes/paste.rs index bbe8aef..bdf02eb 100644 --- a/src/api/routes/paste.rs +++ b/src/api/routes/paste.rs @@ -161,5 +161,5 @@ fn anonymous(input: String, conn: DbConn) -> String { pub fn fuel(rocket: Rocket) -> Rocket { rocket .mount("/api/paste", routes![create, fetch, update]) - .mount("/", routes![anonymous]) + .mount("/api", routes![anonymous]) } -- 2.45.2