Static site generator + my presonnal website written in rust for some reason.
1---
2title: Full website Re-Update
3date: 2024-08-29
4---
5## New Website?
6
7### Rust
8
9The time has come and another re-write is upon us.
10
11This time inspied by [this article](https://blog.transrights.art/blogs/2024_Screw-Frameworks-New-Site-2),
12[theprimeagen](https://www.youtube.com/watch?v=rcZSOLAI1lM), and everpresent desire to rewrite everything in Rust,
13the new website is a complete rewrite using rust and adjacent techniques.
14
15Using only
16```
17[dependencies]
18askama = { version = "0.12.1", features = ["with-axum"] }
19askama_axum = "0.4.0"
20axum = "0.7.5"
21comrak = "0.27.0"
22markdown-parser = "0.1.2"
23rand = "0.8.5"
24serde = { version = "1.0.209", features = ["derive"] }
25serde_yaml = "0.9.34"
26tokio = { version = "1.39.3", features = ["macros", "rt-multi-thread"] }
27tower-http = { version = "0.5.2", features = ["fs"] }
28```
29as dependencies in the end we get a clean~ish, 2.8MB executable.
30
31All markdown compilation gets done every time page is loaded, which is sub-optimal,
32But reading time for all posts is non-significant compared to other loading times.
33
34And voila, there is the new post.
35
36## Update
37
38This approach however had few issues.
39As fun as it is writing your own routing logic, It feels purely unnecessary.
40
41Even 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,
42it does not affect the load times.
43
44Short 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.
45
46Therefore the goal has been slightly changed.
47From serving the files the goal is now using askama like a sort of static site generator.
48
49# DIY Hugo?
50
51Not exactly. In the current state it is basically Saait again, but with extra steps.
52Additional pages require source code intervention, which isn't hard, but tidious.
53
54//TODO: use some enum for pages, with derive as EnumString, so new pages can be added simply by adding a template.
55
56
57But for now, the new dependencies now look like this:
58
59```toml
60[dependencies]
61askama = { version = "0.12.1" }
62comrak = "0.27.0"
63markdown-parser = "0.1.2"
64rand = "0.8.5"
65serde = { version = "1.0.209", features = ["derive"] }
66serde_yaml = "0.9.34"
67syntect = "5.2.0"
68```
69
70Syntect providing oh so nice code highlighting in the markdown.
71
72
73```json
74{
75 "firstName": "John",
76 "lastName": "Smith",
77 "age": 25
78}
79```
80
81```rust
82
83std::fs::write(format!("{output_path}/index.html"), handlers::index().as_bytes()).expect("Couldnt write index file");
84std::fs::write(format!("{output_path}/about.html"), handlers::about().as_bytes()).expect("Couldnt write about file");
85std::fs::write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file");
86
87
88for entry in post_dir_iter {
89 if let Ok(entry) = entry {
90
91 let filename = entry.file_name().into_string().unwrap().split(".").collect::<Vec<_>>()[0].to_string();
92
93 fs::write(format!("{output_path}/blog/{filename}.html"), handlers::blog(filename).as_bytes()).expect("Couldnt write blog entry");
94 };
95
96}
97
98```
99
100For now, I am rather happy with the result.
101
102Built 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.
103> ./target/release/rusty_duck 0.03s user 0.01s system 97% cpu 0.035 total
104
105This is no **BLAZING** speed, but it's honest work.
106