aboutsummaryrefslogtreecommitdiff
path: root/src/http/handler/Interface.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-06-06 07:57:50 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-06-06 07:57:50 +0200
commit51307d973255e32ec1440083dee383e8b2cd8878 (patch)
treedac0309c7abd40d2a3822fca3f7df0fd485e04f0 /src/http/handler/Interface.zig
parent17561c6be1e4fba11e243f2ac842ebe26fae7e60 (diff)
Implement static handler and synchronous server handling
Diffstat (limited to 'src/http/handler/Interface.zig')
-rw-r--r--src/http/handler/Interface.zig44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/http/handler/Interface.zig b/src/http/handler/Interface.zig
new file mode 100644
index 0000000..2238424
--- /dev/null
+++ b/src/http/handler/Interface.zig
@@ -0,0 +1,44 @@
+const std = @import("std");
+const Response = @import("../Response.zig");
+const Route = @import("../Route.zig");
+
+route: Route,
+base: *const anyopaque,
+handler: *const fn (
+ *const anyopaque,
+ std.mem.Allocator,
+ std.mem.Allocator,
+ std.Io,
+ *std.http.Server.Request,
+) Response,
+
+pub fn from(instance: anytype, route: Route) @This() {
+ const PtrT = @TypeOf(instance);
+ const func = struct {
+ pub fn handle(
+ base: *const anyopaque,
+ gpa: std.mem.Allocator,
+ arena: std.mem.Allocator,
+ io: std.Io,
+ request: *std.http.Server.Request,
+ ) Response {
+ return @as(PtrT, @ptrCast(@alignCast(base))).handle(gpa, arena, io, request);
+ }
+ }.handle;
+
+ return .{
+ .base = @ptrCast(instance),
+ .handler = func,
+ .route = route,
+ };
+}
+
+pub fn handle(
+ self: *const @This(),
+ gpa: std.mem.Allocator,
+ arena: std.mem.Allocator,
+ io: std.Io,
+ request: *std.http.Server.Request,
+) Response {
+ return self.handler(self.base, gpa, arena, io, request);
+}