mach: build+tools: allow address and port of http server be configurable

for wasm

MACH_ADDRESS and MACH_PORT environment variable are added.
This commit is contained in:
iddev5 2022-05-27 18:36:47 +05:30 committed by Stephen Gutekanst
parent 8b8ed4dc8f
commit 8d4c51738e
2 changed files with 16 additions and 4 deletions

View file

@ -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, "");

View file

@ -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 <application-name> <address> <port>\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,
);