╭ 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", 68 .display_name = "Alice Example", 69 .email = "alice@example.test", 70 .password_hash = alice_password_hash, 71 }, 72 }, 73 }, 74 .{ 75 .id = std.fmt.bytesToHex(repo_event_id, .lower), 76 .event = .{ 77 .repo = .{ 78 .user_id = &alice_id, 79 .name = "ziglings", 80 .description = "Learn the Zig programming language by fixing tiny broken programs", 81 .enable_issue = true, 82 }, 83 }, 84 }, 85 }; 86 87 // insert users and repos as commits in the repo 88 { 89 var json: std.Io.Writer.Allocating = .init(allocator); 90 defer json.deinit(); 91 92 for (events_to_consume) |event| { 93 json.clearRetainingCapacity(); 94 95 try std.json.Stringify.value(event, .{}, &json.writer); 96 97 // commit the event into a special branch 98 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, .{ .kind = .head, .name = "haxy/meta" }); 99 } 100 } 101 102 // consume events into the database 103 try evt.consume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }); 104 } 105 106 // start server 107 108 var null_writer = std.Io.Writer.Discarding.init(&.{}); 109 var environ_map = std.process.Environ.Map.init(allocator); 110 defer environ_map.deinit(); 111 const run_opts = hx.main.RunOpts{ .out = &null_writer.writer, .err = &null_writer.writer, .environ_map = &environ_map }; 112 113 try srv.run(.xit, .{}, io, allocator, cwd_path, .{ 114 .data_dir = server_path, 115 .tui = true, 116 }, run_opts.err); 117 } 118