From 4d18f031a8a9f16de204306d24c8a83b23411ed2 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Sat, 23 Apr 2022 03:18:46 -0700 Subject: [PATCH] Remove circular dependency --- src/main.zig | 6 +++--- src/router.zig | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main.zig b/src/main.zig index 5ba6e7e..b4533ba 100644 --- a/src/main.zig +++ b/src/main.zig @@ -21,8 +21,8 @@ pub const routes = [_]Route{ const this_scheme = "http"; const this_host = "localhost:8080"; -fn getUser(ctx: *http.Context) anyerror!void { - const id_str = ctx.request.arg("id"); +fn getUser(ctx: *http.Context, route: *const router.Route) anyerror!void { + const id_str = route.arg("id", ctx.request.path); const host = ctx.request.headers.get("host") orelse { try ctx.response.statusOnly(400); @@ -52,7 +52,7 @@ fn getUser(ctx: *http.Context) anyerror!void { fn staticString(comptime str: []const u8) Route.Handler { return (struct { - fn func(ctx: *http.Context) anyerror!void { + fn func(ctx: *http.Context, _: *const router.Route) anyerror!void { try ctx.response.headers.put("Content-Type", "text/plain"); try ctx.response.write(200, str); } diff --git a/src/router.zig b/src/router.zig index 23c2ed9..6fb5bbc 100644 --- a/src/router.zig +++ b/src/router.zig @@ -11,7 +11,7 @@ pub const Route = struct { literal: []const u8, }; - pub const Handler = fn (*Context) callconv(.Async) anyerror!void; + pub const Handler = fn (*Context, *const Route) callconv(.Async) anyerror!void; fn normalize(comptime path: []const u8) []const u8 { var arr: [path.len]u8 = undefined; @@ -159,7 +159,7 @@ pub fn routeRequest(ctx: *Context) !void { var buf = try ctx.allocator.allocWithOptions(u8, @frameSize(route.handler), 8, null); defer ctx.allocator.free(buf); - return await @asyncCall(buf, {}, route.handler, .{ctx}); + return await @asyncCall(buf, {}, route.handler, .{ ctx, route }); } }