Skip to content

Commit 9b87900

Browse files
committed
Create Router and pass by pointer
1 parent 6f9241e commit 9b87900

2 files changed

Lines changed: 11 additions & 173 deletions

File tree

src/volt.zig

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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
7373
fn 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) {

volt/components.py

Lines changed: 0 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -119,135 +119,3 @@ def __init__(self, block_name: str, template_name: str, message: str | None = No
119119
message or f"Block {block_name} not in template {template_name}"
120120
)
121121

122-
123-
#
124-
# # NOTE: Not sure how _this_ would be generated
125-
# class NavSelected(StrEnum):
126-
# HOME = "home"
127-
# FEATURES = "features"
128-
# DEMO = "demo"
129-
# PERFORMANCE = "performance"
130-
# QUICKSTART = "quickstart"
131-
#
132-
#
133-
# class NavBar(Component):
134-
# template_name: str = "base.html"
135-
# block_name: str = "navbar"
136-
#
137-
# @dataclass
138-
# class Context(Component.Context):
139-
# selected: NavSelected
140-
#
141-
# def __init__(self, context: Context) -> None:
142-
# super().__init__(context)
143-
#
144-
#
145-
# class Base(Component):
146-
# template_name: str = "base.html"
147-
#
148-
# @dataclass
149-
# class Context(Component.Context):
150-
# selected: NavSelected
151-
#
152-
# def __init__(self, context: Context) -> None:
153-
# super().__init__(context)
154-
#
155-
#
156-
# # vvv Tentatively thinking these component classes should be generated? vvv #
157-
# class Home(Base):
158-
# template_name: str = "home.html"
159-
#
160-
# @dataclass
161-
# class Context(Base.Context): ...
162-
#
163-
# def __init__(self, context: Context) -> None:
164-
# super().__init__(context)
165-
#
166-
#
167-
# class Features(Base):
168-
# template_name: str = "features.html"
169-
#
170-
# @dataclass
171-
# class Context(Base.Context): ...
172-
#
173-
# def __init__(self, context: Context) -> None:
174-
# super().__init__(context)
175-
#
176-
#
177-
# @dataclass
178-
# class ProgrammingLanguage:
179-
# name: str
180-
# abbrev: str
181-
# description: str
182-
# category: str
183-
# text_colour: str
184-
# bg_colour: str
185-
#
186-
#
187-
# class DemoLanguages(Component):
188-
# template_name: str = "demo.html"
189-
# block_name: str = "programming_language_list"
190-
#
191-
# @dataclass
192-
# class Context(Component.Context):
193-
# programming_languages: list[ProgrammingLanguage]
194-
# searching: bool
195-
#
196-
# def __init__(self, context: Context) -> None:
197-
# super().__init__(context)
198-
#
199-
#
200-
# class DemoCounter(Component):
201-
# template_name: str = "demo.html"
202-
# block_name: str = "counter"
203-
#
204-
# @dataclass
205-
# class Context(Component.Context):
206-
# value: int
207-
#
208-
# def __init__(self, context: Context) -> None:
209-
# super().__init__(context)
210-
#
211-
#
212-
# class DemoTasks(Component):
213-
# template_name: str = "demo.html"
214-
# block_name: str = "task_list"
215-
#
216-
# @dataclass
217-
# class Context(Component.Context):
218-
# tasks: list[str]
219-
#
220-
# def __init__(self, context: Context) -> None:
221-
# super().__init__(context)
222-
#
223-
# @dataclass
224-
# class ChatMessage:
225-
# message: str
226-
# time: datetime
227-
#
228-
# class DemoChatMessages(Component):
229-
# template_name: str = "demo.html"
230-
# block_name: str = "chat_messages"
231-
#
232-
# @dataclass
233-
# class Context(Component.Context):
234-
# messages: list[ChatMessage]
235-
#
236-
# def __init__(self, context: Context) -> None:
237-
# super().__init__(context)
238-
#
239-
# class Demo(Base):
240-
# template_name: str = "demo.html"
241-
#
242-
# @dataclass
243-
# class Context(
244-
# Base.Context,
245-
# DemoLanguages.Context,
246-
# DemoCounter.Context,
247-
# DemoTasks.Context,
248-
# ):
249-
# tasks: list[str]
250-
# value: int
251-
#
252-
# def __init__(self, context: Context) -> None:
253-
# super().__init__(context)

0 commit comments

Comments
 (0)