A "just-enough-functionality" Zig docs server: zds

zds comes in at just 70k in ReleaseSmall on my m1 mac. It’s hardcoded to just serve the Zig autodoc files in the current working directory. Thre’s a sample project in the sample directory with a build.zig that shows one possible way of using this.

The relevant code in that sample build.zig:

    const docs_wf = b.addWriteFiles();
    _ = docs_wf.addCopyDirectory(exe.getEmittedDocs(), "docs", .{});

    const zds_wf = b.addWriteFiles();
    const zds_src = zds_wf.add("zds.zig",
        \\const std = @import("std");
        \\
        \\pub fn main() !void {
        \\    try @import("zds").zds("127.0.0.1", 8080, 6);
        \\}
    );

    const zds = b.dependency("zds", .{});

    const docs_server = b.addExecutable(.{
        .name = "zds",
        .root_source_file = zds_src,
        .target = b.host,
        .optimize = .ReleaseSmall,
    });
    docs_server.root_module.addImport("zds", zds.module("zds"));

    _ = docs_wf.addCopyFile(docs_server.getEmittedBin(), "docs/zds");

    const install_docs = b.addInstallDirectory(.{
        .source_dir = docs_wf.getDirectory(),
        .install_dir = .prefix,
        .install_subdir = ".",
    });

    const docs_opt = b.option(bool, "docs", "Generate and install docs.") orelse false;
    if (docs_opt) b.getInstallStep().dependOn(&install_docs.step);

Then

$ cd zig-out/docs && ./zds

and point your browser to http://localhost:8080/.

7 Likes