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