Lemmy Coupou.fr
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
whoareu@lemmy.ca to Rust Programming@lemmy.mlEnglish · 5 months ago

I made a pastebin in 60 lines of Rust

lemmy.ca

external-link
message-square
0
link
fedilink
22
external-link

I made a pastebin in 60 lines of Rust

lemmy.ca

whoareu@lemmy.ca to Rust Programming@lemmy.mlEnglish · 5 months ago
message-square
0
link
fedilink
I made a minimal pastebin in 60 lines of Rust - Lemmy.ca
lemmy.ca
external-link
the code I have written isn’t very idiomatic or efficient. I am still new to Rust so I am learning things. I am amazed that I can write a pastebin in just 60 lines of Rust code. It’s awesome. I am thinking about deploying it on my server. any suggestions would be appreciated :) code: use axum::{extract::Path, routing::get, Router}; use std::fs::{read_to_string, File}; use std::io::prelude::*; use std::net::{TcpListener, TcpStream}; use std::str; const MAX_FILE_SIZE: usize = 1024 * 1024 * 10; static mut FILE_COUNT: usize = 0; fn handle_client(stream: &mut TcpStream) -> std::io::Result<()> { let mut buf = vec![0; 1024]; unsafe { let file_name = FILE_COUNT.to_string(); FILE_COUNT += 1; let mut file = File::create(file_name)?; let mut size: usize = 0; loop { let read_data = stream.read(&mut buf).unwrap(); size += read_data; if size >= MAX_FILE_SIZE { return Ok(()) } if read_data == 0 { return Ok(()); } stream.write_all(&buf[..read_data]).unwrap(); write!(file, "{}", str::from_utf8(&buf[..read_data]).unwrap())?; } } } async fn upload_handle() -> std::io::Result<()> { let listener = TcpListener::bind("127.0.0.1:8080")?; // accept connections and process them serially for stream in listener.incoming() { handle_client(&mut stream?)?; } Ok(()) } async fn handle(Path(id): Path) -> String { if let Ok(content) = read_to_string(id) { return content; } return String::from("ERROR: File not found"); } #[tokio::main] async fn main() { tokio::spawn(upload_handle()); let app = Router::new() .route("/", get(|| async { "Paste something in pastebin!" })) .route("/{id}", get(handle)); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await.unwrap(); }
alert-triangle
You must log in or register to comment.

Rust Programming@lemmy.ml

rust@lemmy.ml

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !rust@lemmy.ml
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 1 user / day
  • 25 users / week
  • 86 users / month
  • 606 users / 6 months
  • 1 local subscriber
  • 8.77K subscribers
  • 468 Posts
  • 1.28K Comments
  • Modlog
  • mods:
  • Nutomic@lemmy.ml
  • Joe@lemmy.ml
  • AgreeableLandscape@lemmy.ml
  • BE: 0.19.11
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org