From 8d4c51738e20065c55162687c12ab9c9a1d48747 Mon Sep 17 00:00:00 2001 From: iddev5 Date: Fri, 27 May 2022 18:36:47 +0530 Subject: [PATCH] mach: build+tools: allow address and port of http server be configurable for wasm MACH_ADDRESS and MACH_PORT environment variable are added. --- build.zig | 7 +++++-- tools/http-server.zig | 13 +++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/build.zig b/build.zig index 170c813b..ffcfe34f 100644 --- a/build.zig +++ b/build.zig @@ -229,18 +229,21 @@ pub const App = struct { // This is because running the server would block the process (a limitation of current // RunStep). So we assume that (xdg-)open is a launcher and not a blocking process. + const address = std.process.getEnvVarOwned(app.b.allocator, "MACH_ADDRESS") catch "127.0.0.1"; + const port = std.process.getEnvVarOwned(app.b.allocator, "MACH_PORT") catch "8000"; + const launch = app.b.addSystemCommand(&.{ switch (builtin.os.tag) { .macos, .windows => "open", else => "xdg-open", // Assume linux-like }, // TODO: use actual application name - "http://127.0.0.1:8000/application.html", + app.b.fmt("http://{s}:{s}/{s}.html", .{ address, port, "application" }), }); launch.step.dependOn(&app.getInstallStep().?.step); const serve = http_server.run(); - serve.addArg("application"); + serve.addArgs(&.{ "application", address, port }); serve.step.dependOn(&launch.step); serve.cwd = app.b.getInstallPath(web_install_dir, ""); diff --git a/tools/http-server.zig b/tools/http-server.zig index 4fe36067..ca7ce818 100644 --- a/tools/http-server.zig +++ b/tools/http-server.zig @@ -12,14 +12,23 @@ pub fn main() !u8 { const args = try std.process.argsAlloc(allocator); defer std.process.argsFree(allocator, args); - std.debug.print("Served at http://127.0.0.1:8000/{s}.html\n", .{args[1]}); + if (args.len < 4) { + std.debug.print("Usage: http-server
\n", .{}); + return 0; + } + + const application_name = args[1]; + const address = args[2]; + const port = try std.fmt.parseUnsigned(u16, args[3], 10); + + std.debug.print("Served at http://{s}:{}/{s}.html\n", .{ address, port, application_name }); try file_server.init(allocator, .{ .dir_path = "." }); defer file_server.deinit(); try http.listenAndServe( allocator, - try std.net.Address.parseIp("127.0.0.1", 8000), + try std.net.Address.parseIp(address, port), {}, file_server.serve, );