From 8bc5be6259c1e7220c15ea48678bd7c4e7be2ef9 Mon Sep 17 00:00:00 2001 From: "Dawid J. Kubis" Date: Sun, 6 Mar 2022 17:02:40 +0100 Subject: [PATCH] modified GET behavior --- Cargo.lock | 31 +++++++++++++++++++++++++++++++ Cargo.toml | 3 ++- src/http.rs | 8 ++++---- src/main.rs | 30 ++++++++++++++++++++++-------- src/post.rs | 17 +++++++++++++++-- 5 files changed, 74 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b4bc44..c360f4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,6 +28,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -154,9 +163,11 @@ dependencies = [ name = "kchan" version = "0.1.0" dependencies = [ + "bincode", "lazy_static", "num_cpus", "rand", + "serde", "sled", "structopt", "thiserror", @@ -340,6 +351,26 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "serde" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "sled" version = "0.34.7" diff --git a/Cargo.toml b/Cargo.toml index 408cfe1..e12b350 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,5 @@ thiserror = "1.0.30" rand = "0.8.4" lazy_static = "1.4.0" sled = "0.34.7" - +bincode = "1.3.3" +serde = { version = "1.0.136", features = ["derive"] } diff --git a/src/http.rs b/src/http.rs index f594419..d37e990 100644 --- a/src/http.rs +++ b/src/http.rs @@ -21,15 +21,15 @@ pub struct Response { #[derive(Debug, Clone, Copy)] pub enum Status { - OK = 200, - NOTFOUND = 404, + Ok = 200, + NotFound = 404, } impl Status { pub fn get_message(&self) -> &'static str { match self { - Self::OK => "OK", - Self::NOTFOUND => "NOT FOUND", + Self::Ok => "OK", + Self::NotFound => "NOT FOUND", } } } diff --git a/src/main.rs b/src/main.rs index c81012e..47c3c7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,18 +2,20 @@ mod http; mod post; use crate::http::{Request, Response, Status}; +use crate::post::{Post, Thread}; use std::io::{Read, Write}; use std::net::{TcpListener, TcpStream}; use std::path::PathBuf; use std::str; +use std::str::from_utf8; use std::sync::{Arc, Mutex, MutexGuard}; +use bincode::{deserialize, serialize}; use lazy_static::lazy_static; use structopt::StructOpt; use threadpool::ThreadPool; -// TODO minify html const INDEX: &'static str = include_str!("www/index.html"); const FAVICON: &'static [u8] = include_bytes!("www/favicon.ico"); @@ -43,24 +45,36 @@ lazy_static! { //#[derive(Debug)] //struct Cache<'a>(&'a str); -fn handle(request: Request, database: Arc>) -> &'static str { +fn handle(request: Request, database: Arc>) -> String { let database = database.lock().unwrap(); match request.method.as_str() { "GET" => get(&request.uri, database), - "POST" => "HTTP/1.1 200 OK", + "POST" => "HTTP/1.1 200 OK".to_string(), // check admin hash - "DELETE" => "HTTP/1.1 200 OK", - _ => "", + "DELETE" => "HTTP/1.1 200 OK".to_string(), + _ => "".to_string(), } } -fn get(path: &str, database: MutexGuard) -> &'static str { +fn get(path: &str, database: MutexGuard) -> String { match path { // list threads in this case - "/" => dbg!(INDEX), + "/" => { + let content = INDEX.to_string().replace( + "{}", + &*database + .iter() + .map(|x| x.unwrap()) + .map(|x| (x.0, deserialize::(&x.1).unwrap())) + .map(|x| x.1.head(from_utf8(&x.0).unwrap())) + .fold(String::from(""), |a, b| format!("{}{}", a, b)), + ); + + Response::new(Status::Ok, vec![], content).to_string() + } // list specific thread here - s => "", + s => "".to_string(), } } diff --git a/src/post.rs b/src/post.rs index 9e67d2e..fdc4301 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,6 +1,8 @@ use std::fmt; -#[derive(Debug, Clone)] +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Post { pub id: u64, pub ip: [u8; 4], @@ -18,8 +20,8 @@ impl fmt::Display for Post { } } +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Thread { - pub id: u64, pub topic: String, pub posts: Vec, } @@ -33,3 +35,14 @@ impl fmt::Display for Thread { write!(f, "{}", posts) } } + +impl Thread { + pub fn head(&self, id: &str) -> String { + format!( + "
{} {}
{}
", + id, + self.topic, + self.posts[0].to_string(), + ) + } +}