Andrew Egeler
Learning how things work.

This Blog is Written in C++

TL;DR: I wrote this site in C++. See the not-documented-or-cleaned-up code here (Note that it may have changed significantly since this blog post).

When I set up this blog, I had a number of different options. I could have made it a React app, but Typescript isn't my favorite language. I could have made it a statically generated site, but then I'd have to rework it if I did want to try anything more dynamic (if I ever add comments, for example).

Ultimately, I had a couple of requirements:

So, I picked C++. It's the best language I'm aware of that lets you write high- level code, but gives you the power to optimize your memory allocations if you have the need (Rust might be close, but every time I look at it I find another feature I miss from C++). If your immediate reaction to this is "But C/C++ manual memory management corruption segfault!", then I recommend taking another look at modern C++, unique_ptr, and RAII patterns. C++ has far better tools for memory management than C does - most of the pain comes from the times you need to use C libraries.

My architecture is simple: I have a front-end nginx server that is routing requests for multiple domains, and the requests for andrew.egeler.us get forwarded to a container running my C++ application, which serves the entire site - static files and all.

The application itself is simple as well. All of the static files are compiled directly into the binary, on startup it reads a directory of .md files and compiles them to HTML with cmark, and it then starts up a Restinio server. For each request, it uses ctemplate to generate the final HTML for the client.

While this still isn't as fast as it could be, or as well-written as it could be, or even written in a currently-popular language, it results in a small, fast website. I suspect that I could even make it faster than a static site, given enough work - because the blog code is the webserver, I can make assumptions and take shortcuts that a normal webserver wouldn't be able to.

Who knows - if I decide to get clever with it, maybe I'll write my own web server library that uses liburing to have the fastest possible network interface.