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