Hello, Zig!
The following text is partly by Claude Code.
This blog consists of a single Zig program. It reads Markdown files from a folder and serves them as HTML – similar to the flat-file CMS Grav, only smaller.
Life of a request #
On every request the server reads the content tree anew. A change is therefore visible immediately, and there is no cache you could forget to clear.
The folder structure is the page structure #
content/
├── index.md → /
├── index.en.md → /en/
├── 01.blog/
│ ├── index.md → /blog/
│ └── hallo-zig/
│ ├── index.md → /blog/hallo-zig/
│ ├── index.en.md → /en/blog/hallo-zig/
│ ├── ablauf.svg → /blog/hallo-zig/ablauf.svg
│ └── demo.mp4 → /blog/hallo-zig/demo.mp4
└── 02.ueber/
└── index.md → /ueber/
A few rules are enough:
- A folder with
index.mdis a page. - A number prefix like
01.sets the order and adds the page to the navigation. It does not appear in the URL. - Images and videos live next to the Markdown file and are included with relative paths.
- A translation sits next to the original and carries the language code in
its name:
index.en.md.
Code #
Code blocks are highlighted on the server, so no JavaScript is needed. This is the loop that accepts new connections:
pub fn run(self: *const Server, listener: *net.Server) void {
var group: Io.Group = .init;
defer group.cancel(self.io);
while (true) {
const stream = listener.accept(self.io) catch return;
// Every connection gets its own task.
group.concurrent(self.io, handleConnection, .{ self, stream }) catch {
stream.close(self.io);
};
}
}
Building and starting works with the Zig build system:
zig build run -- --port 8080
Videos #
Videos are included like images; the file extension decides whether you get an
<img> or a <video>. The server supports range requests for this, so you can
seek within the video.
Summary #
| Building block | Implementation |
|---|---|
| Server | Zig 0.17, std.http.Server |
| Content | Markdown with frontmatter |
| Design | One stylesheet, light and dark mode |
| Hierarchy | Folder structure |
| Languages | index.md, index.en.md, … |
Little technology, little maintenance: one binary, one folder of text files.