From f33b4a856f62866f3b0ab56c7be54bbab2f01e17 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Thu, 17 Nov 2022 20:11:04 -0800 Subject: [PATCH] Template helpers --- src/main/controllers.zig | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/controllers.zig b/src/main/controllers.zig index 3fcfd6a..821ebd0 100644 --- a/src/main/controllers.zig +++ b/src/main/controllers.zig @@ -16,9 +16,7 @@ pub const notes = @import("./controllers/api/notes.zig"); pub const streaming = @import("./controllers/api/streaming.zig"); pub const timelines = @import("./controllers/api/timelines.zig"); -const web = struct { - const index = @import("./controllers/web/index.zig"); -}; +const web = @import("./controllers/web.zig"); pub fn routeRequest(api_source: anytype, req: *http.Request, res: *http.Response, alloc: std.mem.Allocator) void { // TODO: hashmaps? @@ -54,9 +52,7 @@ const routes = .{ follows.create, follows.query_followers, follows.query_following, - - web.index, -}; +} ++ web.routes; fn parseRouteArgs(comptime route: []const u8, comptime Args: type, path: []const u8) !Args { var args: Args = undefined; @@ -284,6 +280,18 @@ pub const Response = struct { self.opened = true; return self.res; } + + pub fn template(self: *Self, status_code: http.Status, comptime templ: []const u8, data: anytype) !void { + try self.headers.put("Content-Type", "text/html"); + + var stream = try self.open(status_code); + defer stream.close(); + + const writer = stream.writer(); + try @import("template").execute(writer, templ, data); + + try stream.finish(); + } }; const json_options = if (builtin.mode == .Debug)