Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option to change server port #17

Open
wants to merge 1 commit into
base: http-plugin
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repositories {
mavenCentral()
}

def runeLiteVersion = '1.6.10.1'
def runeLiteVersion = '1.6.27'

dependencies {
compileOnly group: 'net.runelite', name:'client', version: runeLiteVersion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package info.sigterm.plugins.httpserver;

import net.runelite.client.config.Config;
import net.runelite.client.config.ConfigGroup;
import net.runelite.client.config.ConfigItem;

@ConfigGroup("httpserver")
public interface HttpServerConfig extends Config
{

@ConfigItem(
keyName = "ports",
name = "Server Port",
description = "Comma separated list of ports. The first free port will be used."
)
default String ports()
{
return "8080";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.inject.Provides;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
Expand All @@ -12,6 +13,7 @@
import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.api.Skill;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.http.api.RuneLiteAPI;
Expand All @@ -24,15 +26,33 @@ public class HttpServerPlugin extends Plugin
@Inject
private Client client;

@Inject
private HttpServerConfig config;

private HttpServer server;

@Override
protected void startUp() throws Exception
{
server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/stats", new StatsHandler());
server.setExecutor(Executors.newSingleThreadExecutor());
server.start();

String[] ports = config.ports().split(",");
Exception ex = null;

for (String port : ports) {
try {
server = HttpServer.create(new InetSocketAddress(Integer.parseInt(port)), 0);
server.createContext("/stats", new StatsHandler());
server.setExecutor(Executors.newSingleThreadExecutor());
server.start();
return;
} catch (Exception e) {
ex = e;
}
}

if (ex != null) {
throw ex;
}
}

@Override
Expand All @@ -41,6 +61,12 @@ protected void shutDown() throws Exception
server.stop(1);
}

@Provides
HttpServerConfig provideConfig(ConfigManager configManager)
{
return configManager.getConfig(HttpServerConfig.class);
}

class StatsHandler implements HttpHandler
{
@Override
Expand Down