fediglam/src/main/controllers.zig

100 lines
3.6 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");
2022-07-27 05:02:09 +00:00
pub const admin = struct {
pub const invites = @import("./controllers/admin/invites.zig");
2022-08-02 04:33:23 +00:00
pub const communities = @import("./controllers/admin/communities.zig");
2022-07-27 05:02:09 +00:00
};
2022-07-22 06:53:05 +00:00
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-08-02 06:24:16 +00:00
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});
};
2022-07-21 03:39:43 +00:00
}
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 {
2022-09-05 08:52:49 +00:00
const host = ctx.request.headers.get("Host") orelse return error.NoHost;
return authorizeApiConn(srv, ctx, host) catch |err| switch (err) {
error.NoToken => srv.api.connectUnauthorized(host, ctx.alloc),
2022-07-26 02:07:05 +00:00
error.InvalidToken => return error.InvalidToken,
else => @panic("TODO"), // doing this to resolve some sort of compiler analysis dependency issue
};
}
2022-09-05 08:52:49 +00:00
fn authorizeApiConn(srv: *RequestServer, ctx: *http.server.Context, host: []const u8) !api.ApiSource.Conn {
2022-07-26 02:07:05 +00:00
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-09-05 08:52:49 +00:00
return try srv.api.connectToken(host, 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 {
2022-08-02 06:24:16 +00:00
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");
2022-07-13 07:57:21 +00:00
}