From b197fb5ff7f46d2ab2dde893e81985ef3188973f Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Fri, 21 Apr 2023 22:18:22 +0200 Subject: [PATCH] housekeeping --- examples/endpoint/endpoint.zig | 4 ++++ examples/endpoint/main.zig | 2 ++ examples/endpoint/users.zig | 4 ++++ src/endpoint.zig | 16 +++++++++------- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/examples/endpoint/endpoint.zig b/examples/endpoint/endpoint.zig index 1a3ac80..6335075 100644 --- a/examples/endpoint/endpoint.zig +++ b/examples/endpoint/endpoint.zig @@ -28,6 +28,10 @@ pub fn init( }; } +pub fn deinit(self: *Self) void { + self.users.deinit(); +} + pub fn getUsers(self: *Self) *Users { return &self.users; } diff --git a/examples/endpoint/main.zig b/examples/endpoint/main.zig index 9f60af5..586cfea 100644 --- a/examples/endpoint/main.zig +++ b/examples/endpoint/main.zig @@ -20,8 +20,10 @@ pub fn main() !void { .max_body_size = 100 * 1024 * 1024, }, ); + defer listener.deinit(); var endpoint = Endpoint.init(allocator, "/users"); + defer endpoint.deinit(); // add endpoint try listener.addEndpoint(endpoint.getUserEndpoint()); diff --git a/examples/endpoint/users.zig b/examples/endpoint/users.zig index 3eea1bb..75c114e 100644 --- a/examples/endpoint/users.zig +++ b/examples/endpoint/users.zig @@ -29,6 +29,10 @@ pub fn init(a: std.mem.Allocator) Self { }; } +pub fn deinit(self: *Self) void { + self.users.deinit(); +} + // the request will be freed (and its mem reused by facilio) when it's // completed, so we take copies of the names pub fn addByName(self: *Self, first: ?[]const u8, last: ?[]const u8) !usize { diff --git a/src/endpoint.zig b/src/endpoint.zig index 7410666..3b19b6a 100644 --- a/src/endpoint.zig +++ b/src/endpoint.zig @@ -160,9 +160,6 @@ pub const EndpointListenerError = error{ EndpointPathShadowError, }; -// var endpoints: std.StringHashMap(*SimpleEndpoint) = undefined; -var endpoints: std.ArrayList(*SimpleEndpoint) = undefined; - // NOTE: We switch on path.startsWith -> so use endpoints with distinctly // starting names!! pub const SimpleEndpointListener = struct { @@ -171,10 +168,15 @@ pub const SimpleEndpointListener = struct { const Self = @This(); + /// static struct member endpoints + var endpoints: std.ArrayList(*SimpleEndpoint) = undefined; + pub fn init(a: std.mem.Allocator, l: ListenerSettings) Self { - var ls = l; - ls.on_request = onRequest; endpoints = std.ArrayList(*SimpleEndpoint).init(a); + + var ls = l; // take copy of listener settings + ls.on_request = onRequest; + return .{ .listener = Listener.init(ls), .allocator = a, @@ -186,11 +188,11 @@ pub const SimpleEndpointListener = struct { endpoints.deinit(); } - pub fn listen(self: *SimpleEndpointListener) !void { + pub fn listen(self: *Self) !void { try self.listener.listen(); } - pub fn addEndpoint(self: *SimpleEndpointListener, e: *SimpleEndpoint) !void { + pub fn addEndpoint(self: *Self, e: *SimpleEndpoint) !void { _ = self; for (endpoints.items) |other| { if (std.mem.startsWith(