fediglam/src/main/controllers.zig
2022-08-01 23:35:56 -07:00

97 lines
3.5 KiB
Zig

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;
pub const auth = @import("./controllers/auth.zig");
pub const notes = @import("./controllers/notes.zig");
pub const actors = @import("./controllers/actors.zig");
pub const admin = struct {
pub const invites = @import("./controllers/admin/invites.zig");
pub const communities = @import("./controllers/admin/communities.zig");
};
pub const utils = struct {
const json_options = if (builtin.mode == .Debug) .{
.whitespace = .{
.indent = .{ .Space = 2 },
.separator = true,
},
.string = .{ .String = .{} },
} else .{
.whitespace = .{
.indent = .None,
.separator = false,
},
.string = .{ .String = .{} },
};
// Responds to a request with a json value
pub 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
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();
}
pub fn respondError(ctx: *http.server.Context, status: http.Status, err: []const u8) void {
respondJson(ctx, status, .{ .@"error" = err }) catch |write_err| {
std.log.err("Unable to print error: {}", .{write_err});
};
}
pub 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 = ctx.alloc });
return parsed;
}
pub fn freeRequestBody(value: anytype, alloc: std.mem.Allocator) void {
std.json.parseFree(@TypeOf(value), value, .{ .allocator = alloc });
}
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.request.headers.get("Host"), 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;
if (header.len < ("bearer ").len) return error.InvalidToken;
const token = header[("bearer ").len..];
return try srv.api.connectToken(ctx.request.headers.get("Host"), token, ctx.alloc);
}
};
const RequestServer = root.RequestServer;
const RouteArgs = http.RouteArgs;
pub fn healthcheck(_: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
try utils.respondJson(ctx, .ok, .{ .status = "ok" });
}
pub fn notFound(_: *RequestServer, ctx: *http.server.Context) void {
utils.respondError(ctx, .not_found, "Not Found");
}
pub fn internalServerError(_: *RequestServer, ctx: *http.server.Context) void {
utils.respondError(ctx, .internal_server_error, "Internal Server Error");
}