frmdstryr / zhp

A Http server written in Zig
MIT License
355 stars 24 forks source link
http zig

ZHP

status

A (work in progress) Http server written in Zig.

If you have suggestions on improving the design please feel free to comment!

Features

See how it compares in the http benchmarks done by kprotty (now very old).

It's a work in progress... feel free to contribute!

Demo

Try out the demo at https://zhp.codelv.com.

Note: If you try to benchmark the server it'll ban you, please run it locally or on your own server to do benchmarks.

To make and deploy your own app see:

Example

See the example folder for a more detailed example.

const std = @import("std");
const web = @import("zhp");

pub const io_mode = .evented;
pub const log_level = .info;

const MainHandler = struct {
    pub fn get(self: *MainHandler, request: *web.Request, response: *web.Response) !void {
        _ = self;
        _ = request;
        try response.headers.put("Content-Type", "text/plain");
        _ = try response.stream.write("Hello, World!");
    }

};

pub const routes = [_]web.Route{
    web.Route.create("home", "/", MainHandler),
};

pub const middleware = [_]web.Middleware{
    web.Middleware.create(web.middleware.LoggingMiddleware),
};

pub fn main() anyerror!void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer std.debug.assert(!gpa.deinit());
    const allocator = gpa.allocator();

    var app = web.Application.init(allocator, .{.debug=true});
    defer app.deinit();

    try app.listen("127.0.0.1", 9000);
    try app.start();
}