filesystem cleanup, make it single page

doctorpavel
Dawid J. Kubis 3 years ago
parent e2de017014
commit 36f5eb943e

1
.gitignore vendored

@ -1,2 +1 @@
/target
src/readme.html

42
Cargo.lock generated

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

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

@ -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();
}

@ -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<Request<String>> {
// and get rid of unwraps
fn parse_request(text: &str) -> Request<String> {
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<Request<String>> {
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::<bytesize::ByteSize>().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)
});
}
}

@ -1,24 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>rchan - post</title>
<link rel="stylesheet" type="text/css" href="css">
</head>
<body>
<div class="grid">
<div class="home">
<h1>
<a href="/">rchan</a> <a href="/readme">readme</a> <a href="post">post</a>
</h1>
<hr>
</div>
<div class="text">
<form id="postform" action="/post" method="post">
<textarea name="content" rows="10" cols="50"></textarea>
<br>
<button>POST</button>
</form>
</div>
</div>
</head>
</html>

Before

Width:  |  Height:  |  Size: 236 KiB

After

Width:  |  Height:  |  Size: 236 KiB

Loading…
Cancel
Save