From 36f5eb943eaf2d827c3c99399a9dddbed9201635 Mon Sep 17 00:00:00 2001 From: "Dawid J. Kubis" Date: Fri, 26 Nov 2021 10:51:19 +0100 Subject: [PATCH] filesystem cleanup, make it single page --- .gitignore | 1 - Cargo.lock | 42 ++++++++++++------- Cargo.toml | 7 +--- build.rs | 14 ------- src/main.rs | 56 +++++++++++++++++--------- src/post.html | 24 ----------- src/routes.rs | 1 + src/{ => www}/favicon.ico | Bin src/{skeleton.html => www/index.html} | 0 9 files changed, 67 insertions(+), 78 deletions(-) delete mode 100644 build.rs delete mode 100644 src/post.html rename src/{ => www}/favicon.ico (100%) rename src/{skeleton.html => www/index.html} (100%) diff --git a/.gitignore b/.gitignore index f8da6bf..ea8c4bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ /target -src/readme.html diff --git a/Cargo.lock b/Cargo.lock index 17de6cf..ebd86bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,12 +26,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "anyhow" -version = "1.0.48" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62e1f47f7dc0422027a4e370dd4548d4d66b26782e513e98dca1e689e058a80e" - [[package]] name = "atty" version = "0.2.14" @@ -109,6 +103,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" +[[package]] +name = "bytesize" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c58ec36aac5066d5ca17df51b3e70279f5670a72102f5752cb7e7c856adfc70" + [[package]] name = "cc" version = "1.0.72" @@ -321,12 +321,6 @@ dependencies = [ "itoa", ] -[[package]] -name = "httparse" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acd94fdbe1d4ff688b67b04eee2e17bd50995534a61539e45adfefb45e5e5503" - [[package]] name = "indexmap" version = "1.7.0" @@ -356,13 +350,13 @@ checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" name = "kchan" version = "0.1.0" dependencies = [ - "anyhow", + "bytesize", "comrak", "http", - "httparse", "num_cpus", "sled", "structopt", + "thiserror", "threadpool", ] @@ -819,6 +813,26 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "thiserror" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "threadpool" version = "1.8.1" diff --git a/Cargo.toml b/Cargo.toml index dc1c2b4..488d7fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,6 @@ name = "kchan" version = "0.1.0" edition = "2021" -build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -13,8 +12,6 @@ http = "0.2.5" threadpool = "1.8.1" num_cpus = "1.13.0" sled = "0.34.7" -httparse = "1.5.1" -anyhow = "1.0.48" +thiserror = "1.0.30" +bytesize = "1.1.0" -[build-dependencies] -comrak = "0.12.1" diff --git a/build.rs b/build.rs deleted file mode 100644 index bf45618..0000000 --- a/build.rs +++ /dev/null @@ -1,14 +0,0 @@ -use std::fs::{read_to_string, write}; - -use comrak::{markdown_to_html, ComrakOptions}; - -fn main() { - let mut skeleton = read_to_string("src/skeleton.html").unwrap(); - let readme = read_to_string("README.md").unwrap(); - - skeleton = skeleton - .replace("{name}", "readme") - .replace("{}", &markdown_to_html(&readme, &ComrakOptions::default())); - - write("src/readme.html", skeleton).unwrap(); -} diff --git a/src/main.rs b/src/main.rs index 014fc91..6388af5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,37 +1,42 @@ mod routes; -use std::path::PathBuf; -use std::net::{TcpListener, TcpStream}; -use std::sync::mpsc::channel; use std::io::Read; +use std::net::{TcpListener, TcpStream}; +use std::path::PathBuf; use std::str; +use std::sync::mpsc::channel; +use http::request::Request; +use http::method::Method; use structopt::StructOpt; use threadpool::ThreadPool; -use http::request::Request; -use anyhow::Result; -use anyhow::Context; #[derive(StructOpt, Debug)] struct Opt { #[structopt(short, long, default_value = "8000")] port: u16, - #[structopt(long, default_value="posts", parse(from_os_str))] + #[structopt(long, default_value = "posts")] data_directory: PathBuf, - #[structopt(short, long, default_value = "")] + #[structopt(short, long, default_value = "100mb")] max_storage: String, + + #[structopt(short, long, default_value = "posts.sled")] + database: PathBuf, +} + +struct KchanError { } // very specific // TODO less retarded implementation -fn parse_request(text: &str) -> Result> { +// and get rid of unwraps +fn parse_request(text: &str) -> Request { let request = Request::builder(); let mut iter = text.lines(); - let mut first = iter.nth(0).unwrap() - .split_whitespace(); + let mut first = iter.nth(0).unwrap().split_whitespace(); let method = first.next().unwrap(); let uri = first.next().unwrap(); @@ -41,27 +46,27 @@ fn parse_request(text: &str) -> Result> { for line in &mut iter { println!("{}", line); - if line.is_empty() {break}; + if line.is_empty() { + break; + }; let line: Vec<&str> = line.split(":").take(2).collect(); headers.push((line[0], line[1])); } let body: String = iter.fold(String::from(""), |a, b| format!("{}{}", a, b)); - let mut request = request - .method(method) - .uri(uri); + let mut request = request.method(method).uri(uri); for header in headers { request = request.header(header.0, header.1); } - request.body(body).context("lmao") + request.body(body).unwrap() } fn handle(mut stream: TcpStream) { let ip = stream.peer_addr(); - let mut buffer = [0; 1<<10]; // 2 to the power of 10 + let mut buffer = [0; 1 << 10]; // 2 to the power of 10 stream.read(&mut buffer).unwrap(); let text = str::from_utf8(&buffer) @@ -69,18 +74,29 @@ fn handle(mut stream: TcpStream) { .trim_matches(0 as char) // gets rid of null at the end of buffer .trim(); - println!("{:?}", parse_request(text).unwrap()); - + let request = parse_request(text); + println!("{:?}", request); + match *request.method() { + Method::GET => "", + Method::POST => "", + Method::DELETE => "", + _ => "", + }; } fn main() { let opt = Opt::from_args(); + let max_storage = opt.max_storage.parse::().unwrap(); let listener = TcpListener::bind(format!("127.0.0.1:{}", opt.port)).unwrap(); let pool = ThreadPool::new(num_cpus::get()); + // maybe send back here? for stream in listener.incoming() { let stream = stream.unwrap(); - pool.execute(|| handle(stream)) + pool.execute(|| { + + handle(stream) + }); } } diff --git a/src/post.html b/src/post.html deleted file mode 100644 index 74a2947..0000000 --- a/src/post.html +++ /dev/null @@ -1,24 +0,0 @@ - - - - rchan - post - - - -
-
-

- rchan readme post -

-
-
-
-
- -
- -
-
-
- - diff --git a/src/routes.rs b/src/routes.rs index e69de29..8b13789 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -0,0 +1 @@ + diff --git a/src/favicon.ico b/src/www/favicon.ico similarity index 100% rename from src/favicon.ico rename to src/www/favicon.ico diff --git a/src/skeleton.html b/src/www/index.html similarity index 100% rename from src/skeleton.html rename to src/www/index.html