features/curl #6
@ -1,18 +1,28 @@
|
|||||||
use std::ops::DerefMut;
|
use std::ops::DerefMut;
|
||||||
|
|
||||||
use diesel::result::Error;
|
use diesel::result::Error;
|
||||||
use rocket::{http::{Cookies, Status}, response::status, Rocket};
|
|
||||||
use rocket::response::status::Custom;
|
use rocket::response::status::Custom;
|
||||||
|
use rocket::{
|
||||||
|
http::{Cookies, Status},
|
||||||
|
response::status,
|
||||||
|
Rocket,
|
||||||
|
};
|
||||||
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::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::entity::User;
|
||||||
use crate::core::users::service::{create_or_fetch_user, fetch_user};
|
use crate::core::users::service::{create_or_fetch_user, fetch_user};
|
||||||
use crate::utils::phonetic_key;
|
use crate::utils::phonetic_key;
|
||||||
use crate::utils::users::get_session_id;
|
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 = "<paste>")]
|
#[post("/", data = "<paste>")]
|
||||||
fn create(mut paste: Json<Paste>, conn: DbConn, mut ck: Cookies) -> Custom<Json<Value>> {
|
fn create(mut paste: Json<Paste>, conn: DbConn, mut ck: Cookies) -> Custom<Json<Value>> {
|
||||||
@ -53,12 +63,10 @@ fn fetch(id: String, conn: DbConn, mut ck: Cookies) -> Custom<Json<Value>> {
|
|||||||
let r_user = fetch_user(user_id, &conn);
|
let r_user = fetch_user(user_id, &conn);
|
||||||
let user = match r_user {
|
let user = match r_user {
|
||||||
Ok(user) => user,
|
Ok(user) => user,
|
||||||
Err(e) => {
|
Err(e) => match e.downcast_ref::<Error>() {
|
||||||
match e.downcast_ref::<Error>() {
|
Some(Error::NotFound) => User::new(),
|
||||||
Some(Error::NotFound) => User::new(),
|
_ => return internal_server_error(),
|
||||||
_ => return internal_server_error(),
|
},
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let paste = match fetch_paste(id, &conn) {
|
let paste = match fetch_paste(id, &conn) {
|
||||||
@ -73,17 +81,20 @@ fn fetch(id: String, conn: DbConn, mut ck: Cookies) -> Custom<Json<Value>> {
|
|||||||
|
|
||||||
let belongs_to = paste.belongs_to.as_ref().unwrap();
|
let belongs_to = paste.belongs_to.as_ref().unwrap();
|
||||||
|
|
||||||
return if user.id == *belongs_to {
|
if user.id == *belongs_to {
|
||||||
Custom(Status::Ok, Json(json!({
|
Custom(
|
||||||
"id": paste.id,
|
Status::Ok,
|
||||||
"belongs_to": *belongs_to,
|
Json(json!({
|
||||||
"is_url": paste.is_url.unwrap(),
|
"id": paste.id,
|
||||||
"content": paste.content,
|
"belongs_to": *belongs_to,
|
||||||
"is_owner": true
|
"is_url": paste.is_url.unwrap(),
|
||||||
})))
|
"content": paste.content,
|
||||||
|
"is_owner": true
|
||||||
|
})),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
Custom(Status::Ok, Json(json!(paste)))
|
Custom(Status::Ok, Json(json!(paste)))
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[patch("/", data = "<paste>")]
|
#[patch("/", data = "<paste>")]
|
||||||
@ -107,7 +118,7 @@ fn update(mut paste: Json<Paste>, conn: DbConn, mut ck: Cookies) -> Custom<Json<
|
|||||||
|
|
||||||
new_paste.belongs_to = match fetch_paste(new_paste.id.as_ref().unwrap().clone(), &conn) {
|
new_paste.belongs_to = match fetch_paste(new_paste.id.as_ref().unwrap().clone(), &conn) {
|
||||||
Ok(paste) => paste.belongs_to,
|
Ok(paste) => paste.belongs_to,
|
||||||
Err(_) => return internal_server_error()
|
Err(_) => return internal_server_error(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if new_paste.belongs_to.is_some() {
|
if new_paste.belongs_to.is_some() {
|
||||||
@ -116,9 +127,9 @@ fn update(mut paste: Json<Paste>, conn: DbConn, mut ck: Cookies) -> Custom<Json<
|
|||||||
Ok(_) => status::Custom(
|
Ok(_) => status::Custom(
|
||||||
Status::Created,
|
Status::Created,
|
||||||
Json(json!({
|
Json(json!({
|
||||||
"msg": "Successfully created paste",
|
"msg": "Successfully created paste",
|
||||||
"paste_id": new_paste.id
|
"paste_id": new_paste.id
|
||||||
})),
|
})),
|
||||||
),
|
),
|
||||||
Err(_) => internal_server_error(),
|
Err(_) => internal_server_error(),
|
||||||
}
|
}
|
||||||
@ -130,6 +141,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("/api", routes![anonymous])
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,7 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use rocket::{
|
|
||||||
http::Status,
|
|
||||||
response::status,
|
|
||||||
Rocket,
|
|
||||||
};
|
|
||||||
use rocket::http::Cookies;
|
use rocket::http::Cookies;
|
||||||
use rocket::response::status::Custom;
|
use rocket::response::status::Custom;
|
||||||
|
use rocket::{http::Status, response::status, Rocket};
|
||||||
use rocket_contrib::json::Json;
|
use rocket_contrib::json::Json;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::schema::pastes;
|
use crate::schema::pastes;
|
||||||
|
|
||||||
#[table_name = "pastes"]
|
|
||||||
#[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)]
|
#[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)]
|
||||||
|
#[table_name = "pastes"]
|
||||||
pub struct Paste {
|
pub struct Paste {
|
||||||
pub id: Option<String>,
|
pub id: Option<String>,
|
||||||
pub belongs_to: Option<String>,
|
pub belongs_to: Option<String>,
|
||||||
|
@ -16,7 +16,9 @@ pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result<usize> {
|
|||||||
|
|
||||||
pub fn update_paste(paste: &Paste, conn: &PgConnection) -> Result<Paste> {
|
pub fn update_paste(paste: &Paste, conn: &PgConnection) -> Result<Paste> {
|
||||||
use crate::schema::pastes::dsl::*;
|
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)
|
Ok(updated_user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::schema::users;
|
use crate::schema::users;
|
||||||
|
|
||||||
|
#[derive(Default, AsChangeset, Serialize, Deserialize, Queryable, Insertable)]
|
||||||
#[table_name = "users"]
|
#[table_name = "users"]
|
||||||
#[derive(AsChangeset, Serialize, Deserialize, Queryable, Insertable)]
|
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub username: Option<String>,
|
pub username: Option<String>,
|
||||||
|
@ -19,7 +19,7 @@ pub fn create_or_fetch_user(id: String, conn: &PgConnection) -> Result<User> {
|
|||||||
Err(err) => match err.downcast_ref::<Error>() {
|
Err(err) => match err.downcast_ref::<Error>() {
|
||||||
Some(Error::NotFound) => {
|
Some(Error::NotFound) => {
|
||||||
let new_user = User {
|
let new_user = User {
|
||||||
id: id.clone(),
|
id,
|
||||||
username: None,
|
username: None,
|
||||||
password: None,
|
password: None,
|
||||||
activated: Some(false),
|
activated: Some(false),
|
||||||
|
@ -15,8 +15,6 @@ extern crate serde_json;
|
|||||||
extern crate slog;
|
extern crate slog;
|
||||||
|
|
||||||
use slog::{Drain, Logger};
|
use slog::{Drain, Logger};
|
||||||
use slog_async;
|
|
||||||
use slog_term;
|
|
||||||
|
|
||||||
pub mod api;
|
pub mod api;
|
||||||
pub mod core;
|
pub mod core;
|
||||||
@ -40,7 +38,7 @@ fn main() {
|
|||||||
let drain = slog_async::Async::new(drain).build().fuse();
|
let drain = slog_async::Async::new(drain).build().fuse();
|
||||||
let logger = slog::Logger::root(drain, o!());
|
let logger = slog::Logger::root(drain, o!());
|
||||||
|
|
||||||
// run_migrations(&logger);
|
run_migrations(&logger);
|
||||||
|
|
||||||
let mut rocket = rocket::ignite();
|
let mut rocket = rocket::ignite();
|
||||||
|
|
||||||
|
@ -7,7 +7,10 @@ pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
|
|||||||
|
|
||||||
pub fn pool() -> Pool {
|
pub fn pool() -> Pool {
|
||||||
let manager = ConnectionManager::<PgConnection>::new(database_url());
|
let manager = ConnectionManager::<PgConnection>::new(database_url());
|
||||||
let pool_size = env::var("KATBIN_POOL_SIZE").unwrap().parse::<u32>().expect("pool size");
|
let pool_size = env::var("KATBIN_POOL_SIZE")
|
||||||
|
.unwrap()
|
||||||
|
.parse::<u32>()
|
||||||
|
.expect("pool size");
|
||||||
Pool::builder().max_size(pool_size).build(manager).unwrap()
|
Pool::builder().max_size(pool_size).build(manager).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
8
src/utils/domain.rs
Normal file
8
src/utils/domain.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
use std::env;
|
||||||
|
|
||||||
|
pub fn get_domain() -> String {
|
||||||
|
env::var("KATBIN_DOMAIN_NAME")
|
||||||
|
.unwrap()
|
||||||
|
.parse::<String>()
|
||||||
|
.expect("domain name")
|
||||||
|
}
|
@ -64,7 +64,7 @@ impl Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn set_msg(mut self) -> Self {
|
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::InvalidCredentials => "invalid credentials were provided".to_string(),
|
||||||
ErrorCode::MultipleAuthToken => {
|
ErrorCode::MultipleAuthToken => {
|
||||||
"multiple authorization tokens were provided".to_string()
|
"multiple authorization tokens were provided".to_string()
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
pub mod db;
|
pub mod db;
|
||||||
|
pub mod domain;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
pub mod phonetic_key;
|
pub mod phonetic_key;
|
||||||
pub mod users;
|
pub mod users;
|
||||||
|
Loading…
Reference in New Issue
Block a user