Previously I added a basic comment system to my website using a separate web server which served only the comment HTML. This is fine, but it does require another program running continuously on my server. Since that server is a raspberry pi, and it is running a lot of other software as well, and my blog doesn’t get a lot of hits (let alone comments), I thought I could do better by using the Common Gateway Interface (CGI). CGI doesn’t require a daemon program, but instead will launch a program to generate dynamic content when someone loads the page. In this way, no memory or CPU is required until an actual page is requested. The downside is that a new process is launched for each page load, but I think that’s an OK trade-off for me. I already have CGI configured on my web server for running cgit.
I also wanted to explore the zig programming language some more by writing an actual program with it, so I chose to rewrite it in zig.
I made a quick exploratory program just to remind myself how CGI works in Caddy, then I ported my original comments app to a CGI app. The new app has far fewer dependencies since it doesn’t embed a web server or web framework. It depends on the zig standard library, libpq, mustache-zig and some of my own shared code from another project to do page routing.
The new comments app works just the same as the old one, although with some loss of functionality - it no longer emails me when a new comment is posted. I plan to implement this, however the zig ecosystem is much less mature than rust right now, and I could not find a good email library so I might end up rolling my own.
Another spin-off from this project could be a pure-zig postgres database library. Libpq is great, however it’s a C library and requires linking the C standard library, and makes code less easily portable to other platforms. The pgx project shows that it’s possible to have a pure-go postgresql driver, and this might be inspiration for a zig version. Another advantage of using the host language rather than the C library is making use of future asynchronous capabilities.