modified GET behavior

doctorpavel
Dawid J. Kubis 3 years ago
parent 7e7c0e2173
commit 8bc5be6259

31
Cargo.lock generated

@ -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"

@ -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"] }

@ -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",
}
}
}

@ -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<Mutex<sled::Db>>) -> &'static str {
fn handle(request: Request, database: Arc<Mutex<sled::Db>>) -> 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<sled::Db>) -> &'static str {
fn get(path: &str, database: MutexGuard<sled::Db>) -> 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::<Thread>(&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(),
}
}

@ -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<Post>,
}
@ -33,3 +35,14 @@ impl fmt::Display for Thread {
write!(f, "{}", posts)
}
}
impl Thread {
pub fn head(&self, id: &str) -> String {
format!(
"<article><div><span class=\"info\">{} {}</span></div>{}</article>",
id,
self.topic,
self.posts[0].to_string(),
)
}
}

Loading…
Cancel
Save