rewrite to be impromptu Saait

This commit is contained in:
Technoduck 2024-08-31 15:56:15 -04:00
parent 5af7ec2964
commit bb50e3b2e5
4 changed files with 74 additions and 12 deletions

View file

@ -12,7 +12,7 @@ strip = "symbols" # Strip symbols from binary
[dependencies] [dependencies]
askama = { version = "0.12.1" } askama = { version = "0.12.1" }
comrak = "0.27.0" comrak = { version = "0.27.0", features = ["syntect"] }
markdown-parser = "0.1.2" markdown-parser = "0.1.2"
rand = "0.8.5" rand = "0.8.5"
serde = { version = "1.0.209", features = ["derive"] } serde = { version = "1.0.209", features = ["derive"] }

View file

@ -12,7 +12,7 @@ This time inspied by [this article](https://blog.transrights.art/blogs/2024_Scre
[theprimeagen](https://www.youtube.com/watch?v=rcZSOLAI1lM), and everpresent desire to rewrite everything in Rust, [theprimeagen](https://www.youtube.com/watch?v=rcZSOLAI1lM), and everpresent desire to rewrite everything in Rust,
the new website is a complete rewrite using rust and adjacent techniques. the new website is a complete rewrite using rust and adjacent techniques.
Using only Using only
``` ```
[dependencies] [dependencies]
askama = { version = "0.12.1", features = ["with-axum"] } askama = { version = "0.12.1", features = ["with-axum"] }
@ -26,6 +26,49 @@ serde_yaml = "0.9.34"
tokio = { version = "1.39.3", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.39.3", features = ["macros", "rt-multi-thread"] }
tower-http = { version = "0.5.2", features = ["fs"] } tower-http = { version = "0.5.2", features = ["fs"] }
``` ```
as dependencies in the end we get a clean~ish, 2.8MB executable.
All markdown compilation gets done every time page is loaded, which is sub-optimal,
But reading time for all posts is non-significant compared to other loading times.
And voila, there is the new post.
## Update
This approach however had few issues.
As fun as it is writing your own routing logic, It feels purely unnecessary.
Even scaling down from `axum` to `tiny_http`, it doesn't change the binary size, and because of the limited resources and location of the VPS,
it does not affect the load times.
Short of rewriting it all in [Yew](yew.rs) and loading the wasm as a SPA (which comes with it's own complexity) loading times would not improve.
Therefore the goal has been slightly changed.
From serving the files the goal is now using askama like a sort of static site generator.
# DIY Hugo?
Not exactly. In the current state it is basically Saait again, but with extra steps.
Additional pages require source code intervention, which isn't hard, but tidious.
//TODO: use some enum for pages, with derive as EnumString, so new pages can be added simply by adding a template.
But for now, the new dependencies now look like this:
```toml
[dependencies]
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"
syntect = "5.2.0"
```
Syntect providing oh so nice code highlighting in the markdown.
```json ```json
{ {
@ -35,10 +78,29 @@ tower-http = { version = "0.5.2", features = ["fs"] }
} }
``` ```
as dependancies in the end we get a clean~ish, 2.8MB executable. ```rust
All markdown compilation gets done every time page is loaded, which is sub-optimal, std::fs::write(format!("{output_path}/index.html"), handlers::index().as_bytes()).expect("Couldnt write index file");
But reading time for all posts is non-significant compared to other loading times. std::fs::write(format!("{output_path}/about.html"), handlers::about().as_bytes()).expect("Couldnt write about file");
std::fs::write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file");
And voila, there is the new post.
for entry in post_dir_iter {
if let Ok(entry) = entry {
let filename = entry.file_name().into_string().unwrap().split(".").collect::<Vec<_>>()[0].to_string();
fs::write(format!("{output_path}/blog/{filename}.html"), handlers::blog(filename).as_bytes()).expect("Couldnt write blog entry");
};
}
```
For now, I am rather happy with the result.
Built for release profile, binary is just 3.1M (majority of it is syntact), and produces a output folder with it's contents in a rather pleasant amount of time.
> ./target/release/rusty_duck 0.03s user 0.01s system 97% cpu 0.035 total
This is no **BLAZING** speed, but it's honest work.

View file

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

View file

@ -1,4 +1,4 @@
use std::{fs::{self, read_dir, DirBuilder},path::Path}; use std::{fs::{write, read_dir, DirBuilder},path::Path};
mod handlers; mod handlers;
mod structs; mod structs;
@ -23,9 +23,9 @@ fn main() {
fs::write(format!("{output_path}/index.html"), handlers::index().as_bytes()).expect("Couldnt write index file"); write(format!("{output_path}/index.html"), handlers::index().as_bytes()).expect("Couldnt write index file");
fs::write(format!("{output_path}/about.html"), handlers::about().as_bytes()).expect("Couldnt write about file"); write(format!("{output_path}/about.html"), handlers::about().as_bytes()).expect("Couldnt write about file");
fs::write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file"); write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file");
match DirBuilder::new() match DirBuilder::new()
.create(format!("{output_path}/blog")) { .create(format!("{output_path}/blog")) {
@ -44,7 +44,7 @@ fn main() {
let filename = entry.file_name().into_string().unwrap().split(".").collect::<Vec<_>>()[0].to_string(); let filename = entry.file_name().into_string().unwrap().split(".").collect::<Vec<_>>()[0].to_string();
fs::write(format!("{output_path}/blog/{filename}.html"), handlers::blog(filename).as_bytes()).expect("Couldnt write blog entry"); write(format!("{output_path}/blog/{filename}.html"), handlers::blog(filename).as_bytes()).expect("Couldnt write blog entry");
}; };
} }