From 7e7c0e217384c40beb09c5f5e451de2fcc44a8c5 Mon Sep 17 00:00:00 2001 From: "Dawid J. Kubis" Date: Sun, 6 Mar 2022 14:42:05 +0100 Subject: [PATCH] fmt::Display implemented for Threads and Posts --- src/main.rs | 28 ++++--- src/post.rs | 39 ++++++++-- src/www/index.html | 187 --------------------------------------------- 3 files changed, 51 insertions(+), 203 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9f52d39..c81012e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use std::io::{Read, Write}; use std::net::{TcpListener, TcpStream}; use std::path::PathBuf; use std::str; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, MutexGuard}; use lazy_static::lazy_static; use structopt::StructOpt; @@ -32,17 +32,22 @@ struct Opt { max_posts: usize, } +// get command line arguments and make them static +// safe because they're read-only lazy_static! { // first parse command line arguments static ref OPT: Opt = Opt::from_args(); } -#[derive(Debug)] -struct Cache<'a>(&'a str); +// TODO cache +//#[derive(Debug)] +//struct Cache<'a>(&'a str); + +fn handle(request: Request, database: Arc>) -> &'static str { + let database = database.lock().unwrap(); -fn handle(request: Request) -> &'static str { match request.method.as_str() { - "GET" => get(&request.uri), + "GET" => get(&request.uri, database), "POST" => "HTTP/1.1 200 OK", // check admin hash "DELETE" => "HTTP/1.1 200 OK", @@ -50,11 +55,11 @@ fn handle(request: Request) -> &'static str { } } -fn get(path: &str) -> &'static str { +fn get(path: &str, database: MutexGuard) -> &'static str { match path { // list threads in this case "/" => dbg!(INDEX), - // list posts here + // list specific thread here s => "", } } @@ -66,13 +71,14 @@ fn main() { // create threadpool for incoming requests let pool = ThreadPool::new(num_cpus::get()); // open database - let db = sled::open(&OPT.database).unwrap(); - // setup cache - let cache = Arc::new(Cache("")); + let db = Arc::new(Mutex::new(sled::open(&OPT.database).unwrap())); + // TODO setup cache + //let cache = Arc::new(Cache("")); // wait for requests for stream in listener.incoming() { let mut stream = stream.unwrap(); + let database = Arc::clone(&db); pool.execute(move || { let ip = stream.peer_addr(); // TODO check if valid ip before reading request @@ -87,7 +93,7 @@ fn main() { // handle request // add database and cache here - stream.write(handle(request).as_bytes()).unwrap(); + stream.write(handle(request, database).as_bytes()).unwrap(); }); } } diff --git a/src/post.rs b/src/post.rs index 75d0b07..9e67d2e 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,6 +1,35 @@ -struct Post { - ip: [u8; 4], - code: u64, - date: String, - body: String, +use std::fmt; + +#[derive(Debug, Clone)] +pub struct Post { + pub id: u64, + pub ip: [u8; 4], + pub date: String, + pub body: String, +} + +impl fmt::Display for Post { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "
{} {}

{}

", + self.id, self.date, self.body, + ) + } +} + +pub struct Thread { + pub id: u64, + pub topic: String, + pub posts: Vec, +} + +impl fmt::Display for Thread { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let posts: String = self + .posts + .iter() + .fold(String::from(""), |a, b| format!("{}{}", a, b)); + write!(f, "{}", posts) + } } diff --git a/src/www/index.html b/src/www/index.html index d7320be..be455b2 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -17,190 +17,3 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -