╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
164 .dirty_repos = &dirty_repos, 165 .session_store = session_store, 166 .git_http_port = git_http_port, 167 .git_ssh_port = git_ssh_port, 168 .git_ssh_prefix = options.git_ssh_prefix, 169 } }, err); 170 171 if (@TypeOf(runnable) != void) { 172 try runnable.run(wui_server.socket.address.getPort(), git_http_port, git_ssh_port); 173 } else { 174 try tasks.await(io); 175 } 176 } 177 178 // serve just the web UI for a single local repo, running `runnable` (the 179 // local TUI) in the foreground while the listener runs in the background. 180 // when the default port is taken, a random ephemeral port is used instead; 181 // the bound port is passed to `runnable.run`. 182 pub fn runLocal( 183 io: std.Io, 184 allocator: std.mem.Allocator, 185 local: ui.RepoSource, 186 err: *std.Io.Writer, 187 runnable: anytype, 188 ) !void { 189 // always fall back, so another instance viewing a second repo just binds 190 // its own port 191 const wui_listen_address = try parseListenAddress((Options{}).wui_listen); 192 var wui_server = try listen(io, wui_listen_address, .fallback); 193 defer wui_server.deinit(io); 194 195 var tasks: std.Io.Group = .init; 196 defer tasks.cancel(io); 197 198 runWebListener(io, allocator, &wui_server, &tasks, .{ .local = local }, err); 199 200 try runnable.run(wui_server.socket.address.getPort()); 201 } 202 203 fn runWebListener( 204 io: std.Io, 205 allocator: std.mem.Allocator, 206 net_server: *std.Io.net.Server, 207 tasks: *std.Io.Group, 208 host: web.Host, 209 err: *std.Io.Writer, 210 ) void { 211 const Context = struct { 212 io: std.Io, 213 allocator: std.mem.Allocator, 214 host: web.Host, 215 err: *std.Io.Writer, 216 }; 217 218 const handle = struct { 219 fn h(ctx: Context, stream: std.Io.net.Stream) void { 220 defer stream.close(ctx.io); 221 web.handleConnection(ctx.io, ctx.allocator, stream, ctx.host, ctx.err) catch |request_err| { 222 serve_common.logError(ctx.io, ctx.err, "web ui request failed: {s}\n", .{@errorName(request_err)}); 223 }; 224 } 225 }.h; 226 227 serve_common.runListener(io, net_server, tasks, err, "web ui", Context{ 228 .io = io, 229 .allocator = allocator, 230 .host = host, 231 .err = err, 232 }, handle); 233 } 234 235 // what a listener does with a port that is already taken 236 const PortPolicy = enum { 237 exact, 238 // leave it to whoever holds it and take an OS-assigned port instead 239 fallback, 240 }; 241 242 fn listen(io: std.Io, listen_address: ListenAddress, policy: PortPolicy) !std.Io.net.Server { 243 const address = try std.Io.net.IpAddress.parseIp4(listen_address.host, listen_address.port); 244 return switch (policy) { 245 .exact => try address.listen(io, .{}), 246 .fallback => address.listen(io, .{}) catch |listen_err| switch (listen_err) { 247 error.AddressInUse => blk: { 248 const any_port = try std.Io.net.IpAddress.parseIp4(listen_address.host, 0); 249 break :blk try any_port.listen(io, .{}); 250 }, 251 else => |e| return e, 252 }, 253 }; 254 } 255 256 fn parseListenAddress(value: []const u8) !ListenAddress { 257 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 258 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 259 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 260 return .{ .host = value[0..colon], .port = port }; 261 } 262