Skip to content

Commit

Permalink
Server: Start transitioning web-simple into Athena.
Browse files Browse the repository at this point in the history
  • Loading branch information
e3ndr committed Sep 22, 2023
1 parent 9e9f3a0 commit 24fd6b3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 4 deletions.
1 change: 1 addition & 0 deletions server/src/main/java/xyz/e3ndr/athena/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
16 changes: 12 additions & 4 deletions server/src/main/java/xyz/e3ndr/athena/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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));
}

}
22 changes: 22 additions & 0 deletions server/src/main/java/xyz/e3ndr/athena/server/http/MetaRoutes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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");
}
}

}
76 changes: 76 additions & 0 deletions server/src/main/java/xyz/e3ndr/athena/webui/AthenaUIServer.java
Original file line number Diff line number Diff line change
@@ -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";
}

}

}

0 comments on commit 24fd6b3

Please sign in to comment.