mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 11:47:45 +00:00
WebServer: Add optional positional port agument
This allows us to start WebServer on a port other than 8000, or multiple instances of it at the same time :^) Also print out what port was being used in the end.
This commit is contained in:
parent
38ba13e912
commit
421b6cd393
1 changed files with 15 additions and 3 deletions
|
@ -25,6 +25,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Client.h"
|
#include "Client.h"
|
||||||
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/EventLoop.h>
|
#include <LibCore/EventLoop.h>
|
||||||
#include <LibCore/TCPServer.h>
|
#include <LibCore/TCPServer.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -32,8 +33,18 @@
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
(void)argc;
|
u16 default_port = 8000;
|
||||||
(void)argv;
|
|
||||||
|
int port = default_port;
|
||||||
|
|
||||||
|
Core::ArgsParser args_parser;
|
||||||
|
args_parser.add_positional_argument(port, "Port to listen on", "port", Core::ArgsParser::Required::No);
|
||||||
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
|
if ((u16)port != port) {
|
||||||
|
printf("Warning: invalid port number: %d\n", port);
|
||||||
|
port = default_port;
|
||||||
|
}
|
||||||
|
|
||||||
if (pledge("stdio accept rpath inet unix cpath fattr", nullptr) < 0) {
|
if (pledge("stdio accept rpath inet unix cpath fattr", nullptr) < 0) {
|
||||||
perror("pledge");
|
perror("pledge");
|
||||||
|
@ -51,7 +62,8 @@ int main(int argc, char** argv)
|
||||||
client->start();
|
client->start();
|
||||||
};
|
};
|
||||||
|
|
||||||
server->listen({}, 8000);
|
server->listen({}, port);
|
||||||
|
printf("Listening on 0.0.0.0:%d\n", port);
|
||||||
|
|
||||||
if (unveil("/www", "r") < 0) {
|
if (unveil("/www", "r") < 0) {
|
||||||
perror("unveil");
|
perror("unveil");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue