╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
73 defer allocator.free(admin_repo_path); 74 75 // create http listener 76 77 const port_policy: PortPolicy = if (options.fallback_on_address_in_use) .fallback else .exact; 78 var http_maybe: ?BoundListener = if (options.http_listen) |value| blk: { 79 const address = try parseListenAddress(value); 80 break :blk .{ .address = address, .server = try listen(io, address, port_policy) }; 81 } else null; 82 defer if (http_maybe) |*http| http.deinit(io); 83 84 // disk-backed store for web login sessions 85 const session_store = try web.SessionStore.init(io, data_dir); 86 defer session_store.deinit(); 87 88 // create wui listener 89 90 const wui_listen_address = try parseListenAddress(options.wui_listen); 91 var wui_server = try listen(io, wui_listen_address, port_policy); 92 defer wui_server.deinit(io); 93 94 // create ssh listener and its session state 95 96 const git_http_port: ?u16 = if (http_maybe) |*http| http.port() else null; 97 var ssh_maybe: ?SshService = if (options.ssh_listen) |value| blk: { 98 const address = try parseListenAddress(value); 99 var listener = BoundListener{ .address = address, .server = try listen(io, address, port_policy) }; 100 errdefer listener.deinit(io); 101 const ssh_port = listener.port(); 102 break :blk .{ 103 .listener = listener, 104 .host_key = try serve_ssh_protocol.HostKey.loadOrGenerate(io, allocator, data_dir_path), 105 .session_handler = .{ 106 .admin_repo_path = admin_repo_path, 107 .repo_root_path = repo_root_path, 108 .wui_port = wui_server.socket.address.getPort(), 109 .git_http_port = git_http_port, 110 .git_ssh_port = ssh_port, 111 .git_ssh_prefix = options.git_ssh_prefix, 112 .err = err, 113 }, 114 }; 115 } else null; 116 defer if (ssh_maybe) |*ssh| ssh.listener.deinit(io); 117 118 // start task group 119 120 var tasks: std.Io.Group = .init; 121 defer tasks.cancel(io); 122 123 // run listeners, printing the ports actually bound 124 125 const git_ssh_port: ?u16 = if (ssh_maybe) |*ssh| ssh.listener.port() else null; 126 127 if (http_maybe) |*http| { 128 try err.print("serving HTTP on {s}:{d}, repo root {s}\n", .{ http.address.host, http.port(), repo_root_path }); 129 try err.flush(); 130 serve_http.runListener(repo_kind, any_repo_opts, io, allocator, repo_root_path, admin_repo_path, &http.server, &tasks, err); 131 } 132 133 if (ssh_maybe) |*ssh| { 134 try err.print("serving SSH on {s}:{d}, host key ED25519 {s}\n", .{ ssh.listener.address.host, ssh.listener.port(), try ssh.host_key.fingerprint(allocator) }); 135 try err.flush(); 136 serve_ssh.runListener(io, allocator, &ssh.host_key, &ssh.session_handler, &ssh.watchdog, &ssh.listener.server, &tasks, err); 137 } 138 139 try err.print("serving web UI on http://{s}:{d}/\n", .{ wui_listen_address.host, wui_server.socket.address.getPort() }); 140 try err.flush(); 141 142 runWebListener(io, allocator, &wui_server, &tasks, .{ .server = .{ 143 .admin_repo_path = admin_repo_path, 144 .session_store = session_store, 145 .git_http_port = git_http_port, 146 .git_ssh_port = git_ssh_port, 147 .git_ssh_prefix = options.git_ssh_prefix, 148 } }, err); 149 150 if (@TypeOf(runnable) != void) { 151 try runnable.run(wui_server.socket.address.getPort(), git_http_port, git_ssh_port); 152 } else { 153 try tasks.await(io); 154 } 155 } 156 157 // serve just the web UI for a single local repo, running `runnable` (the 158 // local TUI) in the foreground while the listener runs in the background. 159 // when the default port is taken, a random ephemeral port is used instead; 160 // the bound port is passed to `runnable.run`. 161 pub fn runLocal( 162 io: std.Io, 163 allocator: std.mem.Allocator, 164 local: ui.RepoSource, 165 err: *std.Io.Writer, 166 runnable: anytype, 167 ) !void { 168 // always fall back, so another instance viewing a second repo just binds 169 // its own port 170 const wui_listen_address = try parseListenAddress((Options{}).wui_listen); 171 var wui_server = try listen(io, wui_listen_address, .fallback); 172 defer wui_server.deinit(io); 173 174 var tasks: std.Io.Group = .init; 175 defer tasks.cancel(io); 176 177 runWebListener(io, allocator, &wui_server, &tasks, .{ .local = local }, err); 178 179 try runnable.run(wui_server.socket.address.getPort()); 180 } 181 182 fn runWebListener( 183 io: std.Io, 184 allocator: std.mem.Allocator, 185 net_server: *std.Io.net.Server, 186 tasks: *std.Io.Group, 187 host: web.Host, 188 err: *std.Io.Writer, 189 ) void { 190 const Context = struct { 191 io: std.Io, 192 allocator: std.mem.Allocator, 193 host: web.Host, 194 err: *std.Io.Writer, 195 }; 196 197 const handle = struct { 198 fn h(ctx: Context, stream: std.Io.net.Stream) void { 199 defer stream.close(ctx.io); 200 web.handleConnection(ctx.io, ctx.allocator, stream, ctx.host, ctx.err) catch |request_err| { 201 serve_common.logError(ctx.io, ctx.err, "web ui request failed: {s}\n", .{@errorName(request_err)}); 202 }; 203 } 204 }.h; 205 206 serve_common.runListener(io, net_server, tasks, err, "web ui", Context{ 207 .io = io, 208 .allocator = allocator, 209 .host = host, 210 .err = err, 211 }, handle); 212 } 213 214 // what a listener does with a port that is already taken 215 const PortPolicy = enum { 216 exact, 217 // leave it to whoever holds it and take an OS-assigned port instead 218 fallback, 219 }; 220 221 fn listen(io: std.Io, listen_address: ListenAddress, policy: PortPolicy) !std.Io.net.Server { 222 const address = try std.Io.net.IpAddress.parseIp4(listen_address.host, listen_address.port); 223 return switch (policy) { 224 .exact => try address.listen(io, .{}), 225 .fallback => address.listen(io, .{}) catch |listen_err| switch (listen_err) { 226 error.AddressInUse => blk: { 227 const any_port = try std.Io.net.IpAddress.parseIp4(listen_address.host, 0); 228 break :blk try any_port.listen(io, .{}); 229 }, 230 else => |e| return e, 231 }, 232 }; 233 } 234 235 fn parseListenAddress(value: []const u8) !ListenAddress { 236 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 237 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 238 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 239 return .{ .host = value[0..colon], .port = port }; 240 } 241