features/curl #6

Closed
supercmmetry wants to merge 3 commits from features/curl into master
3 changed files with 28 additions and 3 deletions
Showing only changes of commit 0182fa749e - Show all commits

View File

@ -6,7 +6,7 @@ use rocket::response::status::Custom;
use rocket_contrib::json::Json; use rocket_contrib::json::Json;
use serde_json::Value; 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::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::service::{create_or_fetch_user, fetch_user}; use crate::core::users::service::{create_or_fetch_user, fetch_user};
@ -130,6 +130,25 @@ fn update(mut paste: Json<Paste>, conn: DbConn, mut ck: Cookies) -> Custom<Json<
} }
} }
pub fn fuel(rocket: Rocket) -> Rocket { #[post("/", data = "<input>")]
rocket.mount("/api/paste", routes![create, fetch, update]) 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])
} }

5
src/utils/domain.rs Normal file
View File

@ -0,0 +1,5 @@
use std::env;
pub fn get_domain() -> String {
env::var("KATBIN_DOMAIN_NAME").unwrap().parse::<String>().expect("domain name")
}

View File

@ -2,3 +2,4 @@ pub mod db;
pub mod errors; pub mod errors;
pub mod phonetic_key; pub mod phonetic_key;
pub mod users; pub mod users;
pub mod domain;