From 459dae22b906ca1c699d7892661ae309482dcb10 Mon Sep 17 00:00:00 2001 From: Jeeves Date: Mon, 25 Mar 2024 20:38:25 -0600 Subject: [PATCH] much much much much better(ish) --- src/main.zig | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main.zig b/src/main.zig index 8db27f1..f5b13b2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -9,20 +9,18 @@ pub fn main() !void { defer _ = gpa.deinit(); const allocator = gpa.allocator(); - // var router = Router.init(allocator, &handle, &handleError); - // defer router.deinit(); - - // try router.addRoute("/", &handle); - const address = try net.Address.parseIp("0.0.0.0", 8080); var listener = Listener.init(.{ .address = address, .allocator = allocator, - .root_handler = &handle, + .root_handler = &handleError, }); defer listener.deinit(); + try listener.router.addRoute("/", &handle); try listener.router.addRoute("/error", &handleError); + try listener.router.addRoute("//pee", &handleError); + try listener.router.addRoute("/error/*", &handleError); try listener.listen(); } @@ -163,6 +161,7 @@ pub const Listener = struct { } pub fn addRoute(self: *Router, path: []const u8, handler: HandlerFn) !void { + // std.debug.print("\npath {s}\n", .{path}); var is_static_route = true; var sections = mem.splitScalar(u8, path, '/'); var node = &self.root_node; @@ -171,8 +170,7 @@ pub const Listener = struct { defer matched_nodes.deinit(); try matched_nodes.append(node); while (sections.next()) |section| { - if (section.len == 0) continue; - std.debug.print("adding section {s}\n", .{section}); + // std.debug.print("adding section {s}\n", .{section}); if (node.children.get(section)) |child| { node = child; } else { @@ -189,12 +187,15 @@ pub const Listener = struct { switch (child_node.type) { .normal => {}, .wildcard => { + // std.debug.print("is wildcard\n", .{}); node.wildcard_child_node = child_node; child_node.param_name = if (section.len > 3) section[3..] else "_"; is_static_route = false; }, .placeholder => { + // std.debug.print("is placeholder\n", .{}); child_node.param_name = if (mem.eql(u8, section, "*")) blk: { + // std.debug.print("is unnamed placeholder #{d}\n", .{unnamed_placeholder_ctr}); const s = try std.fmt.allocPrint(self.allocator, "_{d}", .{unnamed_placeholder_ctr}); // TODO: this will leak unnamed_placeholder_ctr += 1; break :blk s; @@ -211,6 +212,8 @@ pub const Listener = struct { node.handler = handler; + // if (is_static_route) std.debug.print("was static route\n", .{}); + if (is_static_route) try self.static_routes.put(path, node); } @@ -224,8 +227,6 @@ pub const Listener = struct { var node: *Node = &self.root_node; var wildcard_param: ?[]const u8 = null; - // std.debug.print("{any}\n", .{node.handler}); - var sections = mem.splitScalar(u8, path, '/'); while (sections.next()) |section| { if (node.wildcard_child_node) |wildcard_child_node| { @@ -236,7 +237,7 @@ pub const Listener = struct { const next_node = node.children.get(section); if (next_node) |child| { node = child; - std.debug.print("found child_node {any}\n", .{child}); + // std.debug.print("found section {s} child_node {any}\n", .{ section, child.type }); } else { var child_node: ?*Node = null; if (node.placeholder_children.items.len > 1) { @@ -245,7 +246,6 @@ pub const Listener = struct { child_node = node.placeholder_children.items[0]; if (child_node) |n| { - std.debug.print("didn't find child_node {any}\n", .{child_node}); if (n.param_name) |name| try params.put(name, section); params_found = true; node = n; @@ -253,7 +253,7 @@ pub const Listener = struct { } } - // std.debug.print("{any}\n", .{node.handler}); + // std.debug.print("ended up with node {any}\n", .{node.type}); if (wildcard_node) |wildcard| { node = wildcard;