WebServer: Add optional listen address argument

With this we can make the Webserver listen on
another address than the default of "0.0.0.0".
This commit is contained in:
Edwin Hoksberg 2021-05-30 01:00:53 +02:00 committed by Linus Groh
parent 4190fd2199
commit 2deffeb74d
Notes: sideshowbarker 2024-07-18 17:10:42 +09:00

View file

@ -15,16 +15,25 @@
int main(int argc, char** argv)
{
String default_listen_address = "0.0.0.0";
u16 default_port = 8000;
const char* root_path = "/www";
String listen_address = default_listen_address;
int port = default_port;
Core::ArgsParser args_parser;
args_parser.add_option(listen_address, "IP address to listen on", "listen-address", 'l', "listen_address");
args_parser.add_option(port, "Port to listen on", "port", 'p', "port");
args_parser.add_positional_argument(root_path, "Path to serve the contents of", "path", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
auto ipv4_address = IPv4Address::from_string(listen_address);
if (!ipv4_address.has_value()) {
warnln("Invalid listen address: {}", listen_address);
return 1;
}
if ((u16)port != port) {
printf("Warning: invalid port number: %d\n", port);
port = default_port;
@ -53,12 +62,12 @@ int main(int argc, char** argv)
client->start();
};
if (!server->listen({}, port)) {
warnln("Failed to listen on 0.0.0.0:{}", port);
if (!server->listen(ipv4_address.value(), port)) {
warnln("Failed to listen on {}:{}", ipv4_address.value(), port);
return 1;
}
outln("Listening on 0.0.0.0:{}", port);
outln("Listening on {}:{}", ipv4_address.value(), port);
if (unveil("/res/icons", "r") < 0) {
perror("unveil");