gistlib
main.rsuse std::io::{Error, ErrorKind}; use actix_web::{web, App, HttpServer, HttpResponse, Responder}; use rand::distributions::Alphanumeric; use rand::{thread_rng, Rng}; use std::fs; async fn save_paste(data: web::Data<Vec<String>>, text: String) -> impl Responder { let id: String = thread_rng() .sample_iter(&Alphanumeric) .take(10) .map(char::from) .collect(); data.get_ref().write().unwrap().push(text.clone()); fs::write(format!("./pastes/{}.txt", &id), text) .unwrap_or_else(|_| Error::new(ErrorKind::Other, "Could not save paste")); HttpResponse::Ok().body(id) } async fn load_paste(data: web::Data<Vec<String>>, paste_id: web::Path<String>) -> impl Responder { match data.get_ref().read().unwrap().get_index(&paste_id).map(|x| x.clone()) { Some(text) => HttpResponse::Ok().body(text), None => HttpResponse::NotFound().body("Paste not found"), } } #[actix_web::main] async fn main() -> std::io::Result<()> { let data = web::Data::new(Arc::new(RwLock::new(Vec::new()))); fs::create_dir_all("./pastes").unwrap(); HttpServer::new(move || { App::new() .app_data(data.clone()) .route("/save", web::post().to(save_paste)) .route("/load/{paste_id}", web::get().to(load_paste)) }) .bind("127.0.0.1:8080")? .run() .await } 1380 chars45 lines
use std::io::{Error, ErrorKind}; use actix_web::{web, App, HttpServer, HttpResponse, Responder}; use rand::distributions::Alphanumeric; use rand::{thread_rng, Rng}; use std::fs; async fn save_paste(data: web::Data<Vec<String>>, text: String) -> impl Responder { let id: String = thread_rng() .sample_iter(&Alphanumeric) .take(10) .map(char::from) .collect(); data.get_ref().write().unwrap().push(text.clone()); fs::write(format!("./pastes/{}.txt", &id), text) .unwrap_or_else(|_| Error::new(ErrorKind::Other, "Could not save paste")); HttpResponse::Ok().body(id) } async fn load_paste(data: web::Data<Vec<String>>, paste_id: web::Path<String>) -> impl Responder { match data.get_ref().read().unwrap().get_index(&paste_id).map(|x| x.clone()) { Some(text) => HttpResponse::Ok().body(text), None => HttpResponse::NotFound().body("Paste not found"), } } #[actix_web::main] async fn main() -> std::io::Result<()> { let data = web::Data::new(Arc::new(RwLock::new(Vec::new()))); fs::create_dir_all("./pastes").unwrap(); HttpServer::new(move || { App::new() .app_data(data.clone()) .route("/save", web::post().to(save_paste)) .route("/load/{paste_id}", web::get().to(load_paste)) }) .bind("127.0.0.1:8080")? .run() .await }
gistlibby LogSnag