From 80678c1cf9d19c07a75f9c0413fa8e9314bbce24 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Wed, 11 Jan 2023 20:56:08 +0100 Subject: [PATCH] zap progress --- build.zig | 3 +- examples/hello/hello.zig | 48 ++++++++++-------------------- src/deps/facilio.zig | 63 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 33 deletions(-) diff --git a/build.zig b/build.zig index a56382b..de31e1a 100644 --- a/build.zig +++ b/build.zig @@ -30,7 +30,8 @@ pub fn build(b: *std.build.Builder) !void { const example_build_step = b.addInstallArtifact(example); // only after the ensure step example_build_step.step.dependOn(ensure_step); - // via `zig build example` invoked step depends on the installed exe + // the step invoked via `zig build example` on the installed exe which + // itself depends on the "ensure" step example_step.dependOn(&example_build_step.step); } diff --git a/examples/hello/hello.zig b/examples/hello/hello.zig index c22c2a0..4fa3bde 100644 --- a/examples/hello/hello.zig +++ b/examples/hello/hello.zig @@ -1,39 +1,23 @@ const std = @import("std"); -const facilio = @import("facilio"); +const zap = @import("facilio"); -fn on_request(request: [*c]facilio.C.http_s) callconv(.C) void { - std.debug.print("REQUEST!\n", .{}); - var msg: []const u8 = "Hello from ZAP!"; - _ = facilio.sendBody(request, msg); +fn on_request(request: [*c]zap.C.http_s) callconv(.C) void { + std.debug.print("GOT A REQUEST!\n", .{}); + _ = zap.sendBody(request, "Hello from ZAP!!!"); } -pub fn main() void { - if (facilio.C.http_listen("3000", null, .{ - .on_request = on_request, - .log = 1, - .on_upgrade = null, - .on_response = null, - .on_finish = null, - .udata = null, - .public_folder = null, - .public_folder_length = 0, - .max_header_size = 4096, - .max_body_size = 4096, - .max_clients = 42, - .tls = null, - .reserved1 = 0, - .reserved2 = 0, - .reserved3 = 0, - .ws_max_msg_size = 250 * 1024, - .timeout = 0, - .ws_timeout = 0, - .is_client = 0, - }) == -1) { - // listen failed - std.debug.print("Listening failed\n", .{}); - return; - } - facilio.start(.{ +pub fn main() !void { + // configure + var listen_settings = zap.ListenSettings.init(); + listen_settings.on_request = on_request; + listen_settings.log = true; + + // listen + try zap.listen("3000", null, listen_settings); + std.debug.print("Listening on port 3000\n", .{}); + + // start working + zap.start(.{ .threads = 4, .workers = 4, }); diff --git a/src/deps/facilio.zig b/src/deps/facilio.zig index 96002a0..7829e3e 100644 --- a/src/deps/facilio.zig +++ b/src/deps/facilio.zig @@ -1,4 +1,5 @@ // zig type definitions for facilio lib +// or maybe let's just make it zap directly... pub const C = @cImport({ @cInclude("http.h"); @@ -15,3 +16,65 @@ pub fn sendBody(request: [*c]C.http_s, body: []const u8) void { pub fn start(args: C.fio_start_args) void { C.fio_start(args); } + +const ListenError = error{ + ValueNotZeroTerminated, + ListenError, +}; + +pub fn listen(port: [*c]const u8, interface: [*c]const u8, settings: ListenSettings) ListenError!void { + var pfolder: [*c]const u8 = null; + var pfolder_len: usize = 0; + + if (settings.public_folder) |pf| { + pfolder_len = pf.len; + // TODO: make sure it's 0-terminated!!! + if (pf[pf.len - 1] != 0) { + return error.ValueNotZeroTerminated; + } + pfolder = pf.ptr; + } + + if (C.http_listen(port, interface, .{ + .on_request = settings.on_request, + .on_upgrade = settings.on_upgrade, + .on_response = settings.on_response, + .on_finish = settings.on_finish, + .udata = null, + .public_folder = pfolder, + .public_folder_length = pfolder_len, + .max_header_size = settings.max_header_size, + .max_body_size = settings.max_body_size, + .max_clients = settings.max_clients, + .tls = null, + .reserved1 = 0, + .reserved2 = 0, + .reserved3 = 0, + .ws_max_msg_size = 0, + .timeout = settings.keepalive_timeout_s, + .ws_timeout = 0, + .log = if (settings.log) 1 else 0, + .is_client = 0, + }) == -1) { + return error.ListenError; + } +} + +pub const ListenSettings = struct { + on_request: ?*const fn ([*c]C.http_s) callconv(.C) void = null, + on_upgrade: ?*const fn ([*c]C.http_s, [*c]u8, usize) callconv(.C) void = null, + on_response: ?*const fn ([*c]C.http_s) callconv(.C) void = null, + on_finish: ?*const fn ([*c]C.struct_http_settings_s) callconv(.C) void = null, + public_folder: ?[]const u8 = null, + max_header_size: usize = 32 * 1024, + max_body_size: usize = 50 * 1024 * 1024, + max_clients: isize = 100, + keepalive_timeout_s: u8 = 5, + log: bool = false, + + const Self = @This(); + + pub fn init() Self { + return .{}; + } +};