pmbanugo/gotham
HTTP server library/framework for Zig (early & experimental)
master
master
Gotham is a concurrent, high-performance HTTP server library written in Zig. It is designed to be lightweight, efficient, and easy to use, making it an excellent choice for building web applications and frameworks. The design principles/style are largely inspired by uWebSockets and h2o, with the aim to deliver similar performance and scalability.
Its goal is to provide a robust foundation for developers who need a fast and reliable HTTP server without the overhead of larger frameworks. Those can be broken down to include:
Gotham is in early development but functional for experimentation and hobby projects. Here's what's currently available:
The library is transitioning from experimental code to a usable library interface. Performance testing shows promising results (~122k req/s, single-threaded), but more optimization and features are planned.
Gotham isn't ready to be used as a library yet. However, you can run the example to see how it works.
I believe I've reached a point of my experimentation where I can start tuning it to be used as a library. The current code is still in an early stage, but it is functional.
The current example demonstrates a simple HTTP server that responds with a "Hello World" message. To run the example, open your terminal and execute the following commands:
git clone [email protected]:pmbanugo/gotham.git
cd gotham
zig build run
curl -v http://localhost:8080
You can change what the response is by modifying the defaultRequestHandler
function in src/main.zig
. For example, you can change it to return the request path:
fn defaultRequestHandler(arena_allocator: std.mem.Allocator, request: *const parser.HttpRequest, response: *HttpResponse) void {
_ = arena_allocator; // Allocator might be used for dynamic responses
_ = response.writeStatus(.ok);
_ = response.writeHeader("Content-Type", "text/plain");
_ = response.writeHeader("Server", "Gotham/0.1");
response.end(request.path) catch |err| {
log.err("Failed to send response in default handler: {}", .{err});
};
}
Then, run the server (zig build -Doptimize=ReleaseFast run
) and make a request to see the new response.
Hello via default handler!
response (measured with zig build -Doptimize=ReleaseFast
and oha -z 20s --no-tui http://localhost:3000
). Those will likely change as I work on adding more features and optimizations.