fediglam/src/main/controllers.zig

177 lines
6.4 KiB
Zig

const std = @import("std");
const root = @import("root");
const builtin = @import("builtin");
const http = @import("http");
const api = @import("api");
const util = @import("util");
pub const auth = @import("./controllers/auth.zig");
pub const communities = @import("./controllers/communities.zig");
pub const invites = @import("./controllers/invites.zig");
pub const users = @import("./controllers/users.zig");
pub const notes = @import("./controllers/notes.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 parseQueryParams(comptime T: type, ctx: *http.server.Context) !T {
// TODO: clean up parsing
const path = ctx.request.path;
const start = (std.mem.indexOfScalar(u8, path, '?') orelse return error.NoQuery) + 1;
const rest = path[start..];
const query = std.mem.sliceTo(rest, '#');
const fake_url = util.Url{
.scheme = "",
.hostport = "",
.path = "",
.query = query,
.fragment = "",
};
var result: T = .{};
inline for (std.meta.fields(T)) |f| {
if (fake_url.getQuery(f.name)) |param| {
const F = if (comptime @typeInfo(f.field_type) == .Optional) std.meta.Child(f.field_type) else f.field_type;
std.log.debug("{}: {s}", .{ F, param });
@field(result, f.name) = switch (F) {
[]const u8 => param,
else => switch (@typeInfo(F)) {
.Struct => if (@hasDecl(F, "parse")) try F.parse(param) else @compileError("Invalid type " ++ @typeName(F)),
.Enum => std.meta.stringToEnum(F, param) orelse return error.ParseError,
.Int => try std.fmt.parseInt(F, param, 10),
else => {},
},
};
}
}
if (false) {
inline for (std.meta.fields(T)) |f| {
if (fake_url.getQuery(f.name)) |param| {
const F = if (comptime @typeInfo(f.field_type) == .Optional) std.meta.Child(f.field_type) else f.field_type;
switch (F) {
[]const u8 => @field(result, f.name) = param,
else => switch (@typeInfo(F)) {
.Struct,
.Opaque,
//.Union,
=> if (@hasDecl(F, "parse")) {
@compileLog(F);
if (true) @compileError(F);
@field(result, f.name) = try F.parse(param);
},
//.Int => @field(result, f.name) = try std.fmt.parseInt(F, param),
},
}
}
}
}
return result;
}
fn parseTypeFromQueryParams(comptime T: type, comptime name_prefix: []const u8, url: util.Url) !T {
var result: T = .{};
inline for (std.meta.fields(T)) |field| {
const FieldType = switch (@typeInfo(field.field_type)) {
.Optional => |info| info.child,
else => field.field_type,
};
_ = FieldType;
_ = result;
const qualified_name = name_prefix ++ field.name;
if (url.getQuery(qualified_name)) |param| {
_ = param;
}
}
}
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 {
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),
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, host: []const u8) !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(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");
}