this ain't it, chief

This commit is contained in:
Jeeves 2024-03-25 15:58:40 -06:00
parent e538cbbcef
commit 65a426a786

View file

@ -9,28 +9,29 @@ pub fn main() !void {
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const allocator = gpa.allocator(); const allocator = gpa.allocator();
const router = BasicRouter.init(allocator);
defer router.deinit();
const address = try net.Address.parseIp("0.0.0.0", 8080); const address = try net.Address.parseIp("0.0.0.0", 8080);
var listener = Listener.init(address, allocator, &handler); var listener = Listener.init(address, allocator, &handle);
defer listener.deinit(); defer listener.deinit();
try listener.listen(); try listener.listen();
} }
pub fn handler(event: *Listener.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 const Router = struct {};
pub const Listener = struct { pub const Listener = struct {
address: net.Address, address: net.Address,
arena: heap.ArenaAllocator, arena: heap.ArenaAllocator,
handler: *const fn (event: *Event) anyerror!void, handlerFn: *const fn (event: *Event) anyerror!void,
pub fn init(address: net.Address, allocator: mem.Allocator, hand: *const fn (event: *Event) anyerror!void) Listener { pub fn init(address: net.Address, allocator: mem.Allocator, handler: *const fn (event: *Event) anyerror!void) Listener {
return .{ return .{
.address = address, .address = address,
.arena = heap.ArenaAllocator.init(allocator), .arena = heap.ArenaAllocator.init(allocator),
.handler = hand, .handlerFn = handler,
}; };
} }
@ -71,7 +72,7 @@ pub const Listener = struct {
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.handler(&event); try self.handlerFn(&event);
try self.respondFromEvent(&event, &req); try self.respondFromEvent(&event, &req);
} }
@ -105,3 +106,56 @@ pub const Listener = struct {
}; };
}; };
}; };
pub const BasicRouter = Router(Listener.Event);
pub fn Router(comptime Context: type) type {
return struct {
ctx: Context,
allocator: mem.Allocator,
static_routes: std.StringHashMap(*Node),
pub fn init(allocator: mem.Allocator, ctx: Context) Self {
return .{
.ctx = ctx,
.allocator = allocator,
.static_routes = std.StringHashMap(*Node).init(allocator),
};
}
pub fn deinit(self: *Self) void {
self.static_routes.deinit();
}
pub fn addRoute(self: *Self, path: []const u8, handler: HandlerFn) !void {
try self.addStaticRoute(path, handler);
}
fn addStaticRoute(self: *Self, path: []const u8, handler: HandlerFn) !void {
try self.static_routes.put(path, .{
.type = .normal,
.max_depth = 1,
.children = std.StringHashMap(*Node).init(self.allocator),
.handler = handler,
.placeholder_children = std.ArrayList(*Node).init(self.allocator),
});
}
pub const Node = struct {
type: Type,
max_depth: usize, // TODO: what is best here
parent: ?*Node = null,
children: std.StringHashMap(*Node),
handler: HandlerFn,
wildcard_child_node: ?*Node = null,
placeholder_children: std.ArrayList(*Node),
pub const Type = enum { normal, wildcard, placeholder };
};
pub const HandlerFn = *const fn (ctx: *Context) anyerror!void;
const Self = @This();
};
}