╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const xit = @import("xit"); 3 const rp = xit.repo; 4 const web = @import("./web.zig"); 5 const serve_ssh_protocol = @import("./serve_ssh_protocol.zig"); 6 const serve_ssh = @import("./serve_ssh.zig"); 7 const serve_http = @import("./serve_http.zig"); 8 9 pub const Options = struct { 10 http_listen: []const u8 = "127.0.0.1:8080", 11 ssh_listen: []const u8 = "127.0.0.1:8022", 12 wui_listen: []const u8 = "127.0.0.1:8000", 13 data_dir: []const u8 = ".", 14 15 // the port from `wui_listen`, for the TUI footer's url. 16 pub fn wuiPort(self: Options) !u16 { 17 return (try parseListenAddress(self.wui_listen)).port; 18 } 19 }; 20 21 const ListenAddress = struct { 22 host: []const u8, 23 port: u16, 24 }; 25 26 pub fn run( 27 comptime repo_kind: rp.RepoKind, 28 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 29 io: std.Io, 30 allocator: std.mem.Allocator, 31 cwd_path: []const u8, 32 options: Options, 33 err: *std.Io.Writer, 34 runnable: anytype, 35 ) !void { 36 // create the data dir 37 38 const data_dir_path = try std.fs.path.resolve(allocator, &.{ cwd_path, options.data_dir }); 39 defer allocator.free(data_dir_path); 40 41 const data_dir = try std.Io.Dir.cwd().createDirPathOpen(io, data_dir_path, .{}); 42 defer data_dir.close(io); 43 44 // create the repos dir 45 46 const repo_root_path = try std.fs.path.resolve(allocator, &.{ data_dir_path, "repos" }); 47 defer allocator.free(repo_root_path); 48 49 try std.Io.Dir.cwd().createDirPath(io, repo_root_path); 50 51 // the admin repo path is where the user/repo metadata events live 52 53 const admin_repo_path = try std.fs.path.resolve(allocator, &.{ data_dir_path, "admin" }); 54 defer allocator.free(admin_repo_path); 55 56 // create http listener 57 58 const http_listen_address = try parseListenAddress(options.http_listen); 59 const http_address = try std.Io.net.IpAddress.parseIp4(http_listen_address.host, http_listen_address.port); 60 var http_server = try http_address.listen(io, .{ .reuse_address = true }); 61 defer http_server.deinit(io); 62 63 // create ssh listener 64 65 const ssh_listen_address = try parseListenAddress(options.ssh_listen); 66 const ssh_address = try std.Io.net.IpAddress.parseIp4(ssh_listen_address.host, ssh_listen_address.port); 67 var ssh_server = try ssh_address.listen(io, .{ .reuse_address = true }); 68 defer ssh_server.deinit(io); 69 70 // load or generate the SSH host key 71 const host_key = try serve_ssh_protocol.HostKey.loadOrGenerate(io, allocator, data_dir_path); 72 73 // disk-backed store for web login sessions 74 const session_store = try web.SessionStore.init(io, data_dir); 75 defer session_store.deinit(); 76 77 // create wui listener 78 79 const wui_listen_address = try parseListenAddress(options.wui_listen); 80 const wui_address = try std.Io.net.IpAddress.parseIp4(wui_listen_address.host, wui_listen_address.port); 81 var wui_server = try wui_address.listen(io, .{ .reuse_address = true }); 82 defer wui_server.deinit(io); 83 84 // start task group 85 86 var tasks: std.Io.Group = .init; 87 defer tasks.cancel(io); 88 89 // run listeners 90 91 try err.print("serving HTTP on {s}, repo root {s}\n", .{ options.http_listen, repo_root_path }); 92 try err.flush(); 93 94 serve_http.runListener(repo_kind, any_repo_opts, io, allocator, repo_root_path, admin_repo_path, &http_server, &tasks, err); 95 96 try err.print("serving SSH on {s}\n", .{options.ssh_listen}); 97 try err.flush(); 98 99 const ssh_session_handler = serve_ssh.SessionHandler{ 100 .admin_repo_path = admin_repo_path, 101 .repo_root_path = repo_root_path, 102 .wui_port = wui_listen_address.port, 103 .err = err, 104 }; 105 serve_ssh.runListener(io, allocator, &host_key, &ssh_session_handler, &ssh_server, &tasks, err); 106 107 try err.print("serving web UI on http://{s}/\n", .{options.wui_listen}); 108 try err.flush(); 109 110 web.run(io, allocator, &wui_server, &tasks, admin_repo_path, session_store, err); 111 112 if (@TypeOf(runnable) != void) { 113 try runnable.run(); 114 } else { 115 try tasks.await(io); 116 } 117 } 118 119 fn parseListenAddress(value: []const u8) !ListenAddress { 120 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 121 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 122 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 123 return .{ .host = value[0..colon], .port = port }; 124 } 125