╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
30 server: std.Io.net.Server, 31 32 fn deinit(self: *BoundListener, io: std.Io) void { 33 self.server.deinit(io); 34 } 35 36 fn port(self: *const BoundListener) u16 { 37 return self.server.socket.address.getPort(); 38 } 39 }; 40 41 const SshService = struct { 42 listener: BoundListener, 43 host_key: serve_ssh_protocol.HostKey, 44 session_handler: serve_ssh.SessionHandler, 45 watchdog: serve_ssh.Watchdog = .{}, 46 }; 47 48 pub fn run( 49 comptime repo_kind: rp.RepoKind, 50 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 51 io: std.Io, 52 allocator: std.mem.Allocator, 53 cwd_path: []const u8, 54 options: Options, 55 err: *std.Io.Writer, 56 runnable: anytype, 57 ) !void { 58 // create the data dir 59 60 const data_dir_path = try std.Io.Dir.path.resolveAlloc(allocator, &.{ cwd_path, options.data_dir }); 61 defer allocator.free(data_dir_path); 62 63 const data_dir = try std.Io.Dir.cwd().createDirPathOpen(io, data_dir_path, .{}); 64 defer data_dir.close(io); 65 66 // create the users dir 67 68 const users_dir = try std.Io.Dir.path.resolveAlloc(allocator, &.{ data_dir_path, "users" }); 69 defer allocator.free(users_dir); 70 71 try std.Io.Dir.cwd().createDirPath(io, users_dir); 72 73 // the admin repo path is where the user/repo metadata events live 74 75 const admin_repo_path = try std.Io.Dir.path.resolveAlloc(allocator, &.{ data_dir_path, "admin" }); 76 defer allocator.free(admin_repo_path); 77 78 // create the admin repo on first run 79 { 80 var admin_repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |open_err| switch (open_err) { 81 error.RepoNotFound, error.FileNotFound => try evt.initAdminRepo(io, allocator, admin_repo_path), 82 else => |e| return e, 83 }; 84 defer admin_repo.deinit(io, allocator); 85 } 86 87 const port_policy: PortPolicy = if (options.fallback_on_address_in_use) .fallback else .exact; 88 89 // disk-backed store for web login sessions 90 const session_store = try web.SessionStore.init(io, data_dir); 91 defer session_store.deinit(); 92 93 // create web listener 94 95 const web_listen_address = try parseListenAddress(options.web_listen); 96 var web_server = try listen(io, web_listen_address, port_policy); 97 defer web_server.deinit(io); 98 99 const web_url = options.web_url orelse try allocator.print("http://localhost:{d}", .{web_server.socket.address.getPort()}); 100 defer if (options.web_url == null) allocator.free(web_url); 101 102 // create ssh listener and its session state 103 104 var ssh_url_owned: ?[]const u8 = null; 105 defer if (ssh_url_owned) |url| allocator.free(url); 106 var ssh_maybe: ?SshService = if (options.ssh_listen) |value| blk: { 107 const address = try parseListenAddress(value); 108 var listener = BoundListener{ .address = address, .server = try listen(io, address, port_policy) }; 109 errdefer listener.deinit(io); 110 const ssh_url = options.ssh_url orelse url_blk: { 111 const url = try allocator.print("ssh://localhost:{d}", .{listener.port()}); 112 ssh_url_owned = url; 113 break :url_blk url; 114 }; 115 break :blk .{ 116 .listener = listener, 117 .host_key = try serve_ssh_protocol.HostKey.loadOrGenerate(io, allocator, data_dir_path), 118 .session_handler = .{ 119 .admin_repo_path = admin_repo_path, 120 .server = .{ .users_dir = users_dir, .admin_repo_path = admin_repo_path }, 121 .web_url = web_url, 122 .ssh_url = ssh_url, 123 .git_ssh_prefix = options.git_ssh_prefix, 124 .err = err, 125 }, 126 }; 127 } else null; 128 defer if (ssh_maybe) |*ssh| ssh.listener.deinit(io); 129 130 // start task group 131 132 var tasks: std.Io.Group = .init; 133 defer tasks.cancel(io); 134 135 // run listeners, printing the ports actually bound 136 137 const ssh_url: ?[]const u8 = if (ssh_maybe) |*ssh| ssh.session_handler.ssh_url else null; 138 139 if (ssh_maybe) |*ssh| { 140 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) }); 141 try err.flush(); 142 serve_ssh.runListener(io, allocator, &ssh.host_key, &ssh.session_handler, &ssh.watchdog, &ssh.listener.server, &tasks, err); 143 } 144 145 try err.print("serving web UI and git over HTTP on http://{s}:{d}/, users dir {s}\n", .{ web_listen_address.host, web_server.socket.address.getPort(), users_dir }); 146 try err.flush(); 147 148 runWebListener(io, allocator, &web_server, &tasks, .{ .server = .{ 149 .admin_repo_path = admin_repo_path, 150 .users_dir = users_dir, 151 .session_store = session_store, 152 .web_url = web_url, 153 .ssh_url = ssh_url, 154 .git_ssh_prefix = options.git_ssh_prefix, 155 .serve_git = serve_http.gitHandler(repo_kind, any_repo_opts), 156 } }, err); 157 158 if (@TypeOf(runnable) != void) { 159 try runnable.run(web_url, ssh_url); 160 } else { 161 try tasks.await(io); 162 } 163 } 164 165 // serve just the web UI for a single local repo, running `runnable` (the 166 // local TUI) in the foreground while the listener runs in the background. 167 // when the default port is taken, a random ephemeral port is used instead; 168 // the web UI's url is passed to `runnable.run`. 169 pub fn runLocal( 170 io: std.Io, 171 allocator: std.mem.Allocator, 172 local: ui.RepoSource, 173 err: *std.Io.Writer, 174 runnable: anytype, 175 ) !void { 176 // always fall back, so another instance viewing a second repo just binds 177 // its own port 178 const web_listen_address = try parseListenAddress((Options{}).web_listen); 179 var web_server = try listen(io, web_listen_address, .fallback); 180 defer web_server.deinit(io); 181 182 var tasks: std.Io.Group = .init; 183 defer tasks.cancel(io); 184 185 runWebListener(io, allocator, &web_server, &tasks, .{ .local = local }, err); 186 187 const web_url = try allocator.print("http://localhost:{d}", .{web_server.socket.address.getPort()}); 188 defer allocator.free(web_url); 189 try runnable.run(web_url); 190 } 191 192 fn runWebListener( 193 io: std.Io, 194 allocator: std.mem.Allocator, 195 net_server: *std.Io.net.Server, 196 tasks: *std.Io.Group, 197 host: web.Host, 198 err: *std.Io.Writer, 199 ) void { 200 const Context = struct { 201 io: std.Io, 202 allocator: std.mem.Allocator, 203 host: web.Host, 204 err: *std.Io.Writer, 205 }; 206 207 const handle = struct { 208 fn h(ctx: Context, stream: std.Io.net.Stream) void { 209 defer stream.close(ctx.io); 210 web.handleConnection(ctx.io, ctx.allocator, stream, ctx.host, ctx.err) catch |request_err| { 211 serve_common.logError(ctx.io, ctx.err, "web ui request failed: {s}\n", .{@errorName(request_err)}); 212 }; 213 } 214 }.h; 215 216 serve_common.runListener(io, net_server, tasks, err, "web ui", Context{ 217 .io = io, 218 .allocator = allocator, 219 .host = host, 220 .err = err, 221 }, handle); 222 } 223 224 // what a listener does with a port that is already taken 225 const PortPolicy = enum { 226 exact, 227 // leave it to whoever holds it and take an OS-assigned port instead 228 fallback, 229 }; 230 231 fn listen(io: std.Io, listen_address: ListenAddress, policy: PortPolicy) !std.Io.net.Server { 232 const address = try std.Io.net.IpAddress.parseIp4(listen_address.host, listen_address.port); 233 return switch (policy) { 234 .exact => try address.listen(io, .{}), 235 .fallback => address.listen(io, .{}) catch |listen_err| switch (listen_err) { 236 error.AddressInUse => blk: { 237 const any_port = try std.Io.net.IpAddress.parseIp4(listen_address.host, 0); 238 break :blk try any_port.listen(io, .{}); 239 }, 240 else => |e| return e, 241 }, 242 }; 243 } 244 245 fn parseListenAddress(value: []const u8) !ListenAddress { 246 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 247 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 248 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 249 return .{ .host = value[0..colon], .port = port }; 250 } 251