much better(ish)
This commit is contained in:
parent
224157889a
commit
328391a6a3
1 changed files with 14 additions and 13 deletions
27
src/main.zig
27
src/main.zig
|
@ -18,8 +18,7 @@ pub fn main() !void {
|
||||||
var listener = Listener.init(.{
|
var listener = Listener.init(.{
|
||||||
.address = address,
|
.address = address,
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
// .handler = router.handle,
|
.root_handler = &handle,
|
||||||
.handler = &handle,
|
|
||||||
});
|
});
|
||||||
defer listener.deinit();
|
defer listener.deinit();
|
||||||
try listener.listen();
|
try listener.listen();
|
||||||
|
@ -36,23 +35,25 @@ pub fn handleError(event: *Listener.Event) anyerror!void {
|
||||||
pub const Listener = struct {
|
pub const Listener = struct {
|
||||||
address: net.Address,
|
address: net.Address,
|
||||||
arena: heap.ArenaAllocator,
|
arena: heap.ArenaAllocator,
|
||||||
handlerFn: HandlerFn,
|
|
||||||
|
router: Router,
|
||||||
|
|
||||||
pub const Options = struct {
|
pub const Options = struct {
|
||||||
address: net.Address,
|
address: net.Address,
|
||||||
allocator: mem.Allocator,
|
allocator: mem.Allocator,
|
||||||
handler: HandlerFn,
|
root_handler: HandlerFn,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn init(options: Options) Listener {
|
pub fn init(options: Options) Listener {
|
||||||
return .{
|
return .{
|
||||||
.address = options.address,
|
.address = options.address,
|
||||||
.arena = heap.ArenaAllocator.init(options.allocator),
|
.arena = heap.ArenaAllocator.init(options.allocator),
|
||||||
.handlerFn = options.handler,
|
.router = Router.init(options.allocator, options.root_handler),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Listener) void {
|
pub fn deinit(self: *Listener) void {
|
||||||
|
self.router.deinit();
|
||||||
self.arena.deinit();
|
self.arena.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +90,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.handlerFn(&event);
|
try self.router.handle(&event);
|
||||||
|
|
||||||
try self.respondFromEvent(&event, &req);
|
try self.respondFromEvent(&event, &req);
|
||||||
}
|
}
|
||||||
|
@ -131,9 +132,7 @@ pub const Listener = struct {
|
||||||
root_node: Node,
|
root_node: Node,
|
||||||
static_routes: std.StringHashMap(*Node),
|
static_routes: std.StringHashMap(*Node),
|
||||||
|
|
||||||
error_handler: HandlerFn,
|
pub fn init(allocator: mem.Allocator, root_handler: HandlerFn) Router {
|
||||||
|
|
||||||
pub fn init(allocator: mem.Allocator, root_handler: HandlerFn, error_handler: HandlerFn) Router {
|
|
||||||
return .{
|
return .{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.root_node = .{
|
.root_node = .{
|
||||||
|
@ -144,16 +143,18 @@ pub const Listener = struct {
|
||||||
.placeholder_children = std.ArrayList(*Node).init(allocator),
|
.placeholder_children = std.ArrayList(*Node).init(allocator),
|
||||||
},
|
},
|
||||||
.static_routes = std.StringHashMap(*Node).init(allocator),
|
.static_routes = std.StringHashMap(*Node).init(allocator),
|
||||||
.error_handler = error_handler,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Router) void {
|
pub fn deinit(self: *Router) void {
|
||||||
|
self.root_node.children.deinit();
|
||||||
|
self.root_node.placeholder_children.deinit();
|
||||||
self.static_routes.deinit();
|
self.static_routes.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle(self: *Router, event: *Listener.Event) anyerror!void {
|
pub fn handle(self: *Router, event: *Listener.Event) !void {
|
||||||
try self.route(event.req.uri.path)(self, event);
|
const handlerFn = self.route(event.req.uri.path);
|
||||||
|
try handlerFn(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn addRoute(self: *Router, path: []const u8, handler: HandlerFn) !void {
|
pub fn addRoute(self: *Router, path: []const u8, handler: HandlerFn) !void {
|
||||||
|
@ -173,7 +174,7 @@ pub const Listener = struct {
|
||||||
pub fn route(self: *Router, path: []const u8) HandlerFn {
|
pub fn route(self: *Router, path: []const u8) HandlerFn {
|
||||||
if (self.static_routes.get(path)) |rt| return rt.handler;
|
if (self.static_routes.get(path)) |rt| return rt.handler;
|
||||||
if (self.root_node.wildcard_child_node) |node| return node.handler;
|
if (self.root_node.wildcard_child_node) |node| return node.handler;
|
||||||
return self.error_handler;
|
return self.root_node.handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const Node = struct {
|
pub const Node = struct {
|
||||||
|
|
Loading…
Add table
Reference in a new issue