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