removed unnecesary web server

This commit is contained in:
Technoduck 2024-08-31 13:26:43 -04:00
parent fc3a2abcf4
commit 5af7ec2964
4 changed files with 22 additions and 178 deletions

59
Cargo.lock generated
View file

@ -66,12 +66,6 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "ascii"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16"
[[package]]
name = "askama"
version = "0.12.1"
@ -98,7 +92,7 @@ dependencies = [
"proc-macro2",
"quote",
"serde",
"syn 2.0.76",
"syn 2.0.77",
]
[[package]]
@ -210,12 +204,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chunked_transfer"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901"
[[package]]
name = "clap"
version = "4.5.16"
@ -248,7 +236,7 @@ dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
]
[[package]]
@ -314,7 +302,7 @@ dependencies = [
"proc-macro2",
"quote",
"strsim",
"syn 2.0.76",
"syn 2.0.77",
]
[[package]]
@ -325,7 +313,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
dependencies = [
"darling_core",
"quote",
"syn 2.0.76",
"syn 2.0.77",
]
[[package]]
@ -355,7 +343,7 @@ dependencies = [
"darling",
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
]
[[package]]
@ -365,7 +353,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4abae7035bf79b9877b779505d8cf3749285b80c43941eda66604841889451dc"
dependencies = [
"derive_builder_core",
"syn 2.0.76",
"syn 2.0.77",
]
[[package]]
@ -483,12 +471,6 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "httpdate"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "humansize"
version = "2.1.3"
@ -853,14 +835,13 @@ dependencies = [
name = "rusty_duck"
version = "0.1.0"
dependencies = [
"ascii",
"askama",
"comrak",
"markdown-parser",
"rand",
"serde",
"serde_yaml 0.9.34+deprecated",
"tiny_http",
"syntect",
]
[[package]]
@ -895,7 +876,7 @@ checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
]
[[package]]
@ -976,9 +957,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.76"
version = "2.0.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525"
checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed"
dependencies = [
"proc-macro2",
"quote",
@ -1035,7 +1016,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
]
[[package]]
@ -1069,18 +1050,6 @@ dependencies = [
"time-core",
]
[[package]]
name = "tiny_http"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "389915df6413a2e74fb181895f933386023c71110878cd0825588928e64cdc82"
dependencies = [
"ascii",
"chunked_transfer",
"httpdate",
"log",
]
[[package]]
name = "tinyvec"
version = "1.8.0"
@ -1197,7 +1166,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
"wasm-bindgen-shared",
]
@ -1219,7 +1188,7 @@ checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -1420,5 +1389,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.76",
"syn 2.0.77",
]

View file

@ -11,11 +11,10 @@ panic = 'abort' # Abort on panic
strip = "symbols" # Strip symbols from binary
[dependencies]
ascii = "1.1.0"
askama = { version = "0.12.1" }
comrak = "0.27.0"
markdown-parser = "0.1.2"
rand = "0.8.5"
serde = { version = "1.0.209", features = ["derive"] }
serde_yaml = "0.9.34"
tiny_http = "0.12.0"
syntect = "5.2.0"

View file

@ -40,11 +40,11 @@ fn parse_markdown(content: &str) -> String {
options.extension.underline = true;
options.extension.strikethrough = true;
let plugins = comrak::Plugins::default();
//let adapter = comrak::plugins::syntect::SyntectAdapterBuilder::new()
// .theme("base16-ocean.dark")
// .build();
let mut plugins = comrak::Plugins::default();
let adapter = comrak::plugins::syntect::SyntectAdapterBuilder::new()
.theme("base16-ocean.dark")
.build();
//plugins.render.codefence_syntax_highlighter = Some(&adapter);
plugins.render.codefence_syntax_highlighter = Some(&adapter);
comrak::markdown_to_html_with_plugins(content, &options, &plugins)
}

View file

@ -1,8 +1,4 @@
use std::{error::Error, fs::{self, read_dir, DirBuilder, File}, io::Write, path::Path};
use ascii::AsciiString;
use tiny_http::{Response, Server};
use std::{fs::{self, read_dir, DirBuilder},path::Path};
mod handlers;
mod structs;
@ -34,7 +30,7 @@ fn main() {
match DirBuilder::new()
.create(format!("{output_path}/blog")) {
Ok(_) => (),
Err(_) => (),
Err(err) => println!("Error detected: {err}"),
}
@ -67,123 +63,3 @@ fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result
}
Ok(())
}
fn old_main() {
// let app: axum::Router = Router::new()
// .route("/", get(handlers::index))
// .route("/about", get(handlers::about))
// .route("/blog",get(handlers::index))
// .route("/blog/:blog_path",get(handlers::blog))
// .route_service("/robots.txt", ServeFile::new("assets/robots.txt"))
// .fallback(get(handlers::not_found))
// .nest_service("/assets", ServeDir::new("assets"));
//
//
// let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
// axum::serve(listener, app).await.unwrap();
//
//-----
// let server = Arc::new(Server::http("127.0.0.1:3001").unwrap());
// println!("started server on port 3001");
//
// let mut handles = Vec::new();
//
// for thread_num in 0..6 {
// println!("starting thread {}",thread_num);
// let server = server.clone();
// handles.push(thread::spawn(move || server_thread(server)));
//
// }
// for h in handles {
// h.join().unwrap();
// }
// ------------
let server = Server::http("127.0.0.1:3000").unwrap();
println!("started server on port 3001");
server_thread(server);
}
fn server_thread(server: Server) {
for request in server.incoming_requests() {
println!("received request; method: {}, url: {}",
request.method(),request.url());
match request.url() {
"" | "/" => {
let response = Response::from_string(handlers::index());
let response = response.with_header(tiny_http::Header {
field: "Content-Type".parse().unwrap(),
value: AsciiString::from_ascii("text/html;charset=utf8").unwrap(),
});
let _ = request.respond(response);
}
"/about" => {
let response = Response::from_string(handlers::about());
let response = response.with_header(tiny_http::Header {
field: "Content-Type".parse().unwrap(),
value: AsciiString::from_ascii("text/html;charset=utf8").unwrap(),
});
let _ = request.respond(response);
}
asset_url @ _ if asset_url.starts_with("/assets/") => {
println!("getting asset : {}",asset_url);
if let Some(asset) = get_asset(&asset_url){
let _ = request.respond(asset);
}
}
blog_url @ _ if blog_url.starts_with("/blog/") => {
let response = Response::from_string(handlers::blog(blog_url[6..].to_string()));
let response = response.with_header(tiny_http::Header {
field: "Content-Type".parse().unwrap(),
value: AsciiString::from_ascii("text/html;charset=utf8").unwrap(),
});
let _ = request.respond(response);
}
&_ => {
let response = Response::from_string(handlers::not_found());
let response = response.with_header(tiny_http::Header {
field: "Content-Type".parse().unwrap(),
value: AsciiString::from_ascii("text/html;charset=utf8").unwrap(),
});
let _ = request.respond(response);
}
}
}
}
fn get_asset(asset:&str)-> Option<Response<File>>{
match Path::new(&asset[1..]).exists() {
true => {
println!("found asset");
Some(Response::from_file(File::open(&asset[1..]).unwrap()))
}
false => None,
}
}