Make server port and update interval configurable
This commit is contained in:
parent
495d55e242
commit
a24c0547de
5 changed files with 126 additions and 2 deletions
|
@ -4,6 +4,9 @@ import io.prometheus.client.exporter.HTTPServer;
|
|||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import ru.ruscalworld.fabricexporter.config.MainConfig;
|
||||
import ru.ruscalworld.fabricexporter.metrics.OnlinePlayers;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -11,13 +14,23 @@ import java.util.Timer;
|
|||
|
||||
public class FabricExporter implements ModInitializer {
|
||||
private MinecraftServer server;
|
||||
private MainConfig config;
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
try {
|
||||
MainConfig config = new MainConfig("exporter.properties");
|
||||
config.load();
|
||||
this.setConfig(config);
|
||||
} catch (IOException e) {
|
||||
FabricExporter.getLogger().fatal("Unable to load config");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
ServerLifecycleEvents.SERVER_STARTED.register(this::setServer);
|
||||
|
||||
try {
|
||||
HTTPServer httpServer = new HTTPServer(1337);
|
||||
new HTTPServer(this.getConfig().getPort());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -26,7 +39,11 @@ public class FabricExporter implements ModInitializer {
|
|||
metricUpdater.registerMetric(new OnlinePlayers());
|
||||
|
||||
Timer timer = new Timer();
|
||||
timer.schedule(metricUpdater, 1000, 1000);
|
||||
timer.schedule(metricUpdater, 1000, this.getConfig().getUpdateInterval());
|
||||
}
|
||||
|
||||
public static Logger getLogger() {
|
||||
return LogManager.getLogger();
|
||||
}
|
||||
|
||||
public static String getMetricName(String name) {
|
||||
|
@ -40,4 +57,12 @@ public class FabricExporter implements ModInitializer {
|
|||
private void setServer(MinecraftServer server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public MainConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void setConfig(MainConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package ru.ruscalworld.fabricexporter.config;
|
||||
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import ru.ruscalworld.fabricexporter.FabricExporter;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Properties;
|
||||
|
||||
public abstract class Config {
|
||||
private final String fileName;
|
||||
|
||||
public Config(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public void load() throws IOException {
|
||||
Path path = FabricLoader.getInstance().getConfigDir().resolve(this.getFileName());
|
||||
if (!path.toFile().exists()) try {
|
||||
URL url = this.getClass().getClassLoader().getResource("config/" + this.getFileName());
|
||||
assert url != null;
|
||||
File file = new File(url.toURI());
|
||||
byte[] bytes = Files.readAllBytes(file.toPath());
|
||||
|
||||
Files.createFile(path);
|
||||
Files.write(path, bytes);
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
FabricExporter.getLogger().fatal("Unable to save default config");
|
||||
}
|
||||
|
||||
Properties properties = new Properties();
|
||||
properties.load(Files.newInputStream(path));
|
||||
this.parse(properties);
|
||||
}
|
||||
|
||||
public abstract void parse(Properties properties);
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package ru.ruscalworld.fabricexporter.config;
|
||||
|
||||
import ru.ruscalworld.fabricexporter.util.ConvertUtil;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public class MainConfig extends Config {
|
||||
private int port;
|
||||
private int updateInterval;
|
||||
|
||||
public MainConfig(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(Properties properties) {
|
||||
String portString = properties.getProperty("server-port", "25585");
|
||||
this.setPort(ConvertUtil.intToStringOrDefault(portString, 25585));
|
||||
|
||||
String updateIntervalString = properties.getProperty("update-interval", "1000");
|
||||
this.setUpdateInterval(ConvertUtil.intToStringOrDefault(updateIntervalString, 1000));
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public int getUpdateInterval() {
|
||||
return updateInterval;
|
||||
}
|
||||
|
||||
public void setUpdateInterval(int updateInterval) {
|
||||
this.updateInterval = updateInterval;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package ru.ruscalworld.fabricexporter.util;
|
||||
|
||||
public class ConvertUtil {
|
||||
public static int intToStringOrDefault(String input, int def) {
|
||||
int result = def;
|
||||
try {
|
||||
result = Integer.parseInt(input);
|
||||
} catch (Exception ignored) { }
|
||||
return result;
|
||||
}
|
||||
}
|
2
src/main/resources/config/exporter.properties
Normal file
2
src/main/resources/config/exporter.properties
Normal file
|
@ -0,0 +1,2 @@
|
|||
server-port=25585
|
||||
update-interval=1000
|
Loading…
Reference in a new issue