diff --git a/server/src/main/java/xyz/e3ndr/athena/Config.java b/server/src/main/java/xyz/e3ndr/athena/Config.java index 5f1a879..2cfe2b9 100644 --- a/server/src/main/java/xyz/e3ndr/athena/Config.java +++ b/server/src/main/java/xyz/e3ndr/athena/Config.java @@ -20,6 +20,7 @@ public class Config { private boolean enableCudaAcceleration; // -1 to disable. + private int webUiPort = 8127; private int httpPort = 8125; private int ftpPort = 8126; diff --git a/server/src/main/java/xyz/e3ndr/athena/Launcher.java b/server/src/main/java/xyz/e3ndr/athena/Launcher.java index 8fb83e1..cd675e8 100644 --- a/server/src/main/java/xyz/e3ndr/athena/Launcher.java +++ b/server/src/main/java/xyz/e3ndr/athena/Launcher.java @@ -6,8 +6,11 @@ import co.casterlabs.commons.async.AsyncTask; import co.casterlabs.rakurai.json.Rson; import co.casterlabs.rakurai.json.serialization.JsonParseException; +import co.casterlabs.sora.SoraFramework; +import lombok.Getter; import xyz.e3ndr.athena.server.ftp.AthenaFtpServer; import xyz.e3ndr.athena.server.http.AthenaHttpServer; +import xyz.e3ndr.athena.webui.AthenaUIServer; import xyz.e3ndr.fastloggingframework.FastLoggingFramework; import xyz.e3ndr.fastloggingframework.logging.FastLogger; import xyz.e3ndr.fastloggingframework.logging.LogLevel; @@ -17,11 +20,15 @@ public class Launcher { private static final FastLogger logger = new FastLogger(); + private static @Getter Config config; + public static void main(String[] args) throws Exception { ClassLoader.getPlatformClassLoader().setDefaultAssertionStatus(true); - Config config = null; + // Some pre-init. + SoraFramework.LOGGER.setCurrentLevel(LogLevel.WARNING); + // Load the config. if (configFile.exists()) { try { config = Rson.DEFAULT.fromJson(Files.readString(configFile.toPath()), Config.class); @@ -76,9 +83,10 @@ public static void main(String[] args) throws Exception { Files.writeString(configFile.toPath(), Rson.DEFAULT.toJson(config).toString(true)); - Config $config_pointer = config; - AsyncTask.createNonDaemon(() -> new AthenaHttpServer().start($config_pointer)); - AsyncTask.createNonDaemon(() -> new AthenaFtpServer().start($config_pointer)); + // Go! + AsyncTask.createNonDaemon(() -> new AthenaHttpServer().start(config)); + AsyncTask.createNonDaemon(() -> new AthenaFtpServer().start(config)); + AsyncTask.createNonDaemon(() -> new AthenaUIServer().start(config)); } } diff --git a/server/src/main/java/xyz/e3ndr/athena/server/http/MetaRoutes.java b/server/src/main/java/xyz/e3ndr/athena/server/http/MetaRoutes.java index b764c8f..b47bc1c 100644 --- a/server/src/main/java/xyz/e3ndr/athena/server/http/MetaRoutes.java +++ b/server/src/main/java/xyz/e3ndr/athena/server/http/MetaRoutes.java @@ -5,6 +5,7 @@ import co.casterlabs.sora.api.http.HttpProvider; import co.casterlabs.sora.api.http.SoraHttpSession; import co.casterlabs.sora.api.http.annotations.HttpEndpoint; +import xyz.e3ndr.athena.Launcher; class MetaRoutes implements HttpProvider { @@ -15,4 +16,25 @@ public HttpResponse onWellKnown(SoraHttpSession session) { .setMimeType("text/plain"); } + @HttpEndpoint(uri = "/*") + public HttpResponse onGetIndex(SoraHttpSession session) { + int webUiPort = Launcher.getConfig().getWebUiPort(); + + if (webUiPort == -1) { + return HttpResponse + .newFixedLengthResponse( + StandardHttpStatus.OK, + "There's nothing here....." + ) + .setMimeType("text/plain"); + } else { + return HttpResponse + .newFixedLengthResponse( + StandardHttpStatus.OK, + "There's nothing here..... Are you looking for the UI? If so, that's on port " + webUiPort + "." + ) + .setMimeType("text/plain"); + } + } + } diff --git a/server/src/main/java/xyz/e3ndr/athena/webui/AthenaUIServer.java b/server/src/main/java/xyz/e3ndr/athena/webui/AthenaUIServer.java new file mode 100644 index 0000000..d63b0bd --- /dev/null +++ b/server/src/main/java/xyz/e3ndr/athena/webui/AthenaUIServer.java @@ -0,0 +1,76 @@ +package xyz.e3ndr.athena.webui; + +import org.jetbrains.annotations.Nullable; + +import co.casterlabs.sora.Sora; +import co.casterlabs.sora.SoraFramework; +import co.casterlabs.sora.SoraLauncher; +import co.casterlabs.sora.api.SoraPlugin; +import lombok.NonNull; +import xyz.e3ndr.athena.Config; +import xyz.e3ndr.athena.server.AthenaServer; +import xyz.e3ndr.fastloggingframework.logging.FastLogger; +import xyz.e3ndr.fastloggingframework.logging.LogLevel; + +public class AthenaUIServer implements AthenaServer { + + @Override + public void start(Config config) { + try { + int port = config.getWebUiPort(); + + if (port == -1) return; + + SoraFramework framework = new SoraLauncher() + .setPort(port) + .buildWithoutPluginLoader(); + + framework + .getSora() + .register(new AthenaSoraAdapter()); + + framework + .getServer() + .getLogger() + .setCurrentLevel(LogLevel.WARNING); + + framework.startHttpServer(); + + FastLogger.logStatic("Started http server on %d!", port); + } catch (Exception e) { + FastLogger.logStatic(LogLevel.SEVERE, "Unable to start http server:\n%s", e); + } + } + + public static class AthenaSoraAdapter extends SoraPlugin { + + @Override + public void onInit(Sora sora) { + } + + @Override + public void onClose() {} + + @Override + public @Nullable String getVersion() { + return null; + } + + @Override + public @Nullable String getAuthor() { + return null; + } + + @Override + public @NonNull String getName() { + return "Athena Web UI"; + } + + @Override + public @NonNull String getId() { + return "athena-webui"; + } + + } + +}