srctree

Gregory Mullen parent eb737d8c 5cb65439
extend router api

src/endpoints/commit-flex.zig added: 28, removed: 21, total 7
@@ -100,11 +100,6 @@ pub fn commitFlex(r: *Response, _: []const u8) Error!void {
}
defer for (stack) |s| r.alloc.free(s.children.?);
 
var now = DateTime.now();
std.debug.print("{any}\n", .{now});
 
std.debug.print("{}\n", .{DateTime.DAYS_IN_MONTH[now.months] - (now.days - now.weekday)});
 
var days = &[_]HTML.Element{
HTML.divAttr(&[_]HTML.Element{
HTML.divAttr(" ", &day),
 
src/routes.zig added: 28, removed: 21, total 7
@@ -13,19 +13,24 @@ const Error = endpoint.Error;
const div = HTML.div;
const span = HTML.span;
 
pub const Router = *const fn (*Response, []const u8) Error!void;
 
const endpoints = [_]struct {
name: []const u8,
call: Endpoint,
match: union(enum) {
call: Endpoint,
route: Router,
},
}{
.{ .name = "/", .call = default },
.{ .name = "/auth", .call = auth },
.{ .name = "/bye", .call = bye },
.{ .name = "/code", .call = code },
.{ .name = "/commits", .call = respond },
.{ .name = "/hi", .call = respond },
.{ .name = "/list", .call = list },
.{ .name = "/tree", .call = respond },
.{ .name = "/user", .call = endpoint.commitFlex },
.{ .name = "/", .match = .{ .call = default } },
.{ .name = "/auth", .match = .{ .call = auth } },
.{ .name = "/bye", .match = .{ .call = bye } },
.{ .name = "/code", .match = .{ .call = code } },
.{ .name = "/commits", .match = .{ .call = respond } },
.{ .name = "/hi", .match = .{ .call = respond } },
.{ .name = "/list", .match = .{ .call = list } },
.{ .name = "/tree", .match = .{ .call = respond } },
.{ .name = "/user", .match = .{ .call = endpoint.commitFlex } },
};
 
fn sendMsg(r: *Response, msg: []const u8) !void {
@@ -163,9 +168,16 @@ fn eql(a: []const u8, b: []const u8) bool {
return std.mem.eql(u8, a, b);
}
 
pub fn route(uri: []const u8) Endpoint {
pub fn router(uri: []const u8) Endpoint {
inline for (endpoints) |ep| {
if (eql(uri, ep.name)) return ep.call;
switch (ep.match) {
.call => |call| {
if (eql(uri, ep.name)) return call;
},
.route => |route| {
if (eql(uri[0..ep.name.len], ep.name)) return route;
},
}
}
return notfound;
}
 
src/zwsgi.zig added: 28, removed: 21, total 7
@@ -103,7 +103,7 @@ pub fn serve(a: Allocator, streamsrv: *StreamServer) !void {
const request = try readHeader(a, acpt);
var response = Response.init(a, acpt.stream, &request);
 
var endpoint = Router.route(response.request.uri);
var endpoint = Router.router(response.request.uri);
try endpoint(&response, "");
if (response.phase != .closed) try response.finish();
acpt.stream.close();