Static Files ​
Serve a directory with automatic MIME type resolution (httpx.mime.fromPath), ETag / If-None-Match conditional requests, range requests, and index resolution. See examples/staticFiles.zig.
Demo Program ​
zig
const std = @import("std");
const httpx = @import("httpx");
pub fn main() !void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const io = std.Io.Threaded.global_single_threaded.io();
var server = try httpx.Server.init(allocator, io, .{
.host = "127.0.0.1",
.port = 0,
.enableDocs = false,
.maxConnections = 1,
});
defer server.deinit();
// Mount a filesystem directory at /static (relative to the process CWD).
try httpx.static.files.register(&server.router, .{
.root = "examples/static",
.mount = "/static",
.indexFile = "index.html",
});
defer httpx.static.files.unregister();
// ...or the shorthand, equivalent for the common case:
// try server.static("/static", "examples/static");
const thread = try server.start();
defer thread.join();
var response = try httpx.get("http://127.0.0.1:PORT/static/index.html", .{});
defer response.deinit();
}Mount options (all fields use camelCase): root, mount, indexFile, maxFileSize, cacheControl, liveReload, reloadSsePath, spaFallback, filesystem (set false for embedded-only deployments).
Run ​
bash
zig build run-static-filesWhat to Verify ​
- HTML file is served from disk with the
Content-Typematching its extension. ETagis emitted andIf-None-Matchcan return304 Not Modified.- Directories resolve to
index.html;..traversal is rejected with403. - Single-file deployments register the same paths with
httpx.assets.registerEmbeddedand serve them from memory (seezig build run-static-embedded).
