WebServer.getInstance to get the current web server

This commit is contained in:
JFronny 2021-11-10 17:04:41 +01:00
parent 8096596683
commit 39555b3270
No known key found for this signature in database
GPG Key ID: BEC5ACBBD4EE17E5
2 changed files with 16 additions and 10 deletions

View File

@ -1,5 +1,7 @@
package io.gitlab.jfronny.libjf.web.api;
import io.gitlab.jfronny.libjf.web.impl.JfWeb;
import java.io.IOException;
import java.nio.file.Path;
@ -14,4 +16,8 @@ public interface WebServer {
void stop();
void restart();
boolean isActive();
static WebServer getInstance() {
return JfWeb.SERVER;
}
}

View File

@ -16,25 +16,25 @@ import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
public class JfWeb implements ClientModInitializer, DedicatedServerModInitializer, ModInitializer {
public final WebServer server;
public JfWeb() {
public static final WebServer SERVER;
static {
JfWebConfig.ensureValidPort();
server = new JfWebServer(JfWebConfig.port, JfWebConfig.maxConnections);
SERVER = new JfWebServer(JfWebConfig.port, JfWebConfig.maxConnections);
}
@Override
public void onInitializeClient() {
if (isEnabled()) {
ClientLifecycleEvents.CLIENT_STARTED.register(client -> server.restart());
ClientLifecycleEvents.CLIENT_STOPPING.register(client -> server.stop());
ClientLifecycleEvents.CLIENT_STARTED.register(client -> SERVER.restart());
ClientLifecycleEvents.CLIENT_STOPPING.register(client -> SERVER.stop());
}
}
@Override
public void onInitializeServer() {
if (isEnabled()) {
ServerLifecycleEvents.SERVER_STARTED.register(client -> server.restart());
ServerLifecycleEvents.SERVER_STOPPED.register(client -> server.stop());
ServerLifecycleEvents.SERVER_STARTED.register(client -> SERVER.restart());
ServerLifecycleEvents.SERVER_STOPPED.register(client -> SERVER.stop());
}
}
@ -46,7 +46,7 @@ public class JfWeb implements ClientModInitializer, DedicatedServerModInitialize
LiteralArgumentBuilder<ServerCommandSource> web = CommandManager.literal("web");
base.then(web);
web.executes(context -> {
if (server.isActive()) {
if (SERVER.isActive()) {
context.getSource().sendFeedback(new LiteralText("LibWeb is active. Use libweb restart to reload"), false);
}
else {
@ -57,7 +57,7 @@ public class JfWeb implements ClientModInitializer, DedicatedServerModInitialize
web.then(CommandManager.literal("restart").executes(context -> {
try {
context.getSource().sendFeedback(new LiteralText("Restarting LibWeb"), true);
server.restart();
SERVER.restart();
}
catch (Exception e) {
LibJf.LOGGER.error("Failed to run restart command", e);
@ -68,7 +68,7 @@ public class JfWeb implements ClientModInitializer, DedicatedServerModInitialize
dispatcher.register(base);
});
}
Runtime.getRuntime().addShutdownHook(new Thread(server::stop));
Runtime.getRuntime().addShutdownHook(new Thread(SERVER::stop));
}
private boolean isEnabled() {