From f39dfd01ab06cf92a3bcad75fcb175a8242ec1b1 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sat, 6 Jun 2026 09:38:56 +0200 Subject: Remove Server struct and replace it with single function --- src/api/root.zig | 5 +++++ src/http/Route.zig | 2 +- src/http/Server.zig | 65 ----------------------------------------------------- src/http/root.zig | 2 +- src/http/serve.zig | 52 ++++++++++++++++++++++++++++++++++++++++++ src/main.zig | 36 +++++------------------------ src/web/root.zig | 27 ++++++++++++++++++++++ 7 files changed, 91 insertions(+), 98 deletions(-) delete mode 100644 src/http/Server.zig create mode 100644 src/http/serve.zig create mode 100644 src/web/root.zig diff --git a/src/api/root.zig b/src/api/root.zig index 5128aa1..7acd953 100644 --- a/src/api/root.zig +++ b/src/api/root.zig @@ -3,6 +3,11 @@ const std = @import("std"); +const http = @import("http"); +const Interface = http.handler.Interface; + +pub const interfaces: []const Interface = &.{}; + test { _ = std.testing.refAllDecls(@This()); } diff --git a/src/http/Route.zig b/src/http/Route.zig index 7dd8ec0..b3f943d 100644 --- a/src/http/Route.zig +++ b/src/http/Route.zig @@ -19,7 +19,7 @@ pub const index: @This() = .{ .segments = &.{} }; pub fn fromAny(comptime route: anytype) @This() { var segments: []const Segment = &.{}; - if (route == .index) { + if (@TypeOf(route) == @EnumLiteral() and route == .index) { return index; } diff --git a/src/http/Server.zig b/src/http/Server.zig deleted file mode 100644 index dc331d8..0000000 --- a/src/http/Server.zig +++ /dev/null @@ -1,65 +0,0 @@ -const std = @import("std"); - -const RouteSet = @import("RouteSet.zig"); - -const Self = @This(); - -server: std.Io.net.Server, -threaded: std.Io.Threaded, -routes: RouteSet, - -pub fn init( - gpa: std.mem.Allocator, - address: std.Io.net.IpAddress, - comptime routes: RouteSet, -) !Self { - var threaded: std.Io.Threaded = .init(gpa, .{}); - - return .{ - .threaded = threaded, - .server = try address.listen(threaded.io(), .{ .reuse_address = true }), - .routes = routes, - }; -} - -pub fn serve(self: *Self, gpa: std.mem.Allocator) !void { - var group: std.Io.Group = .init; - const io = self.threaded.io(); - - while (true) { - const stream = try self.server.accept(self.threaded.io()); - group.async(io, handle, .{self, gpa, stream}); - } -} - -pub fn handle( - self: *Self, - gpa: std.mem.Allocator, - stream: std.Io.net.Stream, -) void { - var read_buffer: [1024 * 8]u8 = undefined; - var write_buffer: [1024 * 8]u8 = undefined; - - defer stream.close(self.threaded.io()); - - var arena: std.heap.ArenaAllocator = .init(gpa); - defer arena.deinit(); - - var reader = stream.reader(self.threaded.io(), &read_buffer); - var writer = stream.writer(self.threaded.io(), &write_buffer); - - var server: std.http.Server = .init(&reader.interface, &writer.interface); - var request = server.receiveHead() catch |e| { - std.debug.print("error: {}\n", .{e}); - return; - }; - - std.debug.print("{s} {s}\n", .{ @tagName(request.head.method), request.head.target }); - - if (self.routes.getInterface(request.head.target)) |interface| { - const message = interface.handle(gpa, arena.allocator(), self.threaded.io(), &request); - message.send(self.threaded.io(), &request) catch return; - } else { - request.respond("", .{ .status = .not_found }) catch return; - } -} diff --git a/src/http/root.zig b/src/http/root.zig index 88d1420..a1cc95c 100644 --- a/src/http/root.zig +++ b/src/http/root.zig @@ -2,7 +2,7 @@ const std = @import("std"); -pub const Server = @import("Server.zig"); +pub const serve = @import("serve.zig").serve; pub const handler = @import("handler/root.zig"); pub const Response = @import("Response.zig"); pub const Route = @import("Route.zig"); diff --git a/src/http/serve.zig b/src/http/serve.zig new file mode 100644 index 0000000..587f4a3 --- /dev/null +++ b/src/http/serve.zig @@ -0,0 +1,52 @@ +const std = @import("std"); + +const RouteSet = @import("RouteSet.zig"); + +pub fn serve( + gpa: std.mem.Allocator, + comptime routes: RouteSet, + address: std.Io.net.IpAddress, +) !void { + var threaded: std.Io.Threaded = .init(gpa, .{}); + var group: std.Io.Group = .init; + const io = threaded.io(); + var server = try address.listen(io, .{ .reuse_address = true }); + + while (true) { + const stream = try server.accept(io); + group.async(io, handle, .{ io, gpa, stream, &routes }); + } +} + +pub fn handle( + io: std.Io, + gpa: std.mem.Allocator, + stream: std.Io.net.Stream, + routes: *const RouteSet, +) void { + var read_buffer: [1024 * 8]u8 = undefined; + var write_buffer: [1024 * 8]u8 = undefined; + + defer stream.close(io); + + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + + var reader = stream.reader(io, &read_buffer); + var writer = stream.writer(io, &write_buffer); + + var server: std.http.Server = .init(&reader.interface, &writer.interface); + var request = server.receiveHead() catch |e| { + std.debug.print("error: {}\n", .{e}); + return; + }; + + std.debug.print("{s} {s}\n", .{ @tagName(request.head.method), request.head.target }); + + if (routes.getInterface(request.head.target)) |interface| { + const message = interface.handle(gpa, arena.allocator(), io, &request); + message.send(io, &request) catch return; + } else { + request.respond("", .{ .status = .not_found }) catch return; + } +} diff --git a/src/main.zig b/src/main.zig index 6d2b9ce..bf1b09f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5,42 +5,16 @@ const std = @import("std"); const http = @import("http"); -const html = @import("html"); -const z = @import("z"); +const api = @import("api"); -const Static = http.handler.Static; +const web = @import("web/root.zig"); -const components = @import("web/component/root.zig").components; - -const document: html.Element = .native(.html, .{}, .children(&.{ - .native(.head, .{}, .children(&.{ - .native(.title, .{}, .content("Memora")), - .native(.meta, .{ - .name = "viewport", - .content = "width=device-width, initial-scale=1.0", - }, .void), - z.script.load(@embedFile("web/pinch.js")), - z.style.load(@embedFile("web/index.css")), - z.env(components), - })), - .native(.body, .{}, .children(&.{ - z.html.load(@embedFile("web/index.html")), - })), -})); - -const routes: http.RouteSet = .init(&.{ - Static(.index, document.toDocument()).interface(), -}); +const routes: http.RouteSet = .init(api.interfaces ++ web.interfaces); pub fn main(init: std.process.Init) !void { - var server: http.Server = try .init( + try http.serve( init.gpa, - try .parseLiteral("0.0.0.0:8080"), routes, + try .parseLiteral("0.0.0.0:8080"), ); - try server.serve(init.gpa); -} - -test { - _ = std.testing.refAllDecls(@This()); } diff --git a/src/web/root.zig b/src/web/root.zig new file mode 100644 index 0000000..c9c1b08 --- /dev/null +++ b/src/web/root.zig @@ -0,0 +1,27 @@ +const z = @import("z"); +const html = @import("html"); +const http = @import("http"); +const Static = http.handler.Static; + +const components = @import("component/root.zig").components; + +const document: html.Element = .native(.html, .{}, .children(&.{ + .native(.head, .{}, .children(&.{ + .native(.title, .{}, .content("Memora")), + .native(.meta, .{ + .name = "viewport", + .content = "width=device-width, initial-scale=1.0", + }, .void), + z.script.load(@embedFile("pinch.js")), + z.style.load(@embedFile("index.css")), + z.env(components), + })), + .native(.body, .{}, .children(&.{ + z.html.load(@embedFile("index.html")), + })), +})); + +pub const interfaces: []const http.handler.Interface = &.{ + Static(.index, document.toDocument()).interface(), +}; + -- cgit v1.2.3-70-g09d2