const std = @import("std"); const builtin = @import("builtin"); const http = @import("http"); const util = @import("util"); const api = @import("./api.zig"); const models = @import("./models.zig"); const Uuid = util.Uuid; // 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{ Route.new(.GET, "/healthcheck", healthcheck), Route.new(.GET, "/notes/:id", getNote), Route.new(.POST, "/notes", createNote), Route.new(.POST, "/users", createUser), }, }; 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 fn respondJson(ctx: *http.server.Context, status: http.Status, value: anytype, alloc: std.mem.Allocator) !void { var headers = http.Headers.init(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(); } fn parseRequestBody(comptime T: type, ctx: *http.server.Context, alloc: std.mem.Allocator) !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 = alloc }); return parsed; } fn freeRequestBody(value: anytype, alloc: std.mem.Allocator) void { std.json.parseFree(@TypeOf(value), value, .{ .allocator = alloc }); } fn createNote(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { const info = try parseRequestBody(api.CreateInfo(models.Note), ctx, srv.alloc); defer freeRequestBody(info, srv.alloc); const note = try srv.api.createNote(info); try respondJson(ctx, .created, note, srv.alloc); } fn createUser(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { const info = try parseRequestBody(api.CreateInfo(models.User), ctx, srv.alloc); defer freeRequestBody(info, srv.alloc); const user = try srv.api.createUser(info); try respondJson(ctx, .created, user, srv.alloc); } fn getNote(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void { const id_str = args.get("id") orelse return error.NotFound; const id = Uuid.parse(id_str) catch return respondJson(ctx, .bad_request, .{ .@"error" = "Invalid UUID" }, srv.alloc); const note = (try srv.api.getNote(id, srv.alloc)) orelse return respondJson(ctx, .not_found, .{ .@"error" = "Note not found" }, srv.alloc); defer api.free(srv.alloc, note); try respondJson(ctx, .ok, note, srv.alloc); } fn healthcheck(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { try respondJson(ctx, .ok, .{ .status = "ok" }, srv.alloc); } const RequestServer = struct { alloc: std.mem.Allocator, api: api.ApiServer, fn init(alloc: std.mem.Allocator) !RequestServer { return RequestServer{ .alloc = alloc, .api = try api.ApiServer.init(alloc), }; } fn listenAndRun(self: *RequestServer, addr: std.net.Address) noreturn { var srv = http.Server.listen(addr) catch unreachable; defer srv.shutdown(); while (true) { var buf: [1 << 20]u8 = undefined; var fba = std.heap.FixedBufferAllocator.init(&buf); const alloc = fba.allocator(); var ctx = srv.accept(alloc) catch unreachable; defer ctx.close(); 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 { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var srv = try RequestServer.init(gpa.allocator()); srv.listenAndRun(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable); }