1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 23:24:09 +00:00

zap progress

This commit is contained in:
Rene Schallner 2023-01-11 20:56:08 +01:00
parent be41be49ea
commit 80678c1cf9
3 changed files with 81 additions and 33 deletions

View file

@ -30,7 +30,8 @@ pub fn build(b: *std.build.Builder) !void {
const example_build_step = b.addInstallArtifact(example); const example_build_step = b.addInstallArtifact(example);
// only after the ensure step // only after the ensure step
example_build_step.step.dependOn(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); example_step.dependOn(&example_build_step.step);
} }

View file

@ -1,39 +1,23 @@
const std = @import("std"); const std = @import("std");
const facilio = @import("facilio"); const zap = @import("facilio");
fn on_request(request: [*c]facilio.C.http_s) callconv(.C) void { fn on_request(request: [*c]zap.C.http_s) callconv(.C) void {
std.debug.print("REQUEST!\n", .{}); std.debug.print("GOT A REQUEST!\n", .{});
var msg: []const u8 = "Hello from ZAP!"; _ = zap.sendBody(request, "Hello from ZAP!!!");
_ = facilio.sendBody(request, msg);
} }
pub fn main() void { pub fn main() !void {
if (facilio.C.http_listen("3000", null, .{ // configure
.on_request = on_request, var listen_settings = zap.ListenSettings.init();
.log = 1, listen_settings.on_request = on_request;
.on_upgrade = null, listen_settings.log = true;
.on_response = null,
.on_finish = null, // listen
.udata = null, try zap.listen("3000", null, listen_settings);
.public_folder = null, std.debug.print("Listening on port 3000\n", .{});
.public_folder_length = 0,
.max_header_size = 4096, // start working
.max_body_size = 4096, zap.start(.{
.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(.{
.threads = 4, .threads = 4,
.workers = 4, .workers = 4,
}); });

View file

@ -1,4 +1,5 @@
// zig type definitions for facilio lib // zig type definitions for facilio lib
// or maybe let's just make it zap directly...
pub const C = @cImport({ pub const C = @cImport({
@cInclude("http.h"); @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 { pub fn start(args: C.fio_start_args) void {
C.fio_start(args); 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 .{};
}
};