wow, never thought of this! neat!
wow, never thought of this! neat!
Thank you I was looking for exactly something like this!
yeah sure! I will make a blog post on Lemmy or somewhere else once I get some free time :)
it shouldn’t be an issue because I will be running it inside a chroot. I might use UUID
though.
Thank you very much!
wow, now it reduced to only 41 lines of code, that’s nice :D
yeah, you are right, I don’t need mut
. here is the improved version:
use axum::{extract::Path, routing::get, routing::post, Router};
use std::fs::{read_to_string, File};
use std::io::prelude::*;
use std::sync::atomic::{AtomicUsize, Ordering};
const MAX_FILE_SIZE: usize = 1024 * 1024 * 10;
static FILE_COUNT: AtomicUsize = AtomicUsize::new(0);
async fn handle(Path(id): Path<String>) -> String {
match read_to_string(id) {
Ok(content) => content,
Err(e) => e.to_string(),
}
}
async fn submit_handle(bytes: String) -> String {
dbg!(&bytes);
if bytes.len() > MAX_FILE_SIZE {
// Don't store the file if it exceeds max size
return String::from("ERROR: max size exceeded");
}
let path = FILE_COUNT.load(Ordering::Relaxed);
FILE_COUNT.fetch_add(1, Ordering::SeqCst);
let mut output = File::create(path.to_string()).unwrap();
write!(output, "{}", bytes).unwrap();
let mut url = String::from("http://localhost:3000/");
url.push_str(&path.to_string());
return url;
}
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(|| async { "Paste something in pastebin! use curl -X POST http://localhost:3000/submit -d 'this is some data'" }))
.route("/{id}", get(handle))
.route("/submit", post(submit_handle));
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}
Thank you for the suggestion, I will update the code locally :)
This is very unneccessary for a simple pastebin clone.
It might be but as I said I am very new to Rust and don’t know how to do some stuff in Rust. someone on IRC said I should use Arc to get around this issue but I didn’t know much about Arc so I went with unsafe.
It’s because I am changing the value of a static variable, there could be more than one thread trying to change the value of FILE_COUNT which could lead to race condition.
If the user only presses Enter then the first character would be “\n”.
yeah, you are right. Thanks for pointing out.
Thank you for the in depth suggestion :)
Thanks, I will keep that in mind.
Thank you so much for the suggestion. I have updated the code locally :)
but to do that I have to use external deps? which I am not comfortable doing. (I am newbie :) )
Thank you for the suggestions.
attempts
is used to display total attempts when user wins the game so it is used in the program, I don’t see any warnings from compiler.
done :D
use std::io;
fn main() {
let mut input: String = String::new();
let stdin = io::stdin();
let x = rand::random::<u32>() % 101;
let mut attempts = 0;
let mut user_inputs: Vec<u32> = Vec::new();
loop {
println!("Guess a number from 0 to 100:");
stdin.read_line(&mut input);
input = input.to_string().replace("\n", ""); // removing the \n
let user_input: u32 = input.parse::<u32>().unwrap();
user_inputs.push(user_input);
if x == user_input {
println!("You won! attempts: {attempts}");
println!("Your inputs:");
for input in user_inputs {
print!("{input} ");
}
println!("");
break;
}
else if x < user_input {
println!("too big");
attempts += 1;
}
else {
println!("too small");
attempts += 1;
}
input.clear()
}
}
that’s actually useful, Thank you for sharing the idea!