|
|
@ -2,18 +2,20 @@ mod http;
|
|
|
|
mod post;
|
|
|
|
mod post;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::http::{Request, Response, Status};
|
|
|
|
use crate::http::{Request, Response, Status};
|
|
|
|
|
|
|
|
use crate::post::{Post, Thread};
|
|
|
|
|
|
|
|
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
use std::net::{TcpListener, TcpStream};
|
|
|
|
use std::net::{TcpListener, TcpStream};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::str;
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
use std::str::from_utf8;
|
|
|
|
use std::sync::{Arc, Mutex, MutexGuard};
|
|
|
|
use std::sync::{Arc, Mutex, MutexGuard};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use bincode::{deserialize, serialize};
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use structopt::StructOpt;
|
|
|
|
use structopt::StructOpt;
|
|
|
|
use threadpool::ThreadPool;
|
|
|
|
use threadpool::ThreadPool;
|
|
|
|
|
|
|
|
|
|
|
|
// TODO minify html
|
|
|
|
|
|
|
|
const INDEX: &'static str = include_str!("www/index.html");
|
|
|
|
const INDEX: &'static str = include_str!("www/index.html");
|
|
|
|
const FAVICON: &'static [u8] = include_bytes!("www/favicon.ico");
|
|
|
|
const FAVICON: &'static [u8] = include_bytes!("www/favicon.ico");
|
|
|
|
|
|
|
|
|
|
|
@ -43,24 +45,36 @@ lazy_static! {
|
|
|
|
//#[derive(Debug)]
|
|
|
|
//#[derive(Debug)]
|
|
|
|
//struct Cache<'a>(&'a str);
|
|
|
|
//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();
|
|
|
|
let database = database.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
match request.method.as_str() {
|
|
|
|
match request.method.as_str() {
|
|
|
|
"GET" => get(&request.uri, database),
|
|
|
|
"GET" => get(&request.uri, database),
|
|
|
|
"POST" => "HTTP/1.1 200 OK",
|
|
|
|
"POST" => "HTTP/1.1 200 OK".to_string(),
|
|
|
|
// check admin hash
|
|
|
|
// 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 {
|
|
|
|
match path {
|
|
|
|
// list threads in this case
|
|
|
|
// 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
|
|
|
|
// list specific thread here
|
|
|
|
s => "",
|
|
|
|
s => "".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|