fediglam/src/main/controllers.zig

158 lines
5.9 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
2022-07-21 03:39:43 +00:00
fn respondJson(ctx: *http.server.Context, status: http.Status, value: anytype) !void {
var headers = http.Headers.init(ctx.alloc);
2022-07-13 07:57:21 +00:00
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();
}
2022-07-21 03:39:43 +00:00
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 {
2022-07-13 07:57:21 +00:00
const body = ctx.request.body orelse return error.BodyRequired;
var tokens = std.json.TokenStream.init(body);
2022-07-21 03:39:43 +00:00
const parsed = try std.json.parse(T, &tokens, .{ .allocator = ctx.alloc });
2022-07-13 07:57:21 +00:00
return parsed;
}
fn freeRequestBody(value: anytype, alloc: std.mem.Allocator) void {
std.json.parseFree(@TypeOf(value), value, .{ .allocator = alloc });
}
2022-07-18 06:11:42 +00:00
2022-07-19 07:07:01 +00:00
fn getApiContext(srv: *RequestServer, ctx: *http.server.Context) !api.ApiContext {
2022-07-18 06:11:42 +00:00
const header = ctx.request.headers.get("authorization") orelse "(null)";
const token = header[("bearer ").len..];
2022-07-21 03:39:43 +00:00
return try srv.api.makeApiContext(token, ctx.alloc);
// TODO: defer api.free(ctx.alloc, user_ctx);
2022-07-18 06:11:42 +00:00
}
2022-07-13 07:57:21 +00:00
};
const RequestServer = root.RequestServer;
const RouteArgs = http.RouteArgs;
pub fn createNote(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
2022-07-19 07:07:01 +00:00
const user_context = try utils.getApiContext(srv, ctx);
// TODO: defer free ApiContext
2022-07-21 03:39:43 +00:00
const info = try utils.parseRequestBody(api.NoteCreate, ctx);
defer utils.freeRequestBody(info, ctx.alloc);
2022-07-13 07:57:21 +00:00
2022-07-18 06:11:42 +00:00
const note = try srv.api.createNoteUser(info, user_context);
2022-07-13 07:57:21 +00:00
2022-07-21 03:39:43 +00:00
try utils.respondJson(ctx, .created, note);
2022-07-13 07:57:21 +00:00
}
2022-07-22 04:19:08 +00:00
pub fn register(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
const info = try utils.parseRequestBody(api.RegistrationInfo, ctx);
defer utils.freeRequestBody(info, ctx.alloc);
2022-07-13 07:57:21 +00:00
2022-07-22 04:19:08 +00:00
const user = srv.api.register(info) catch |err| switch (err) {
error.UsernameUnavailable => return try utils.respondError(ctx, .bad_request, "Username Unavailable"),
2022-07-16 19:30:47 +00:00
else => return err,
};
2022-07-13 07:57:21 +00:00
2022-07-21 03:39:43 +00:00
try utils.respondJson(ctx, .created, user);
2022-07-13 07:57:21 +00:00
}
pub fn getNote(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void {
const id_str = args.get("id") orelse return error.NotFound;
2022-07-21 03:39:43 +00:00
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);
2022-07-13 07:57:21 +00:00
2022-07-21 03:39:43 +00:00
try utils.respondJson(ctx, .ok, note);
2022-07-13 07:57:21 +00:00
}
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;
2022-07-21 03:39:43 +00:00
const id = Uuid.parse(id_str) catch return utils.respondError(ctx, .bad_request, "Invalid UUID");
2022-07-22 04:19:08 +00:00
const user = (try srv.api.getActor(id, ctx.alloc)) orelse return utils.respondError(ctx, .not_found, "Note not found");
2022-07-21 03:39:43 +00:00
defer api.free(ctx.alloc, user);
2022-07-16 18:41:09 +00:00
2022-07-21 03:39:43 +00:00
try utils.respondJson(ctx, .ok, user);
2022-07-16 18:41:09 +00:00
}
2022-07-19 07:07:01 +00:00
pub fn react(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void {
const user_context = try utils.getApiContext(srv, ctx);
// TODO: defer free ApiContext
const note_id = args.get("id") orelse return error.NotFound;
2022-07-21 03:39:43 +00:00
const id = Uuid.parse(note_id) catch return utils.respondError(ctx, .bad_request, "Invalid UUID");
2022-07-19 07:07:01 +00:00
try srv.api.react(id, user_context);
2022-07-21 03:39:43 +00:00
try utils.respondJson(ctx, .created, .{});
2022-07-19 07:07:01 +00:00
}
pub fn listReacts(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void {
const user_context = try utils.getApiContext(srv, ctx);
// TODO: defer free ApiContext
const note_id = args.get("id") orelse return error.NotFound;
2022-07-21 03:39:43 +00:00
const id = Uuid.parse(note_id) catch return utils.respondError(ctx, .bad_request, "Invalid UUID");
2022-07-19 07:07:01 +00:00
const reacts = try srv.api.listReacts(id, user_context);
2022-07-21 03:39:43 +00:00
try utils.respondJson(ctx, .ok, .{ .items = reacts });
2022-07-19 07:07:01 +00:00
}
2022-07-17 23:21:03 +00:00
pub fn authenticate(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
2022-07-19 07:07:01 +00:00
const user_ctx = try utils.getApiContext(srv, ctx);
2022-07-21 03:39:43 +00:00
// TODO: defer api.free(ctx.alloc, user_ctx);
2022-07-17 23:21:03 +00:00
2022-07-21 03:39:43 +00:00
try utils.respondJson(ctx, .ok, user_ctx.user_context);
2022-07-17 23:21:03 +00:00
}
2022-07-21 05:26:13 +00:00
pub fn login(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
const credentials = try utils.parseRequestBody(struct { username: []const u8, password: []const u8 }, ctx);
defer utils.freeRequestBody(credentials, ctx.alloc);
2022-07-22 04:19:08 +00:00
const actor = srv.api.login(credentials.username, credentials.password, ctx.alloc) catch |err| switch (err) {
2022-07-21 05:26:13 +00:00
error.PasswordVerificationFailed => return utils.respondError(ctx, .bad_request, "Invalid Login"),
else => return err,
};
2022-07-22 04:19:08 +00:00
defer api.free(ctx.alloc, actor);
2022-07-21 05:26:13 +00:00
2022-07-22 04:19:08 +00:00
try utils.respondJson(ctx, .ok, actor);
2022-07-21 05:26:13 +00:00
}
2022-07-21 03:39:43 +00:00
pub fn healthcheck(_: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
try utils.respondJson(ctx, .ok, .{ .status = "ok" });
2022-07-13 07:57:21 +00:00
}
2022-07-21 03:39:43 +00:00
pub fn notFound(_: *RequestServer, ctx: *http.server.Context) void {
utils.respondError(ctx, .not_found, "Not Found") catch unreachable;
2022-07-13 07:57:21 +00:00
}