fediglam/src/main/main.zig

112 lines
3.7 KiB
Zig
Raw Normal View History

2022-04-02 20:23:18 +00:00
const std = @import("std");
2022-07-10 22:19:21 +00:00
const builtin = @import("builtin");
2022-07-09 22:07:51 +00:00
const http = @import("http");
2022-07-13 04:16:33 +00:00
const util = @import("util");
2022-07-13 03:40:48 +00:00
const api = @import("./api.zig");
2022-07-11 02:39:28 +00:00
const models = @import("./models.zig");
2022-07-13 04:16:33 +00:00
const Uuid = util.Uuid;
2022-07-10 05:05:01 +00:00
// this thing is overcomplicated and weird. stop this
const Router = http.Router(*RequestServer);
const Route = Router.Route;
const RouteArgs = http.RouteArgs;
const router = Router{
.routes = &[_]Route{
2022-07-10 22:19:21 +00:00
Route.new(.GET, "/healthcheck", healthcheck),
2022-07-11 00:40:17 +00:00
Route.new(.GET, "/:id", getNote),
2022-07-11 02:39:28 +00:00
Route.new(.POST, "/", createNote),
2022-07-10 05:05:01 +00:00
},
};
2022-07-10 22:19:21 +00:00
const json_options = if (builtin.mode == .Debug)
.{
.whitespace = .{
.indent = .{ .Space = 2 },
.separator = true,
},
} else .{
.whitespace = .{
.indent = .None,
.separator = false,
},
};
2022-07-10 05:05:01 +00:00
2022-07-11 00:01:36 +00:00
// Responds to a request with a json value
2022-07-11 00:48:54 +00:00
fn respondJson(ctx: *http.server.Context, status: http.Status, value: anytype, alloc: std.mem.Allocator) !void {
2022-07-11 00:01:36 +00:00
var headers = http.Headers.init(alloc);
defer headers.deinit();
2022-07-10 23:53:17 +00:00
2022-07-11 00:01:36 +00:00
// Don't need to free this k/v pair because they aren't dynamically allocated
try headers.put("Content-Type", "application/json");
2022-07-10 23:53:17 +00:00
2022-07-11 00:48:54 +00:00
var stream = try ctx.openResponse(&headers, status);
2022-07-10 23:53:17 +00:00
defer stream.close();
const writer = stream.writer();
2022-07-11 00:01:36 +00:00
try std.json.stringify(value, json_options, writer);
2022-07-10 23:53:17 +00:00
try stream.finish();
}
2022-07-11 02:39:28 +00:00
fn createNote(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
const body = ctx.request.body orelse return respondJson(ctx, .bad_request, .{ .@"error" = "no note body provided" }, srv.alloc);
var tokens = std.json.TokenStream.init(body);
2022-07-13 04:28:54 +00:00
const info = try std.json.parse(api.CreateInfo(models.Note), &tokens, .{ .allocator = srv.alloc });
defer std.json.parseFree(api.CreateInfo(models.Note), info, .{ .allocator = srv.alloc });
2022-07-11 02:39:28 +00:00
2022-07-13 04:28:54 +00:00
const note = try srv.api.createNote(info);
2022-07-11 02:39:28 +00:00
try respondJson(ctx, .created, note, srv.alloc);
}
2022-07-11 00:40:17 +00:00
fn getNote(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void {
2022-07-13 04:16:33 +00:00
const id_str = args.get("id") orelse return error.NotFound;
const id = Uuid.parse(id_str) catch return respondJson(ctx, .bad_request, .{ .@"error" = "invalid id" }, srv.alloc);
2022-07-13 03:40:48 +00:00
const note = (try srv.api.getNote(id, srv.alloc)) orelse return error.NotFound;
defer api.free(srv.alloc, note);
2022-07-10 05:05:01 +00:00
2022-07-11 00:48:54 +00:00
try respondJson(ctx, .ok, note, srv.alloc);
2022-07-11 00:01:36 +00:00
}
2022-07-10 05:05:01 +00:00
2022-07-11 00:01:36 +00:00
fn healthcheck(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
2022-07-11 00:48:54 +00:00
try respondJson(ctx, .ok, .{ .status = "ok" }, srv.alloc);
2022-07-10 05:05:01 +00:00
}
2022-04-23 07:11:40 +00:00
const RequestServer = struct {
2022-07-10 05:05:01 +00:00
alloc: std.mem.Allocator,
2022-07-13 03:40:48 +00:00
api: api.ApiServer,
2022-07-10 05:05:01 +00:00
2022-07-11 00:12:52 +00:00
fn init(alloc: std.mem.Allocator) !RequestServer {
2022-07-10 05:05:01 +00:00
return RequestServer{
.alloc = alloc,
2022-07-13 03:40:48 +00:00
.api = try api.ApiServer.init(alloc),
2022-07-10 05:05:01 +00:00
};
}
fn listenAndRun(self: *RequestServer, addr: std.net.Address) noreturn {
var srv = http.Server.listen(addr) catch unreachable;
defer srv.shutdown();
2022-04-04 03:36:32 +00:00
while (true) {
var buf: [1 << 20]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
const alloc = fba.allocator();
2022-04-02 20:23:18 +00:00
var ctx = srv.accept(alloc) catch unreachable;
defer ctx.close();
2022-04-21 09:34:04 +00:00
2022-07-11 00:48:54 +00:00
router.dispatch(self, &ctx, ctx.request.method, ctx.request.path) catch |err| switch (err) {
error.NotFound, error.RouteNotApplicable => respondJson(&ctx, .not_found, .{ .@"error" = "Not Found" }, alloc) catch unreachable,
else => unreachable,
};
}
}
};
pub fn main() anyerror!void {
2022-07-10 05:05:01 +00:00
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
2022-07-11 00:12:52 +00:00
var srv = try RequestServer.init(gpa.allocator());
srv.listenAndRun(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable);
2022-05-20 04:46:39 +00:00
}