diff options
| -rw-r--r-- | src/api/root.zig | 5 | ||||
| -rw-r--r-- | src/http/Route.zig | 2 | ||||
| -rw-r--r-- | src/http/root.zig | 2 | ||||
| -rw-r--r-- | src/http/serve.zig (renamed from src/http/Server.zig) | 43 | ||||
| -rw-r--r-- | src/main.zig | 36 | ||||
| -rw-r--r-- | src/web/root.zig | 27 |
6 files changed, 54 insertions, 61 deletions
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/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/Server.zig b/src/http/serve.zig index dc331d8..587f4a3 100644 --- a/src/http/Server.zig +++ b/src/http/serve.zig @@ -2,51 +2,38 @@ 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( +pub fn serve( gpa: std.mem.Allocator, - address: std.Io.net.IpAddress, comptime routes: RouteSet, -) !Self { + address: std.Io.net.IpAddress, +) !void { 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(); + const io = threaded.io(); + var server = try address.listen(io, .{ .reuse_address = true }); while (true) { - const stream = try self.server.accept(self.threaded.io()); - group.async(io, handle, .{self, gpa, stream}); + const stream = try server.accept(io); + group.async(io, handle, .{ io, gpa, stream, &routes }); } } pub fn handle( - self: *Self, + 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(self.threaded.io()); + defer stream.close(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 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| { @@ -56,9 +43,9 @@ pub fn handle( 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; + 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(), +}; + |