fediglam/src/main/controllers.zig

111 lines
4.2 KiB
Zig
Raw Normal View History

2022-07-13 07:57:21 +00:00
const std = @import("std");
const root = @import("root");
const builtin = @import("builtin");
const http = @import("http");
const api = @import("./api.zig");
const models = @import("./models.zig");
const Uuid = @import("util").Uuid;
const utils = struct {
const json_options = if (builtin.mode == .Debug) .{
.whitespace = .{
.indent = .{ .Space = 2 },
.separator = true,
},
} else .{
.whitespace = .{
.indent = .None,
.separator = false,
},
};
// 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);
defer headers.deinit();
// Don't need to free this k/v pair because they aren't dynamically allocated
try headers.put("Content-Type", "application/json");
var stream = try ctx.openResponse(&headers, status);
defer stream.close();
const writer = stream.writer();
try std.json.stringify(value, json_options, writer);
try stream.finish();
}
fn parseRequestBody(comptime T: type, ctx: *http.server.Context, alloc: std.mem.Allocator) !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 });
return parsed;
}
fn freeRequestBody(value: anytype, alloc: std.mem.Allocator) void {
std.json.parseFree(@TypeOf(value), value, .{ .allocator = alloc });
}
};
const RequestServer = root.RequestServer;
const RouteArgs = http.RouteArgs;
pub fn createNote(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
const info = try utils.parseRequestBody(api.CreateInfo(models.Note), ctx, srv.alloc);
defer utils.freeRequestBody(info, srv.alloc);
const note = try srv.api.createNote(info);
try utils.respondJson(ctx, .created, note, srv.alloc);
}
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);
2022-07-16 19:30:47 +00:00
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),
else => return err,
};
2022-07-13 07:57:21 +00:00
try utils.respondJson(ctx, .created, user, srv.alloc);
}
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);
try utils.respondJson(ctx, .ok, note, srv.alloc);
}
2022-07-16 18:41:09 +00:00
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);
try utils.respondJson(ctx, .ok, user, srv.alloc);
}
2022-07-17 23:21:03 +00:00
pub fn authenticate(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
const header = ctx.request.headers.get("authorization") orelse "(null)";
const token = header[("bearer ").len..];
const user_ctx = try srv.api.makeUserContext(token, srv.alloc);
try utils.respondJson(ctx, .ok, user_ctx, srv.alloc);
}
2022-07-13 07:57:21 +00:00
pub fn healthcheck(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
try utils.respondJson(ctx, .ok, .{ .status = "ok" }, srv.alloc);
}
pub fn notFound(srv: *RequestServer, ctx: *http.server.Context) void {
utils.respondJson(ctx, .not_found, .{ .@"error" = "Not Found" }, srv.alloc) catch unreachable;
}