╭ 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 const Page = hx.page.Page; 12 13 pub fn main() !void { 14 var gpa: std.heap.DebugAllocator(.{}) = .init; 15 defer _ = gpa.deinit(); 16 const allocator = gpa.allocator(); 17 18 var threaded = std.Io.Threaded.init(allocator, .{}); 19 defer threaded.deinit(); 20 const io = threaded.io(); 21 22 const temp_dir_name = "temp-try"; 23 24 // create the temp dir 25 const cwd = std.Io.Dir.cwd(); 26 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 27 if (temp_dir_or_err) |*temp_dir| { 28 temp_dir.close(io); 29 try cwd.deleteTree(io, temp_dir_name); 30 } else |_| {} 31 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 32 defer cwd.deleteTree(io, temp_dir_name) catch {}; 33 defer temp_dir.close(io); 34 35 const cwd_path = try std.process.currentPathAlloc(io, allocator); 36 defer allocator.free(cwd_path); 37 38 var page_arena = std.heap.ArenaAllocator.init(allocator); 39 defer page_arena.deinit(); 40 41 // create the admin repo and build the Page from it 42 const page: Page = blk: { 43 const work_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "server", "admin" }); 44 defer allocator.free(work_path); 45 46 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 47 const Repo = rp.Repo(.xit, repo_opts); 48 var repo = try Repo.init(io, allocator, .{ .path = work_path }); 49 defer repo.deinit(io, allocator); 50 51 var arena = std.heap.ArenaAllocator.init(allocator); 52 defer arena.deinit(); 53 54 // define test events 55 56 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 57 58 const user_data = [_]struct { 59 name: []const u8, 60 display_name: []const u8, 61 email: []const u8, 62 }{ 63 .{ .name = "alice", .display_name = "Alice Example", .email = "alice@example.test" }, 64 .{ .name = "bob", .display_name = "Bob Smith", .email = "bob@example.test" }, 65 .{ .name = "carol", .display_name = "Carol Johnson", .email = "carol@example.test" }, 66 .{ .name = "dave", .display_name = "Dave Wilson", .email = "dave@example.test" }, 67 .{ .name = "eve", .display_name = "Eve Anderson", .email = "eve@example.test" }, 68 .{ .name = "frank", .display_name = "Frank Miller", .email = "frank@example.test" }, 69 .{ .name = "grace", .display_name = "Grace Lee", .email = "grace@example.test" }, 70 .{ .name = "henry", .display_name = "Henry Davis", .email = "henry@example.test" }, 71 .{ .name = "ivy", .display_name = "Ivy Martinez", .email = "ivy@example.test" }, 72 .{ .name = "jack", .display_name = "Jack Thompson", .email = "jack@example.test" }, 73 }; 74 75 const repo_data = [_]struct { 76 user_index: usize, 77 name: []const u8, 78 description: []const u8, 79 }{ 80 .{ .user_index = 0, .name = "ziglings", .description = "Learn the Zig programming language by fixing tiny broken programs" }, 81 .{ .user_index = 1, .name = "linux", .description = "Linux kernel source tree" }, 82 .{ .user_index = 2, .name = "kubernetes", .description = "Production-grade container orchestration" }, 83 .{ .user_index = 3, .name = "react", .description = "A declarative, efficient, and flexible JavaScript library for building user interfaces" }, 84 .{ .user_index = 4, .name = "typescript", .description = "TypeScript is a superset of JavaScript that compiles to clean JavaScript output" }, 85 .{ .user_index = 5, .name = "rust", .description = "Empowering everyone to build reliable and efficient software" }, 86 .{ .user_index = 6, .name = "go", .description = "The Go programming language" }, 87 .{ .user_index = 7, .name = "nodejs", .description = "Node.js JavaScript runtime" }, 88 .{ .user_index = 8, .name = "cpython", .description = "The Python programming language" }, 89 .{ .user_index = 9, .name = "docker", .description = "Container platform for developing, shipping, and running applications" }, 90 .{ .user_index = 0, .name = "vim", .description = "The ubiquitous text editor" }, 91 .{ .user_index = 1, .name = "neovim", .description = "Hyperextensible Vim-based text editor" }, 92 .{ .user_index = 2, .name = "emacs", .description = "GNU Emacs source code mirror" }, 93 .{ .user_index = 3, .name = "tmux", .description = "Terminal multiplexer" }, 94 .{ .user_index = 4, .name = "zsh", .description = "Mirror of the Z shell source code repository" }, 95 .{ .user_index = 5, .name = "git", .description = "Distributed version control system" }, 96 .{ .user_index = 6, .name = "mercurial", .description = "Source-control management tool" }, 97 .{ .user_index = 7, .name = "tensorflow", .description = "An end-to-end open source machine learning platform" }, 98 .{ .user_index = 8, .name = "pytorch", .description = "Tensors and dynamic neural networks in Python with strong GPU acceleration" }, 99 .{ .user_index = 9, .name = "numpy", .description = "The fundamental package for scientific computing with Python" }, 100 .{ .user_index = 0, .name = "pandas", .description = "Flexible and powerful data analysis and manipulation library for Python" }, 101 .{ .user_index = 1, .name = "scikit-learn", .description = "Machine learning in Python" }, 102 .{ .user_index = 2, .name = "nginx", .description = "High performance HTTP server and reverse proxy" }, 103 .{ .user_index = 3, .name = "redis", .description = "In-memory data structure store, used as a database, cache, and message broker" }, 104 .{ .user_index = 4, .name = "postgres", .description = "The world's most advanced open source relational database" }, 105 .{ .user_index = 5, .name = "sqlite", .description = "Self-contained, serverless, zero-configuration SQL database engine" }, 106 .{ .user_index = 6, .name = "mongodb", .description = "The MongoDB Database" }, 107 .{ .user_index = 7, .name = "elasticsearch", .description = "Free and open, distributed, RESTful search engine" }, 108 .{ .user_index = 8, .name = "kafka", .description = "Distributed event streaming platform" }, 109 .{ .user_index = 9, .name = "terraform", .description = "Infrastructure as code tool" }, 110 }; 111 112 var user_ids: [user_data.len][evt.event_id_size]u8 = undefined; 113 for (&user_ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); 114 115 var repo_event_ids: [repo_data.len][evt.event_id_size]u8 = undefined; 116 for (&repo_event_ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); 117 118 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 119 const password_hash = try evt.User.hashPassword("correct horse battery staple", &password_hash_buf, io); 120 121 var events_to_consume: [user_data.len + repo_data.len]evt.EventWithId = undefined; 122 for (user_data, 0..) |u, i| { 123 events_to_consume[i] = .{ 124 .id = std.fmt.bytesToHex(user_ids[i], .lower), 125 .event = .{ 126 .user = .{ 127 .name = u.name, 128 .display_name = u.display_name, 129 .email = u.email, 130 .password_hash = password_hash, 131 }, 132 }, 133 }; 134 } 135 for (repo_data, 0..) |r, i| { 136 events_to_consume[user_data.len + i] = .{ 137 .id = std.fmt.bytesToHex(repo_event_ids[i], .lower), 138 .event = .{ 139 .repo = .{ 140 .user_id = &user_ids[r.user_index], 141 .name = r.name, 142 .description = r.description, 143 .enable_issue = true, 144 }, 145 }, 146 }; 147 } 148 149 // insert users and repos as commits in the repo 150 { 151 var json: std.Io.Writer.Allocating = .init(allocator); 152 defer json.deinit(); 153 154 for (events_to_consume) |event| { 155 json.clearRetainingCapacity(); 156 157 try std.json.Stringify.value(event, .{}, &json.writer); 158 159 // commit the event into a special branch 160 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, .{ .kind = .head, .name = "haxy/meta" }); 161 } 162 } 163 164 // consume events into the database 165 try evt.consume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }); 166 167 break :blk .{ .user_repo = try .init(repo_opts, &page_arena, &repo) }; 168 }; 169 170 // start the server 171 172 var null_writer = std.Io.Writer.Discarding.init(&.{}); 173 var environ_map = std.process.Environ.Map.init(allocator); 174 defer environ_map.deinit(); 175 const run_opts = hx.main.RunOpts{ .out = &null_writer.writer, .err = &null_writer.writer, .environ_map = &environ_map }; 176 177 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "server" }); 178 defer allocator.free(server_path); 179 180 const Runnable = struct { 181 io: std.Io, 182 allocator: std.mem.Allocator, 183 page: *const Page, 184 185 pub fn run(self: @This()) !void { 186 // launch the TUI 187 try hx.ui.run(self.io, self.allocator, self.page); 188 } 189 }; 190 191 try srv.run(.xit, .{}, io, allocator, cwd_path, .{ 192 .data_dir = server_path, 193 }, run_opts.err, Runnable{ .io = io, .allocator = allocator, .page = &page }); 194 } 195