╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 //! start a server with some fake data and then launch 2 //! the TUI. this provides a nice way to test things 3 //! out safely. 4 5 const std = @import("std"); 6 const hx = @import("haxy"); 7 const srv = hx.serve; 8 const evt = hx.event; 9 const xit = hx.xit; 10 const rp = xit.repo; 11 12 pub fn main() !void { 13 var gpa: std.heap.DebugAllocator(.{}) = .init; 14 defer _ = gpa.deinit(); 15 const allocator = gpa.allocator(); 16 17 var threaded = std.Io.Threaded.init(allocator, .{}); 18 defer threaded.deinit(); 19 const io = threaded.io(); 20 21 const temp_dir_name = "temp-try"; 22 23 // create the temp dir 24 const cwd = std.Io.Dir.cwd(); 25 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 26 if (temp_dir_or_err) |*temp_dir| { 27 temp_dir.close(io); 28 try cwd.deleteTree(io, temp_dir_name); 29 } else |_| {} 30 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 31 defer cwd.deleteTree(io, temp_dir_name) catch {}; 32 defer temp_dir.close(io); 33 34 const cwd_path = try std.process.currentPathAlloc(io, allocator); 35 defer allocator.free(cwd_path); 36 37 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "server" }); 38 defer allocator.free(server_path); 39 40 // create the admin repo 41 { 42 const work_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "server", "admin" }); 43 defer allocator.free(work_path); 44 45 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 46 const Repo = rp.Repo(.xit, repo_opts); 47 var repo = try Repo.init(io, allocator, .{ .path = work_path }); 48 defer repo.deinit(io, allocator); 49 50 var arena = std.heap.ArenaAllocator.init(allocator); 51 defer arena.deinit(); 52 53 // define test events 54 55 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 56 const alice_id = evt.EventWithId.randomId(prng.random()); 57 const repo_event_id = evt.EventWithId.randomId(prng.random()); 58 59 var alice_password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 60 const alice_password_hash = try evt.User.hashPassword("correct horse battery staple", &alice_password_hash_buf, io); 61 62 const events_to_consume = [_]evt.EventWithId{ 63 .{ 64 .id = std.fmt.bytesToHex(alice_id, .lower), 65 .event = .{ 66 .user = .{ 67 .name = "Alice Example", 68 .email = "alice@example.test", 69 .password_hash = alice_password_hash, 70 }, 71 }, 72 }, 73 .{ 74 .id = std.fmt.bytesToHex(repo_event_id, .lower), 75 .event = .{ 76 .repo = .{ 77 .user_id = &alice_id, 78 .name = "ziglings", 79 .enable_issue = true, 80 }, 81 }, 82 }, 83 }; 84 85 // insert users and repos as commits in the repo 86 { 87 var json: std.Io.Writer.Allocating = .init(allocator); 88 defer json.deinit(); 89 90 for (events_to_consume) |event| { 91 json.clearRetainingCapacity(); 92 93 try std.json.Stringify.value(event, .{}, &json.writer); 94 95 // commit the event into a special branch 96 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, .{ .kind = .head, .name = "haxy/meta" }); 97 } 98 } 99 100 // consume events into the database 101 try evt.consume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }); 102 } 103 104 // start server 105 106 var null_writer = std.Io.Writer.Discarding.init(&.{}); 107 var environ_map = std.process.Environ.Map.init(allocator); 108 defer environ_map.deinit(); 109 const run_opts = hx.main.RunOpts{ .out = &null_writer.writer, .err = &null_writer.writer, .environ_map = &environ_map }; 110 111 try srv.run(.xit, .{}, io, allocator, cwd_path, .{ 112 .data_dir = server_path, 113 .tui = true, 114 }, run_opts.err); 115 } 116