better(ish)

This commit is contained in:
Jeeves 2024-03-25 17:36:13 -06:00
parent f0f072ebab
commit 224157889a

View file

@ -4,48 +4,43 @@ const net = std.net;
const http = std.http; const http = std.http;
const heap = std.heap; const heap = std.heap;
const App = Listener(Router);
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var router = Router.init(allocator, &handle, &handleError); // var router = Router.init(allocator, &handle, &handleError);
defer router.deinit(); // defer router.deinit();
try router.addRoute("/", &handle); // try router.addRoute("/", &handle);
const address = try net.Address.parseIp("0.0.0.0", 8080); const address = try net.Address.parseIp("0.0.0.0", 8080);
var listener = App.init(.{ var listener = Listener.init(.{
.address = address, .address = address,
.allocator = allocator, .allocator = allocator,
.context = router, // .handler = router.handle,
.handler = router.handle, .handler = &handle,
}); });
defer listener.deinit(); defer listener.deinit();
try listener.listen(); try listener.listen();
} }
pub fn handle(_: *Router, event: *App.Event) anyerror!void { pub fn handle(event: *Listener.Event) anyerror!void {
try event.res.body.appendSlice("hello there"); try event.res.body.appendSlice("hello there");
} }
pub fn handleError(_: Router, event: *App.Event) anyerror!void { pub fn handleError(event: *Listener.Event) anyerror!void {
try event.res.body.appendSlice("ahoy, an error occurred"); try event.res.body.appendSlice("ahoy, an error occurred");
} }
pub fn Listener(comptime Context: type) type { pub const Listener = struct {
return struct {
address: net.Address, address: net.Address,
arena: heap.ArenaAllocator, arena: heap.ArenaAllocator,
ctx: *Context,
handlerFn: HandlerFn, handlerFn: HandlerFn,
pub const Options = struct { pub const Options = struct {
address: net.Address, address: net.Address,
allocator: mem.Allocator, allocator: mem.Allocator,
context: *Context,
handler: HandlerFn, handler: HandlerFn,
}; };
@ -53,7 +48,6 @@ pub fn Listener(comptime Context: type) type {
return .{ return .{
.address = options.address, .address = options.address,
.arena = heap.ArenaAllocator.init(options.allocator), .arena = heap.ArenaAllocator.init(options.allocator),
.ctx = options.context,
.handlerFn = options.handler, .handlerFn = options.handler,
}; };
} }
@ -95,7 +89,7 @@ pub fn Listener(comptime Context: type) type {
var header_it = req.iterateHeaders(); var header_it = req.iterateHeaders();
while (header_it.next()) |header| try event.req.headers.append(&header); while (header_it.next()) |header| try event.req.headers.append(&header);
try self.handlerFn(self.ctx, &event); try self.handlerFn(&event);
try self.respondFromEvent(&event, &req); try self.respondFromEvent(&event, &req);
} }
@ -129,11 +123,9 @@ pub fn Listener(comptime Context: type) type {
}; };
}; };
pub const HandlerFn = *const fn (ctx: *Context, event: *Event) anyerror!void; pub const HandlerFn = *const fn (event: *Event) anyerror!void;
};
}
pub const Router = struct { pub const Router = struct {
allocator: mem.Allocator, allocator: mem.Allocator,
root_node: Node, root_node: Node,
@ -195,6 +187,5 @@ pub const Router = struct {
pub const Type = enum { normal, wildcard, placeholder }; pub const Type = enum { normal, wildcard, placeholder };
}; };
};
pub const HandlerFn = *const fn (self: *Router, event: *App.Event) anyerror!void;
}; };