diff --git a/src/main/controllers.zig b/src/main/controllers.zig index f77063e..40fdd76 100644 --- a/src/main/controllers.zig +++ b/src/main/controllers.zig @@ -20,8 +20,8 @@ const utils = struct { }; // Responds to a request with a json value - fn respondJson(ctx: *http.server.Context, status: http.Status, value: anytype, alloc: std.mem.Allocator) !void { - var headers = http.Headers.init(alloc); + fn respondJson(ctx: *http.server.Context, status: http.Status, value: anytype) !void { + var headers = http.Headers.init(ctx.alloc); defer headers.deinit(); // Don't need to free this k/v pair because they aren't dynamically allocated @@ -36,10 +36,14 @@ const utils = struct { try stream.finish(); } - fn parseRequestBody(comptime T: type, ctx: *http.server.Context, alloc: std.mem.Allocator) !T { + fn respondError(ctx: *http.server.Context, status: http.Status, err: []const u8) !void { + return respondJson(ctx, status, .{ .@"error" = err }); + } + + fn parseRequestBody(comptime T: type, ctx: *http.server.Context) !T { const body = ctx.request.body orelse return error.BodyRequired; var tokens = std.json.TokenStream.init(body); - const parsed = try std.json.parse(T, &tokens, .{ .allocator = alloc }); + const parsed = try std.json.parse(T, &tokens, .{ .allocator = ctx.alloc }); return parsed; } @@ -53,8 +57,8 @@ const utils = struct { const token = header[("bearer ").len..]; - return try srv.api.makeApiContext(token, srv.alloc); - // TODO: defer api.free(srv.alloc, user_ctx); + return try srv.api.makeApiContext(token, ctx.alloc); + // TODO: defer api.free(ctx.alloc, user_ctx); } }; @@ -64,42 +68,42 @@ const RouteArgs = http.RouteArgs; pub fn createNote(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { const user_context = try utils.getApiContext(srv, ctx); // TODO: defer free ApiContext - const info = try utils.parseRequestBody(api.NoteCreate, ctx, srv.alloc); - defer utils.freeRequestBody(info, srv.alloc); + const info = try utils.parseRequestBody(api.NoteCreate, ctx); + defer utils.freeRequestBody(info, ctx.alloc); const note = try srv.api.createNoteUser(info, user_context); - try utils.respondJson(ctx, .created, note, srv.alloc); + try utils.respondJson(ctx, .created, note); } pub fn createUser(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { - const info = try utils.parseRequestBody(api.CreateInfo(models.User), ctx, srv.alloc); - defer utils.freeRequestBody(info, srv.alloc); + const info = try utils.parseRequestBody(api.CreateInfo(models.User), ctx); + defer utils.freeRequestBody(info, ctx.alloc); const user = srv.api.createUser(info) catch |err| switch (err) { - error.HandleNotAvailable => return try utils.respondJson(ctx, .bad_request, .{ .@"error" = "handle not available" }, srv.alloc), + error.HandleNotAvailable => return try utils.respondError(ctx, .bad_request, "handle not available"), else => return err, }; - try utils.respondJson(ctx, .created, user, srv.alloc); + try utils.respondJson(ctx, .created, user); } pub fn getNote(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void { const id_str = args.get("id") orelse return error.NotFound; - const id = Uuid.parse(id_str) catch return utils.respondJson(ctx, .bad_request, .{ .@"error" = "Invalid UUID" }, srv.alloc); - const note = (try srv.api.getNote(id, srv.alloc)) orelse return utils.respondJson(ctx, .not_found, .{ .@"error" = "Note not found" }, srv.alloc); - defer api.free(srv.alloc, note); + const id = Uuid.parse(id_str) catch return utils.respondError(ctx, .bad_request, "Invalid UUID"); + const note = (try srv.api.getNote(id, ctx.alloc)) orelse return utils.respondError(ctx, .not_found, "Note not found"); + defer api.free(ctx.alloc, note); - try utils.respondJson(ctx, .ok, note, srv.alloc); + try utils.respondJson(ctx, .ok, note); } pub fn getUser(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void { const id_str = args.get("id") orelse return error.NotFound; - const id = Uuid.parse(id_str) catch return utils.respondJson(ctx, .bad_request, .{ .@"error" = "Invalid UUID" }, srv.alloc); - const user = (try srv.api.getUser(id, srv.alloc)) orelse return utils.respondJson(ctx, .not_found, .{ .@"error" = "Note not found" }, srv.alloc); - defer api.free(srv.alloc, user); + const id = Uuid.parse(id_str) catch return utils.respondError(ctx, .bad_request, "Invalid UUID"); + const user = (try srv.api.getUser(id, ctx.alloc)) orelse return utils.respondError(ctx, .not_found, "Note not found"); + defer api.free(ctx.alloc, user); - try utils.respondJson(ctx, .ok, user, srv.alloc); + try utils.respondJson(ctx, .ok, user); } pub fn react(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void { @@ -107,10 +111,10 @@ pub fn react(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !v // TODO: defer free ApiContext const note_id = args.get("id") orelse return error.NotFound; - const id = Uuid.parse(note_id) catch return utils.respondJson(ctx, .bad_request, .{ .@"error" = "Invalid UUID" }, srv.alloc); + const id = Uuid.parse(note_id) catch return utils.respondError(ctx, .bad_request, "Invalid UUID"); try srv.api.react(id, user_context); - try utils.respondJson(ctx, .created, .{}, srv.alloc); + try utils.respondJson(ctx, .created, .{}); } pub fn listReacts(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void { @@ -118,23 +122,23 @@ pub fn listReacts(srv: *RequestServer, ctx: *http.server.Context, args: RouteArg // TODO: defer free ApiContext const note_id = args.get("id") orelse return error.NotFound; - const id = Uuid.parse(note_id) catch return utils.respondJson(ctx, .bad_request, .{ .@"error" = "Invalid UUID" }, srv.alloc); + const id = Uuid.parse(note_id) catch return utils.respondError(ctx, .bad_request, "Invalid UUID"); const reacts = try srv.api.listReacts(id, user_context); - try utils.respondJson(ctx, .ok, .{ .items = reacts }, srv.alloc); + try utils.respondJson(ctx, .ok, .{ .items = reacts }); } pub fn authenticate(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { const user_ctx = try utils.getApiContext(srv, ctx); - // TODO: defer api.free(srv.alloc, user_ctx); + // TODO: defer api.free(ctx.alloc, user_ctx); - try utils.respondJson(ctx, .ok, user_ctx.user_context, srv.alloc); + try utils.respondJson(ctx, .ok, user_ctx.user_context); } -pub fn healthcheck(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { - try utils.respondJson(ctx, .ok, .{ .status = "ok" }, srv.alloc); +pub fn healthcheck(_: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { + try utils.respondJson(ctx, .ok, .{ .status = "ok" }); } -pub fn notFound(srv: *RequestServer, ctx: *http.server.Context) void { - utils.respondJson(ctx, .not_found, .{ .@"error" = "Not Found" }, srv.alloc) catch unreachable; +pub fn notFound(_: *RequestServer, ctx: *http.server.Context) void { + utils.respondError(ctx, .not_found, "Not Found") catch unreachable; }