From 19022e7cb5bdb4837a0372cca0058ec6e6ac21a3 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Sat, 12 Mar 2022 14:07:33 +0100 Subject: [PATCH] formatting --- backend/src/api.rs | 10 +-- backend/src/client.rs | 12 +-- backend/src/note/model.rs | 20 ++--- backend/src/note/routes.rs | 162 +++++++++++++++++------------------ backend/src/size.rs | 18 ++-- backend/src/status/model.rs | 10 +-- backend/src/status/routes.rs | 16 ++-- backend/src/store.rs | 38 ++++---- 8 files changed, 143 insertions(+), 143 deletions(-) diff --git a/backend/src/api.rs b/backend/src/api.rs index e178478..1ff86de 100644 --- a/backend/src/api.rs +++ b/backend/src/api.rs @@ -4,9 +4,9 @@ use crate::note; use crate::status; pub fn init(cfg: &mut web::ServiceConfig) { - cfg.service( - web::scope("/api") - .service(note::init()) - .service(status::init()), - ); + cfg.service( + web::scope("/api") + .service(note::init()) + .service(status::init()), + ); } diff --git a/backend/src/client.rs b/backend/src/client.rs index 9d06897..2131e1c 100644 --- a/backend/src/client.rs +++ b/backend/src/client.rs @@ -2,13 +2,13 @@ use actix_files::{Files, NamedFile}; use actix_web::{web, Result}; pub fn init(cfg: &mut web::ServiceConfig) { - cfg.service( - Files::new("/", "./frontend/build") - .index_file("index.html") - .use_etag(true), - ); + cfg.service( + Files::new("/", "./frontend/build") + .index_file("index.html") + .use_etag(true), + ); } pub async fn index() -> Result { - Ok(NamedFile::open("./frontend/build/index.html")?) + Ok(NamedFile::open("./frontend/build/index.html")?) } diff --git a/backend/src/note/model.rs b/backend/src/note/model.rs index 398eaaa..8dfdfaf 100644 --- a/backend/src/note/model.rs +++ b/backend/src/note/model.rs @@ -4,10 +4,10 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Clone)] pub struct Note { - pub meta: String, - pub contents: String, - pub views: Option, - pub expiration: Option, + pub meta: String, + pub contents: String, + pub views: Option, + pub expiration: Option, } #[derive(Serialize, Deserialize, Clone)] @@ -15,13 +15,13 @@ pub struct NoteInfo {} #[derive(Serialize, Deserialize, Clone)] pub struct NotePublic { - pub meta: String, - pub contents: String, + pub meta: String, + pub contents: String, } pub fn generate_id() -> String { - let mut id: [u8; 32] = [0; 32]; - let sr = ring::rand::SystemRandom::new(); - let _ = sr.fill(&mut id); - return bs62::encode_data(&id); + let mut id: [u8; 32] = [0; 32]; + let sr = ring::rand::SystemRandom::new(); + let _ = sr.fill(&mut id); + return bs62::encode_data(&id); } diff --git a/backend/src/note/routes.rs b/backend/src/note/routes.rs index 063d5b6..9ff5f06 100644 --- a/backend/src/note/routes.rs +++ b/backend/src/note/routes.rs @@ -7,118 +7,118 @@ use crate::note::{generate_id, Note, NoteInfo, NotePublic}; use crate::store; pub fn now() -> u32 { - SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .unwrap() - .as_secs() as u32 + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs() as u32 } #[derive(Serialize, Deserialize)] struct NotePath { - id: String, + id: String, } #[get("/{id}")] async fn one(path: web::Path) -> impl Responder { - let p = path.into_inner(); - let note = store::get(&p.id); - match note { - None => return HttpResponse::NotFound().finish(), - Some(_) => return HttpResponse::Ok().json(NoteInfo {}), - } + let p = path.into_inner(); + let note = store::get(&p.id); + match note { + None => return HttpResponse::NotFound().finish(), + Some(_) => return HttpResponse::Ok().json(NoteInfo {}), + } } #[derive(Serialize, Deserialize)] struct CreateResponse { - id: String, + id: String, } #[post("/")] async fn create(note: web::Json) -> impl Responder { - let mut n = note.into_inner(); - let id = generate_id(); - let bad_req = HttpResponse::BadRequest().finish(); - if n.views == None && n.expiration == None { - return bad_req; - } - if !*config::ALLOW_ADVANCED { - n.views = Some(1); - n.expiration = None; - } - match n.views { - Some(v) => { - if v > *config::MAX_VIEWS { + let mut n = note.into_inner(); + let id = generate_id(); + let bad_req = HttpResponse::BadRequest().finish(); + if n.views == None && n.expiration == None { return bad_req; - } - n.expiration = None; // views overrides expiration } - _ => {} - } - match n.expiration { - Some(e) => { - if e > *config::MAX_EXPIRATION { - return bad_req; - } - let expiration = now() + (e * 60); - n.expiration = Some(expiration); + if !*config::ALLOW_ADVANCED { + n.views = Some(1); + n.expiration = None; } - _ => {} - } - store::set(&id.clone(), &n.clone()); - return HttpResponse::Ok().json(CreateResponse { id: id }); + match n.views { + Some(v) => { + if v > *config::MAX_VIEWS { + return bad_req; + } + n.expiration = None; // views overrides expiration + } + _ => {} + } + match n.expiration { + Some(e) => { + if e > *config::MAX_EXPIRATION { + return bad_req; + } + let expiration = now() + (e * 60); + n.expiration = Some(expiration); + } + _ => {} + } + store::set(&id.clone(), &n.clone()); + return HttpResponse::Ok().json(CreateResponse { id: id }); } #[delete("/{id}")] async fn delete(path: web::Path) -> impl Responder { - let p = path.into_inner(); - let note = store::get(&p.id); - match note { - None => return HttpResponse::NotFound().finish(), - Some(note) => { - let mut changed = note.clone(); - if changed.views == None && changed.expiration == None { - return HttpResponse::BadRequest().finish(); - } - match changed.views { - Some(v) => { - changed.views = Some(v - 1); - let id = p.id.clone(); - if v <= 1 { - store::del(&id); - } else { - store::set(&id, &changed.clone()); - } - } - _ => {} - } + let p = path.into_inner(); + let note = store::get(&p.id); + match note { + None => return HttpResponse::NotFound().finish(), + Some(note) => { + let mut changed = note.clone(); + if changed.views == None && changed.expiration == None { + return HttpResponse::BadRequest().finish(); + } + match changed.views { + Some(v) => { + changed.views = Some(v - 1); + let id = p.id.clone(); + if v <= 1 { + store::del(&id); + } else { + store::set(&id, &changed.clone()); + } + } + _ => {} + } - let n = now(); - match changed.expiration { - Some(e) => { - if e < n { - store::del(&p.id.clone()); - return HttpResponse::BadRequest().finish(); - } + let n = now(); + match changed.expiration { + Some(e) => { + if e < n { + store::del(&p.id.clone()); + return HttpResponse::BadRequest().finish(); + } + } + _ => {} + } + return HttpResponse::Ok().json(NotePublic { + contents: changed.contents, + meta: changed.meta, + }); } - _ => {} - } - return HttpResponse::Ok().json(NotePublic { - contents: changed.contents, - meta: changed.meta, - }); } - } } #[derive(Serialize, Deserialize)] struct Status { - version: String, - max_size: usize, + version: String, + max_size: usize, } pub fn init() -> Scope { - web::scope("/notes") - .service(one) - .service(create) - .service(delete) + web::scope("/notes") + .service(one) + .service(create) + .service(delete) } diff --git a/backend/src/size.rs b/backend/src/size.rs index e17eb9d..3fd3f23 100644 --- a/backend/src/size.rs +++ b/backend/src/size.rs @@ -3,16 +3,16 @@ use byte_unit::Byte; use mime; lazy_static! { - pub static ref LIMIT: usize = - Byte::from_str(std::env::var("SIZE_LIMIT").unwrap_or("1 KiB".to_string())) - .unwrap() - .get_bytes() as usize; + pub static ref LIMIT: usize = + Byte::from_str(std::env::var("SIZE_LIMIT").unwrap_or("1 KiB".to_string())) + .unwrap() + .get_bytes() as usize; } pub fn init(cfg: &mut web::ServiceConfig) { - let json = web::JsonConfig::default().limit(*LIMIT); - let plain = web::PayloadConfig::default() - .limit(*LIMIT) - .mimetype(mime::STAR_STAR); - cfg.app_data(json).app_data(plain); + let json = web::JsonConfig::default().limit(*LIMIT); + let plain = web::PayloadConfig::default() + .limit(*LIMIT) + .mimetype(mime::STAR_STAR); + cfg.app_data(json).app_data(plain); } diff --git a/backend/src/status/model.rs b/backend/src/status/model.rs index 68c19bb..baadedb 100644 --- a/backend/src/status/model.rs +++ b/backend/src/status/model.rs @@ -2,9 +2,9 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize)] pub struct Status { - pub version: String, - pub max_size: u32, - pub max_views: u32, - pub max_expiration: u32, - pub allow_advanced: bool, + pub version: String, + pub max_size: u32, + pub max_views: u32, + pub max_expiration: u32, + pub allow_advanced: bool, } diff --git a/backend/src/status/routes.rs b/backend/src/status/routes.rs index 4d3591b..ce7144e 100644 --- a/backend/src/status/routes.rs +++ b/backend/src/status/routes.rs @@ -5,15 +5,15 @@ use crate::status::Status; #[get("/")] async fn get_status() -> impl Responder { - return HttpResponse::Ok().json(Status { - version: config::VERSION.to_string(), - max_size: *config::LIMIT, - max_views: *config::MAX_VIEWS, - max_expiration: *config::MAX_EXPIRATION, - allow_advanced: *config::ALLOW_ADVANCED, - }); + return HttpResponse::Ok().json(Status { + version: config::VERSION.to_string(), + max_size: *config::LIMIT, + max_views: *config::MAX_VIEWS, + max_expiration: *config::MAX_EXPIRATION, + allow_advanced: *config::ALLOW_ADVANCED, + }); } pub fn init() -> Scope { - web::scope("/status").service(get_status) + web::scope("/status").service(get_status) } diff --git a/backend/src/store.rs b/backend/src/store.rs index 4c09b13..1a16dbb 100644 --- a/backend/src/store.rs +++ b/backend/src/store.rs @@ -4,33 +4,33 @@ use crate::note::now; use crate::note::Note; lazy_static! { - static ref CLIENT: memcache::Client = memcache::connect(format!( - "memcache://{}?timeout=10&tcp_nodelay=true", - std::env::var("MEMCACHE").unwrap_or("127.0.0.1:11211".to_string()) - )) - .unwrap(); + static ref CLIENT: memcache::Client = memcache::connect(format!( + "memcache://{}?timeout=10&tcp_nodelay=true", + std::env::var("MEMCACHE").unwrap_or("127.0.0.1:11211".to_string()) + )) + .unwrap(); } pub fn set(id: &String, note: &Note) { - let serialized = serde_json::to_string(¬e.clone()).unwrap(); - let expiration: u32 = match note.expiration { - Some(e) => e - now(), - None => 0, - }; - CLIENT.set(id, serialized, expiration).unwrap(); + let serialized = serde_json::to_string(¬e.clone()).unwrap(); + let expiration: u32 = match note.expiration { + Some(e) => e - now(), + None => 0, + }; + CLIENT.set(id, serialized, expiration).unwrap(); } pub fn get(id: &String) -> Option { - let value: Option = CLIENT.get(&id).unwrap(); - match value { - None => return None, - Some(s) => { - let deserialize: Note = serde_json::from_str(&s).unwrap(); - return Some(deserialize); + let value: Option = CLIENT.get(&id).unwrap(); + match value { + None => return None, + Some(s) => { + let deserialize: Note = serde_json::from_str(&s).unwrap(); + return Some(deserialize); + } } - } } pub fn del(id: &String) { - CLIENT.delete(id).unwrap(); + CLIENT.delete(id).unwrap(); }