╭ 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 ui = hx.ui; 12 13 pub fn main(init: std.process.Init) !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 .environ = init.minimal.environ, 20 }); 21 defer threaded.deinit(); 22 const io = threaded.io(); 23 24 const temp_dir_name = "temp-try"; 25 26 // create the temp dir 27 const cwd = std.Io.Dir.cwd(); 28 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 29 if (temp_dir_or_err) |*temp_dir| { 30 temp_dir.close(io); 31 try cwd.deleteTree(io, temp_dir_name); 32 } else |_| {} 33 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 34 defer cwd.deleteTree(io, temp_dir_name) catch {}; 35 defer temp_dir.close(io); 36 37 const cwd_path = try std.process.currentPathAlloc(io, allocator); 38 defer allocator.free(cwd_path); 39 40 var page_arena = std.heap.ArenaAllocator.init(allocator); 41 defer page_arena.deinit(); 42 43 // create the admin repo and build the Page from it 44 const page: ui.Page = blk: { 45 const work_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "server", "admin" }); 46 defer allocator.free(work_path); 47 48 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 49 const Repo = rp.Repo(.xit, repo_opts); 50 var repo = try Repo.init(io, allocator, .{ .path = work_path }); 51 defer repo.deinit(io, allocator); 52 53 var arena = std.heap.ArenaAllocator.init(allocator); 54 defer arena.deinit(); 55 56 // define test events 57 58 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 59 60 const user_data = [_]struct { 61 name: []const u8, 62 display_name: []const u8, 63 email: []const u8, 64 }{ 65 .{ .name = "alice", .display_name = "Alice Tulley", .email = "alice@example.test" }, 66 .{ .name = "bob", .display_name = "Bob Smith", .email = "bob@example.test" }, 67 .{ .name = "carol", .display_name = "Carol Johnson", .email = "carol@example.test" }, 68 .{ .name = "dave", .display_name = "Dave Wilson", .email = "dave@example.test" }, 69 .{ .name = "eve", .display_name = "Eve Anderson", .email = "eve@example.test" }, 70 .{ .name = "frank", .display_name = "Frank Miller", .email = "frank@example.test" }, 71 .{ .name = "grace", .display_name = "Grace Lee", .email = "grace@example.test" }, 72 .{ .name = "henry", .display_name = "Henry Davis", .email = "henry@example.test" }, 73 .{ .name = "ivy", .display_name = "Ivy Martinez", .email = "ivy@example.test" }, 74 .{ .name = "jack", .display_name = "Jack Thompson", .email = "jack@example.test" }, 75 .{ .name = "kate", .display_name = "Kate Robinson", .email = "kate@example.test" }, 76 .{ .name = "liam", .display_name = "Liam Walker", .email = "liam@example.test" }, 77 .{ .name = "mona", .display_name = "Mona Patel", .email = "mona@example.test" }, 78 .{ .name = "noah", .display_name = "Noah Garcia", .email = "noah@example.test" }, 79 .{ .name = "olivia", .display_name = "Olivia Hernandez", .email = "olivia@example.test" }, 80 .{ .name = "peter", .display_name = "Peter Wright", .email = "peter@example.test" }, 81 .{ .name = "quinn", .display_name = "Quinn Foster", .email = "quinn@example.test" }, 82 .{ .name = "rachel", .display_name = "Rachel Bennett", .email = "rachel@example.test" }, 83 .{ .name = "sam", .display_name = "Sam Brooks", .email = "sam@example.test" }, 84 .{ .name = "tina", .display_name = "Tina Cooper", .email = "tina@example.test" }, 85 }; 86 87 const repo_data = [_]struct { 88 user_index: usize, 89 name: []const u8, 90 description: []const u8, 91 }{ 92 .{ .user_index = 0, .name = "ziglings", .description = "Learn the Zig programming language by fixing tiny broken programs" }, 93 .{ .user_index = 1, .name = "linux", .description = "Linux kernel source tree" }, 94 .{ .user_index = 2, .name = "kubernetes", .description = "Production-grade container orchestration" }, 95 .{ .user_index = 3, .name = "react", .description = "A declarative, efficient, and flexible JavaScript library for building user interfaces" }, 96 .{ .user_index = 4, .name = "typescript", .description = "TypeScript is a superset of JavaScript that compiles to clean JavaScript output" }, 97 .{ .user_index = 5, .name = "rust", .description = "Empowering everyone to build reliable and efficient software" }, 98 .{ .user_index = 6, .name = "go", .description = "The Go programming language" }, 99 .{ .user_index = 7, .name = "nodejs", .description = "Node.js JavaScript runtime" }, 100 .{ .user_index = 8, .name = "cpython", .description = "The Python programming language" }, 101 .{ .user_index = 9, .name = "docker", .description = "Container platform for developing, shipping, and running applications" }, 102 .{ .user_index = 0, .name = "vim", .description = "The ubiquitous text editor" }, 103 .{ .user_index = 1, .name = "neovim", .description = "Hyperextensible Vim-based text editor" }, 104 .{ .user_index = 2, .name = "emacs", .description = "GNU Emacs source code mirror" }, 105 .{ .user_index = 3, .name = "tmux", .description = "Terminal multiplexer" }, 106 .{ .user_index = 4, .name = "zsh", .description = "Mirror of the Z shell source code repository" }, 107 .{ .user_index = 5, .name = "git", .description = "Distributed version control system" }, 108 .{ .user_index = 6, .name = "mercurial", .description = "Source-control management tool" }, 109 .{ .user_index = 7, .name = "tensorflow", .description = "An end-to-end open source machine learning platform" }, 110 .{ .user_index = 8, .name = "pytorch", .description = "Tensors and dynamic neural networks in Python with strong GPU acceleration" }, 111 .{ .user_index = 9, .name = "numpy", .description = "The fundamental package for scientific computing with Python" }, 112 .{ .user_index = 0, .name = "pandas", .description = "Flexible and powerful data analysis and manipulation library for Python" }, 113 .{ .user_index = 1, .name = "scikit-learn", .description = "Machine learning in Python" }, 114 .{ .user_index = 2, .name = "nginx", .description = "High performance HTTP server and reverse proxy" }, 115 .{ .user_index = 3, .name = "redis", .description = "In-memory data structure store, used as a database, cache, and message broker" }, 116 .{ .user_index = 4, .name = "postgres", .description = "The world's most advanced open source relational database" }, 117 .{ .user_index = 5, .name = "sqlite", .description = "Self-contained, serverless, zero-configuration SQL database engine" }, 118 .{ .user_index = 6, .name = "mongodb", .description = "The MongoDB Database" }, 119 .{ .user_index = 7, .name = "elasticsearch", .description = "Free and open, distributed, RESTful search engine" }, 120 .{ .user_index = 8, .name = "kafka", .description = "Distributed event streaming platform" }, 121 .{ .user_index = 9, .name = "terraform", .description = "Infrastructure as code tool" }, 122 .{ .user_index = 10, .name = "svelte", .description = "Cybernetically enhanced web apps" }, 123 .{ .user_index = 11, .name = "vue", .description = "The progressive JavaScript framework" }, 124 .{ .user_index = 12, .name = "flask", .description = "The Python micro framework for building web applications" }, 125 .{ .user_index = 13, .name = "django", .description = "The web framework for perfectionists with deadlines" }, 126 .{ .user_index = 14, .name = "rails", .description = "Ruby on Rails web framework" }, 127 .{ .user_index = 15, .name = "phoenix", .description = "Peace of mind from prototype to production for Elixir web apps" }, 128 .{ .user_index = 16, .name = "laravel", .description = "The PHP framework for web artisans" }, 129 .{ .user_index = 17, .name = "prometheus", .description = "The Prometheus monitoring system and time series database" }, 130 .{ .user_index = 18, .name = "grafana", .description = "The open and composable observability and data visualization platform" }, 131 .{ .user_index = 19, .name = "ansible", .description = "Simple, agentless IT automation" }, 132 }; 133 134 var user_ids: [user_data.len][evt.event_id_size]u8 = undefined; 135 for (&user_ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); 136 137 var repo_event_ids: [repo_data.len][evt.event_id_size]u8 = undefined; 138 for (&repo_event_ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); 139 140 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 141 const password_hash = try evt.User.hashPassword("correct horse battery staple", &password_hash_buf, io); 142 143 var events_to_consume: [user_data.len + repo_data.len]evt.EventWithId = undefined; 144 for (user_data, 0..) |u, i| { 145 events_to_consume[i] = .{ 146 .id = std.fmt.bytesToHex(user_ids[i], .lower), 147 .event = .{ 148 .user = .{ 149 .name = u.name, 150 .display_name = u.display_name, 151 .email = u.email, 152 .password_hash = password_hash, 153 }, 154 }, 155 }; 156 } 157 for (repo_data, 0..) |r, i| { 158 events_to_consume[user_data.len + i] = .{ 159 .id = std.fmt.bytesToHex(repo_event_ids[i], .lower), 160 .event = .{ 161 .repo = .{ 162 .user_id = &user_ids[r.user_index], 163 .name = r.name, 164 .description = r.description, 165 .enable_issue = true, 166 }, 167 }, 168 }; 169 } 170 171 // insert users and repos as commits in the repo 172 { 173 var json: std.Io.Writer.Allocating = .init(allocator); 174 defer json.deinit(); 175 176 for (events_to_consume) |event| { 177 json.clearRetainingCapacity(); 178 179 try std.json.Stringify.value(event, .{}, &json.writer); 180 181 // commit the event into a special branch 182 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, .{ .kind = .head, .name = "haxy/meta" }); 183 } 184 } 185 186 // consume events into the database 187 try evt.consume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }); 188 189 break :blk .{ .home = try .init(repo_opts, &page_arena, &repo) }; 190 }; 191 192 // start the server 193 194 var cli = false; 195 196 var arg_it = try init.minimal.args.iterateAllocator(allocator); 197 defer arg_it.deinit(); 198 _ = arg_it.skip(); 199 while (arg_it.next()) |arg| { 200 if (std.mem.eql(u8, "--cli", arg)) { 201 cli = true; 202 } 203 } 204 205 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "server" }); 206 defer allocator.free(server_path); 207 208 if (cli) { 209 var stdout_writer = std.Io.File.stdout().writer(io, &.{}); 210 var stderr_writer = std.Io.File.stderr().writer(io, &.{}); 211 const run_opts = hx.main.RunOpts{ .out = &stdout_writer.interface, .err = &stderr_writer.interface }; 212 213 const Runnable = struct { 214 io: std.Io, 215 216 pub fn run(self: @This()) !void { 217 std.debug.print( 218 \\ 219 \\connect to the TUI with: 220 \\ ssh -p 8022 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null localhost 221 \\ 222 \\create a git repo and push over SSH: 223 \\ mkdir -p temp-try/client/test 224 \\ cd temp-try/client/test 225 \\ git init 226 \\ echo "hello" > hello.txt 227 \\ git add hello.txt 228 \\ git commit -m "let there be light" 229 \\ GIT_SSH_COMMAND='ssh -p 8022 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' git push localhost:test HEAD:master 230 \\ 231 \\to quit, press enter. 232 \\ 233 , .{}); 234 // portable stdin read via std.Io — equivalent to the 235 // std.posix.read(STDIN_FILENO, ...) that doesn't compile 236 // on windows. 237 var buf: [1]u8 = undefined; 238 var stdin_reader = std.Io.File.stdin().reader(self.io, &buf); 239 _ = stdin_reader.interface.takeByte() catch {}; 240 } 241 }; 242 243 try srv.run(.xit, .{}, io, allocator, cwd_path, .{ 244 .data_dir = server_path, 245 }, run_opts.err, Runnable{ .io = io }); 246 } else { 247 var null_writer = std.Io.Writer.Discarding.init(&.{}); 248 const run_opts = hx.main.RunOpts{ .out = &null_writer.writer, .err = &null_writer.writer }; 249 250 const Runnable = struct { 251 io: std.Io, 252 allocator: std.mem.Allocator, 253 page: *const ui.Page, 254 255 pub fn run(self: @This()) !void { 256 // launch the TUI 257 try hx.ui.run(self.io, self.allocator, self.page); 258 } 259 }; 260 261 try srv.run(.xit, .{}, io, allocator, cwd_path, .{ 262 .data_dir = server_path, 263 }, run_opts.err, Runnable{ .io = io, .allocator = allocator, .page = &page }); 264 } 265 } 266