diff --git a/Cargo.toml b/Cargo.toml index b87b1b8..ad68320 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,4 @@ serde = { version = "1.0", features = ["derive"] } askama = "0.10" askama-filters = { version = "0.1.3", features = ["chrono"] } chrono = "0.4.19" -rand = "0.8.5" \ No newline at end of file +rand = "0.8.5" diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..fc10d3a --- /dev/null +++ b/README.MD @@ -0,0 +1,20 @@ +# MicroBin + +![Screenshot](git/index.png) + +MicroBin is a super tiny and simple self hosted pastebin app written in Rust. The executable is around 6MB and it uses 2MB memory (plus your pastas). + +Features: +- No CSS or JS, super lightweight and simple +- Animal names instead of random numbers for pasta identifiers +- Automatically expiring pastas +- Never expiring pastas +- Listing and manually removing pastas + +Needed improvements: +- Persisting pastas on disk (currently they are lost on restart) +- Removing pasta after N reads +- File uploads +- URL shortening + +![Screenshot](git/pasta.png) \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9ac8cb3..1d08c81 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,7 @@ async fn create(data: web::Data, pasta: web::Form) -> i } as i64; let expiration = match innerPasta.expiration.as_str() { - "firstread" => 1, + "1min" => timenow + 60, "10min" => timenow + 60 * 10, "1hour" => timenow + 60 * 60, "24hour" => timenow + 60 * 60 * 24, @@ -78,9 +78,11 @@ async fn create(data: web::Data, pasta: web::Form) -> i #[get("/pasta/{id}")] async fn getpasta(data: web::Data, id: web::Path) -> HttpResponse { - let pastas = data.pastas.lock().unwrap(); + let mut pastas = data.pastas.lock().unwrap(); let id = to_u64(&*id.into_inner()); + remove_expired(&mut pastas); + for pasta in pastas.iter() { if pasta.id == id { return HttpResponse::Found().content_type("text/html").body(PastaTemplate { pasta }.render().unwrap()); @@ -92,9 +94,11 @@ async fn getpasta(data: web::Data, id: web::Path) -> HttpRespo #[get("/rawpasta/{id}")] async fn getrawpasta(data: web::Data, id: web::Path) -> String { - let pastas = data.pastas.lock().unwrap(); + let mut pastas = data.pastas.lock().unwrap(); let id = to_u64(&*id.into_inner()); + remove_expired(&mut pastas); + for pasta in pastas.iter() { if pasta.id == id { return pasta.content.to_owned(); @@ -109,6 +113,8 @@ async fn remove(data: web::Data, id: web::Path) -> HttpRespons let mut pastas = data.pastas.lock().unwrap(); let id = to_u64(&*id.into_inner()); + remove_expired(&mut pastas); + for (i, pasta) in pastas.iter().enumerate() { if pasta.id == id { pastas.remove(i); @@ -123,6 +129,8 @@ async fn remove(data: web::Data, id: web::Path) -> HttpRespons async fn list(data: web::Data) -> HttpResponse { let mut pastas = data.pastas.lock().unwrap(); + remove_expired(&mut pastas); + HttpResponse::Found().content_type("text/html").body(PastaListTemplate { pastas: &pastas }.render().unwrap()) } @@ -135,3 +143,14 @@ async fn main() -> std::io::Result<()> { HttpServer::new(move || App::new().app_data(data.clone()).service(index).service(create).service(getpasta).service(getrawpasta).service(remove).service(list) ).bind("127.0.0.1:8080")?.run().await } + +fn remove_expired(pastas: &mut Vec) { + let timenow: i64 = match SystemTime::now().duration_since(UNIX_EPOCH) { + Ok(n) => n.as_secs(), + Err(_) => panic!("SystemTime before UNIX EPOCH!"), + } as i64; + + pastas.retain(|p| { + p.expiration == 0 || p.expiration > timenow + }); +} diff --git a/templates/header.html b/templates/header.html index 1a093e3..1434ed7 100644 --- a/templates/header.html +++ b/templates/header.html @@ -3,7 +3,12 @@ MicroBin - +
diff --git a/templates/index.html b/templates/index.html index eda0fdb..b75866b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,7 +4,7 @@


- + {% include "footer.html" %} \ No newline at end of file diff --git a/templates/pasta.html b/templates/pasta.html index 29e37fe..4f58c08 100644 --- a/templates/pasta.html +++ b/templates/pasta.html @@ -1,5 +1,5 @@ {% include "header.html" %} -Raw Pasta +Raw Pasta
 {{pasta}}
 
diff --git a/templates/pastalist.html b/templates/pastalist.html index d943656..741f1e7 100644 --- a/templates/pastalist.html +++ b/templates/pastalist.html @@ -1,4 +1,11 @@ {% include "header.html" %} + + +{% if pastas.is_empty() %} +

+ No pastas yet. 😔 Create one here. +

+{%- else %} {% endfor %}
@@ -31,10 +38,5 @@
- -{% if pastas.is_empty() %} -

- No Pastas :-( -

{%- endif %} {% include "footer.html" %} \ No newline at end of file