From 0182fa749ea4b5e5ef4502fa2e2624d430aa8e0f Mon Sep 17 00:00:00 2001 From: supercmmetry Date: Thu, 29 Jul 2021 11:34:00 +0530 Subject: [PATCH] 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;