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,
|
|
|
|
},
|
|
|
|
} else .{
|
|
|
|
.whitespace = .{
|
|
|
|
.indent = .None,
|
|
|
|
.separator = false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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-22 06:53:05 +00:00
|
|
|
pub 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;
|
|
|
|
|
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
|
|
|
}
|