fediglam/src/main/controllers.zig

88 lines
3.1 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 Uuid = @import("util").Uuid;
2022-07-22 06:53:05 +00:00
pub const auth = @import("./controllers/auth.zig");
pub const notes = @import("./controllers/notes.zig");
pub const actors = @import("./controllers/actors.zig");
pub const utils = struct {
2022-07-13 07:57:21 +00:00
const json_options = if (builtin.mode == .Debug) .{
.whitespace = .{
.indent = .{ .Space = 2 },
.separator = true,
},
2022-07-25 00:04:44 +00:00
.string = .{ .String = .{} },
2022-07-13 07:57:21 +00:00
} else .{
.whitespace = .{
.indent = .None,
.separator = false,
},
2022-07-25 00:04:44 +00:00
.string = .{ .String = .{} },
2022-07-13 07:57:21 +00:00
};
// Responds to a request with a json value
2022-07-22 06:53:05 +00:00
pub fn respondJson(ctx: *http.server.Context, status: http.Status, value: anytype) !void {
2022-07-21 03:39:43 +00:00
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-22 06:53:05 +00:00
pub fn respondError(ctx: *http.server.Context, status: http.Status, err: []const u8) !void {
2022-07-21 03:39:43 +00:00
return respondJson(ctx, status, .{ .@"error" = err });
}
2022-07-22 06:53:05 +00:00
pub 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;
}
2022-07-22 06:53:05 +00:00
pub fn freeRequestBody(value: anytype, alloc: std.mem.Allocator) void {
2022-07-13 07:57:21 +00:00
std.json.parseFree(@TypeOf(value), value, .{ .allocator = alloc });
}
2022-07-18 06:11:42 +00:00
2022-07-26 02:07:05 +00:00
pub fn getApiConn(srv: *RequestServer, ctx: *http.server.Context) !api.ApiSource.Conn {
return authorizeApiConn(srv, ctx) catch |err| switch (err) {
error.NoToken => srv.api.connectUnauthorized(ctx.alloc),
error.InvalidToken => return error.InvalidToken,
else => @panic("TODO"), // doing this to resolve some sort of compiler analysis dependency issue
};
}
fn authorizeApiConn(srv: *RequestServer, ctx: *http.server.Context) !api.ApiSource.Conn {
const header = ctx.request.headers.get("authorization") orelse return error.NoToken;
2022-07-18 06:11:42 +00:00
2022-07-26 02:07:05 +00:00
if (header.len < ("bearer ").len) return error.InvalidToken;
2022-07-18 06:11:42 +00:00
const token = header[("bearer ").len..];
2022-07-26 02:07:05 +00:00
return try srv.api.connectToken(token, ctx.alloc);
2022-07-18 06:11:42 +00:00
}
2022-07-13 07:57:21 +00:00
};
const RequestServer = root.RequestServer;
const RouteArgs = http.RouteArgs;
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
}