Move business logic out of main, use askama and bound the ENTRIES
This commit is contained in:
parent
3093b06b4e
commit
9d4b3ecafc
8 changed files with 156 additions and 578 deletions
45
src/io.rs
Normal file
45
src/io.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
extern crate gpw;
|
||||
extern crate linked_hash_map;
|
||||
|
||||
use linked_hash_map::LinkedHashMap;
|
||||
|
||||
use std::sync::RwLock;
|
||||
use std::env;
|
||||
use std::cell::RefCell;
|
||||
|
||||
lazy_static! {
|
||||
static ref ENTRIES: RwLock<LinkedHashMap<String, String>> = RwLock::new(LinkedHashMap::new());
|
||||
}
|
||||
|
||||
/// Ensures `ENTRIES` is less than the size of `BIN_BUFFER_SIZE`. If it isn't then
|
||||
/// `ENTRIES.len() - BIN_BUFFER_SIZE` elements will be popped off the front of the map.
|
||||
fn purge_old() {
|
||||
let entries_len = ENTRIES.read().unwrap().len();
|
||||
let buffer_size = env::var("BIN_BUFFER_SIZE").map(|f| f.parse::<usize>().unwrap()).unwrap_or(1000usize);
|
||||
|
||||
if entries_len > buffer_size {
|
||||
let to_remove = entries_len - buffer_size;
|
||||
|
||||
let mut entries = ENTRIES.write().unwrap();
|
||||
|
||||
for _ in 0..to_remove {
|
||||
entries.pop_front();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a randomly generated id, stores the given paste under that id and then returns the id.
|
||||
pub fn store_paste(content: String) -> String {
|
||||
thread_local!(static KEYGEN: RefCell<gpw::PasswordGenerator> = RefCell::new(gpw::PasswordGenerator::default()));
|
||||
let id = KEYGEN.with(|k| k.borrow_mut().next().unwrap());
|
||||
|
||||
purge_old();
|
||||
ENTRIES.write().unwrap().insert(id.clone(), content);
|
||||
|
||||
id
|
||||
}
|
||||
|
||||
/// Get a paste by id. Returns `None` if the paste doesn't exist.
|
||||
pub fn get_paste<'a>(id: &str) -> Option<String> {
|
||||
ENTRIES.read().unwrap().get(id).map(|f| f.clone())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue