update to zig master and give up
This commit is contained in:
parent
cbadc880b1
commit
29f12b6779
5 changed files with 103 additions and 19 deletions
|
@ -62,7 +62,7 @@ pub fn Application(comptime Context: type) type {
|
|||
var event = Event{
|
||||
.ctx = &ctx,
|
||||
.req = .{
|
||||
.uri = try std.Uri.parseWithoutScheme(req.head.target),
|
||||
.uri = try std.Uri.parseAfterScheme("http://", req.head.target),
|
||||
.headers = std.ArrayList(*const http.Header).init(allocator),
|
||||
},
|
||||
.res = .{
|
||||
|
@ -126,7 +126,7 @@ pub fn Router(comptime Context: type) type {
|
|||
root_node: *Node,
|
||||
// static_routes: std.StringHashMap(*Node),
|
||||
|
||||
pub fn init(allocator: mem.Allocator, root_ctx: Context) Router {
|
||||
pub fn init(allocator: mem.Allocator, root_ctx: Context) Self {
|
||||
var arena = heap.ArenaAllocator.init(allocator);
|
||||
var node = Node.init(arena.allocator()) catch @panic("OOM");
|
||||
node.data = root_ctx;
|
||||
|
@ -139,19 +139,19 @@ pub fn Router(comptime Context: type) type {
|
|||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Router) void {
|
||||
pub fn deinit(self: *Self) void {
|
||||
self.root_node.deinit(self.arena.allocator(), null);
|
||||
self.arena.deinit();
|
||||
// self.static_routes.deinit();
|
||||
}
|
||||
|
||||
// pub fn handle(self: *Router, event: *Listener.Event) !void {
|
||||
// const route = try self.getRoute(event.req.uri.path);
|
||||
// try route.handler(event);
|
||||
// }
|
||||
pub fn handle(self: *Router, ctx: *Context) !void {
|
||||
const route = try self.getRoute(ctx.req.uri.path);
|
||||
try route.handler(ctx);
|
||||
}
|
||||
|
||||
/// Insert a route if the path is not already present, otherwise overwrite preexisting HandlerFn.
|
||||
pub fn putRoute(self: *Router, path: []const u8, ctx: Context) !void {
|
||||
pub fn putRoute(self: *Self, path: []const u8, ctx: Context) !void {
|
||||
std.debug.print("\npath {s}\n", .{path});
|
||||
// var is_static_route = true;
|
||||
var sections = mem.splitScalar(u8, path, '/');
|
||||
|
@ -216,7 +216,7 @@ pub fn Router(comptime Context: type) type {
|
|||
}
|
||||
|
||||
/// Get the HandlerFn associated with path, otherwise get root_handler.
|
||||
pub fn getRoute(self: *Router, path: []const u8) !*Node {
|
||||
pub fn getRoute(self: *Self, path: []const u8) !*Node {
|
||||
// if (self.static_routes.get(path)) |rt| return rt;
|
||||
std.debug.print("\nget path {s}\n", .{path});
|
||||
|
||||
|
@ -277,7 +277,7 @@ pub fn Router(comptime Context: type) type {
|
|||
}
|
||||
|
||||
/// If there is a route with a matching path, it is deleted from the router, and this function return true. Otherwise it returns false.
|
||||
pub fn removeRoute(self: *Router, path: []const u8) bool {
|
||||
pub fn removeRoute(self: *Self, path: []const u8) bool {
|
||||
// _ = self.static_routes.remove(path);
|
||||
std.debug.print("\nremoving path {s}\n", .{path});
|
||||
|
||||
|
@ -351,6 +351,8 @@ pub fn Router(comptime Context: type) type {
|
|||
allocator.destroy(self);
|
||||
}
|
||||
};
|
||||
|
||||
const Self = @This();
|
||||
};
|
||||
}
|
||||
|
||||
|
|
16
src/main.zig
16
src/main.zig
|
@ -11,23 +11,27 @@ pub fn main() !void {
|
|||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
var router = Router.init(allocator, handleError);
|
||||
defer router.deinit();
|
||||
|
||||
try router.putRoute("/", &handle);
|
||||
// try listener.router.putRoute("/error", &handleError);
|
||||
// try listener.router.putRoute("//pee", &handleError);
|
||||
// try listener.router.putRoute("/error/*", &handleError);
|
||||
|
||||
const address = try net.Address.parseIp("0.0.0.0", 8080);
|
||||
var listener = App.init(.{
|
||||
.address = address,
|
||||
.allocator = allocator,
|
||||
.root_handler = &handleError,
|
||||
// .root_handler = ,
|
||||
});
|
||||
defer listener.deinit();
|
||||
|
||||
// try listener.router.putRoute("/", &handle);
|
||||
// try listener.router.putRoute("/error", &handleError);
|
||||
// try listener.router.putRoute("//pee", &handleError);
|
||||
// try listener.router.putRoute("/error/*", &handleError);
|
||||
|
||||
try listener.listen();
|
||||
}
|
||||
|
||||
const App = Application(DummyContext);
|
||||
const Router = @import("application.zig").Router(App.HandlerFn);
|
||||
const DummyContext = struct {};
|
||||
|
||||
fn handle(event: *App.Event) anyerror!void {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue