Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Add ability to set interface to listen on
Browse files Browse the repository at this point in the history
  • Loading branch information
dangmai committed May 15, 2022
1 parent 1405c87 commit be0b455
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/java/net/dangmai/serializer/server/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ShutdownHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
Expand All @@ -20,15 +21,23 @@ public class HttpServer {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpServer.class);
public static void main(String[] args) throws Exception {
Options cliOptions = new Options();
cliOptions.addOption("p", "port", true, "The port to listen to");
cliOptions.addOption("h", "host", true, "The host to listen on, default to 0.0.0.0");
cliOptions.addOption("p", "port", true, "The port to listen on, default to 2117");
cliOptions.addOption("s", "shutdown", false, "Allow the server to be remotely shut down");
cliOptions.addOption("a", "password", true, "Password to remotely shut down server");

CommandLineParser cliParser = new DefaultParser();
CommandLine cmd = cliParser.parse(cliOptions, args);

int port = cmd.hasOption("p") ? Integer.parseInt(cmd.getOptionValue("p")) : 2117;
Server server = new Server(port);
String host = cmd.hasOption("h") ? cmd.getOptionValue("h") : "0.0.0.0";
Server server = new Server();

ServerConnector httpConnector = new ServerConnector(server);
httpConnector.setHost(host);
httpConnector.setPort(port);
httpConnector.setIdleTimeout(-1);
server.addConnector(httpConnector);

ServletContextHandler servletContextHandler = new ServletContextHandler(NO_SESSIONS);
servletContextHandler.setContextPath("/api");
Expand Down

0 comments on commit be0b455

Please sign in to comment.