@@ -62,20 +62,19 @@ pub export fn run_server(
6262 return ;
6363 };
6464
65- runServer (allocator , server_addr , server_port , routes , 0 , & should_exit ) catch | err | {
65+ var router = Router .init (arena_allocator , routes );
66+
67+ runServer (allocator , server_addr , server_port , & router , & should_exit ) catch | err | {
6668 log .err ("error running server: {any}" , .{err });
6769 return ;
6870 };
6971}
7072
71- /// stop_iter will stop the server after stop_iter iterations, unless stop_iter is 0. This is for testing,
72- /// so as to prevent blocking
7373fn runServer (
7474 allocator : std.mem.Allocator ,
7575 server_addr : [* :0 ]const u8 ,
7676 server_port : u16 ,
77- routes : []Route ,
78- stop_iter : u16 ,
77+ router : * Router ,
7978 exit : * bool ,
8079) ! void {
8180 const server_addr_slice = std .mem .span (server_addr );
@@ -115,16 +114,9 @@ fn runServer(
115114
116115 server_is_running = true ;
117116
118- var num_iters : u16 = 0 ;
119117 // Continue checking for new connections. New connections are given a separate thread to be handled in.
120118 // This thread will continue waiting for requests on the same connection until the connection is closed.
121119 while (! exit .* ) {
122- if (stop_iter > 0 and num_iters >= stop_iter ) {
123- break ;
124- }
125- if (stop_iter > 0 ) {
126- num_iters += 1 ;
127- }
128120 const connection = server .accept () catch | err | {
129121 if (err == error .WouldBlock ) {
130122 std .Thread .sleep (10 * std .time .ns_per_ms );
@@ -140,9 +132,11 @@ fn runServer(
140132 // Give each new connection a new thread.
141133 // TODO: This should probably be a threadpool, and the closure of threads handled properly
142134 const thread = std .Thread .spawn (
143- .{},
135+ .{
136+ .allocator = allocator ,
137+ },
144138 handleConnection ,
145- .{ allocator , connection , routes },
139+ .{ allocator , & connection , router },
146140 ) catch | err | {
147141 log .err ("failed to spawn thread: {any}" , .{err });
148142 continue ;
@@ -154,30 +148,7 @@ fn runServer(
154148 log .info ("Shutting down..." , .{});
155149}
156150
157- /// Function to wrap runServer for use in threads, since error returning functions can't be used
158- /// as thread functions. Panics on err, which is fine, since this is used for testing only
159- fn runServerWithErrorHandler (
160- allocator : std.mem.Allocator ,
161- server_addr : [* :0 ]const u8 ,
162- server_port : u16 ,
163- routes : []Route ,
164- stop_iter : u16 ,
165- exit : * bool ,
166- ) void {
167- runServer (
168- allocator ,
169- server_addr ,
170- server_port ,
171- routes ,
172- stop_iter ,
173- exit ,
174- ) catch | err | {
175- log .err ("error running server: {any}" , .{err });
176- @panic ("error running server in runServerWithErrorHandler" );
177- };
178- }
179-
180- fn handleConnection (allocator : std.mem.Allocator , connection : std.net.Server.Connection , routes : []Route ) void {
151+ fn handleConnection (allocator : std.mem.Allocator , connection : * const std.net.Server.Connection , router : * Router ) void {
181152 var recv_header : [4000 ]u8 = undefined ;
182153 var send_header : [4000 ]u8 = undefined ;
183154 var conn_reader = connection .stream .reader (& recv_header );
@@ -225,7 +196,7 @@ fn handleConnection(allocator: std.mem.Allocator, connection: std.net.Server.Con
225196 const head = request .head ;
226197
227198 const logging_middleware = middleware .Logging .init ();
228- const status = handleRequest (allocator , routes , & request ) catch | err | {
199+ const status = handleRequest (allocator , router , & request ) catch | err | {
229200 log .err ("Error calling handleRequest in handleConnection(): {}" , .{err });
230201 if (@errorReturnTrace ()) | trace | {
231202 std .debug .dumpStackTrace (trace .* );
@@ -387,7 +358,7 @@ fn handleStaticRoute(request: *std.http.Server.Request) !std.http.Status {
387358 return status ;
388359}
389360
390- fn handleRequest (allocator : std.mem.Allocator , routes : [] Route , request : * std.http.Server.Request ) ! std.http.Status {
361+ fn handleRequest (allocator : std.mem.Allocator , router : * Router , request : * std.http.Server.Request ) ! std.http.Status {
391362 log .debug ("Handling request for {s}" , .{request .head .target });
392363
393364 // CORS middleware will respond to request if allowed is false
@@ -406,7 +377,6 @@ fn handleRequest(allocator: std.mem.Allocator, routes: []Route, request: *std.ht
406377 defer arena .deinit ();
407378 const arena_allocator = arena .allocator ();
408379
409- var router = Router .init (arena_allocator , routes );
410380 var matched_route = try router .match (request .head .method , request .head .target );
411381 // const route = routing.getRoute(routes, request.head.target);
412382 if (matched_route == null ) {
0 commit comments