Friday, February 5

moving from cgi to static site generation

This is the kind of post that people used to refer to as “administrivia” on the old-timey artisanal internet of yore. It’s about this web site itself, which is usually a boring thing for a web site to talk about.

Still, I’m trying to take this “keep a technical logbook” idea seriously and write down all the miscellaneous stuff that doesn’t go into a tutorial for my full-time gig.

First off, I have been thinking that could be used as a static site generator instead of a piece of dynamic code running under a CGI script. I finally spent time on this, and it turned out to be (mostly) simpler than I had expected.

When I started, the way this worked was that a Perl FastCGI script lived in /var/www/p1k3. FastCGI means that a long-running process loops over input, blocking until the web server receives a new request and hands it a fresh request. In this case, it would just pass the parameters to a method on an instance of Display, which would return some text to be printed. Pretty much like this. In order to use pretty URLs, I wrote a bunch of Apache mod_rewrite rules to pass things like:

Off to:

Which would look for a file in:


…and do markup processing, then push it through a Perl template of sorts to return HTML.

This code hasn’t, to my knowledge, gotten me owned for a while now, and part of me liked continuing to run old-school CGI in some little corner of my life. That said, it turns out that static site generators are popular with the cool kids for some good reasons.

For one thing, all running code is both an attack surface and a source of bugs. For another, a lot of the incidental complexity vanishes from the whole scenario when you’re just serving static files. For example, those RewriteRules, always cheesy and inflexible to maintain, just went away. So did the need to enable CGI at all. And absent those dependencies, I’m now free to serve with Apache, Nginx, Lighttpd, or just about anything else.

Anyway, what I do now is convert the same file to:


There’s some other rigamarole with copying static assets, and the code is still real stupid because I wrote most of it when I was 21ish and an idiot and then later tried to refactor and made it worse.

I should chop this all down into something a lot simpler. Maybe I should move it to some other language.