aboutsummaryrefslogtreecommitdiff
path: root/src/http/Response.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/http/Response.zig')
-rw-r--r--src/http/Response.zig17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/http/Response.zig b/src/http/Response.zig
index 8d10992..d54792b 100644
--- a/src/http/Response.zig
+++ b/src/http/Response.zig
@@ -5,6 +5,15 @@ pub const Message = union(enum) {
static: []const u8,
streaming: std.Io.File,
+ pub fn json(arena: std.mem.Allocator, value: anytype) !Message {
+ var allocating: std.Io.Writer.Allocating = .init(arena);
+
+ var jsonify: std.json.Stringify = .{ .writer = &allocating.writer };
+ try jsonify.write(value);
+
+ return .string(try allocating.toOwnedSlice());
+ }
+
pub fn string(content: []const u8) Message {
return .{ .static = content };
}
@@ -27,6 +36,14 @@ pub fn ok(message: Message) @This() {
return with(.ok, message);
}
+pub fn ok_or_error(message: anyerror!Message) @This() {
+ return ok(message catch return with(.internal_server_error, .void));
+}
+
+pub fn bad_request(message: Message) @This() {
+ return with(.bad_request, message);
+}
+
pub fn send(
self: *const @This(),
io: std.Io,