mirror of
https://github.com/cupcakearmy/cryptgeon.git
synced 2024-12-22 00:06:28 +00:00
formatting
This commit is contained in:
parent
44f43dbc2c
commit
19022e7cb5
@ -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()),
|
||||
);
|
||||
}
|
||||
|
@ -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<NamedFile> {
|
||||
Ok(NamedFile::open("./frontend/build/index.html")?)
|
||||
Ok(NamedFile::open("./frontend/build/index.html")?)
|
||||
}
|
||||
|
@ -4,10 +4,10 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct Note {
|
||||
pub meta: String,
|
||||
pub contents: String,
|
||||
pub views: Option<u32>,
|
||||
pub expiration: Option<u32>,
|
||||
pub meta: String,
|
||||
pub contents: String,
|
||||
pub views: Option<u32>,
|
||||
pub expiration: Option<u32>,
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
|
@ -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<NotePath>) -> 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<Note>) -> 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<NotePath>) -> 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)
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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,
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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<Note> {
|
||||
let value: Option<String> = 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<String> = 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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user