Moved to built in webserver because javalin forced me to add kotlin as a dependancy
Some checks failed
build / build (push) Has been cancelled

This commit is contained in:
Jurn Wubben 2025-09-14 00:10:45 +02:00
parent 739247ebf1
commit d6c69fad21
3 changed files with 42 additions and 18 deletions

View file

@ -28,8 +28,7 @@ dependencies {
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
implementation "org.xerial:sqlite-jdbc:${project.sqlite_jdbc_version}" implementation "org.xerial:sqlite-jdbc:${project.sqlite_jdbc_version}"
implementation "io.javalin:javalin:${project.javalin_version}" include "org.xerial:sqlite-jdbc:${project.sqlite_jdbc_version}"
} }
processResources { processResources {

View file

@ -10,11 +10,10 @@ loader_version=0.17.2
loom_version=1.11-SNAPSHOT loom_version=1.11-SNAPSHOT
# Mod Properties # Mod Properties
mod_version=1.0.0 mod_version=1.0.1
maven_group=tf.jsw.mcadmintracker maven_group=tf.jsw.mcadmintracker
archives_base_name=mcadmintracker archives_base_name=mcadmintracker
# Dependencies # Dependencies
fabric_version=0.133.4+1.21.8 fabric_version=0.133.4+1.21.8
javalin_version=6.7.0
sqlite_jdbc_version=3.50.3.0 sqlite_jdbc_version=3.50.3.0

View file

@ -1,10 +1,15 @@
package tf.jsw.mcadmintracker; package tf.jsw.mcadmintracker;
import io.javalin.Javalin; import com.sun.net.httpserver.HttpServer;
import io.javalin.http.ContentType; import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -19,21 +24,42 @@ public class Web {
public void start(int port) throws IOException { public void start(int port) throws IOException {
String html; String html;
try (InputStream in = this.getClass().getResourceAsStream("/assets/mcadmintracker/index.html")) { try (InputStream in = getClass().getResourceAsStream("/assets/mcadmintracker/index.html")) {
if (in == null) { if (in == null) throw new IllegalStateException("template not found");
throw new IllegalStateException("file not found"); html = new String(in.readAllBytes(), StandardCharsets.UTF_8);
} }
html = new String(in.readAllBytes(), StandardCharsets.US_ASCII); HttpServer srv = HttpServer.create(new InetSocketAddress(port), 0);
srv.createContext("/", new HttpHandler() {
@Override
public void handle(HttpExchange ex) throws IOException {
String rows = null;
try {
rows = db.getAdminLog()
.stream()
.map(e -> "<tr><td><div>%s</div></td><td title=\"%s\"><div>%s</div></td><td><code>%s</code></td></tr>"
.formatted(FMT.format(new Date(e.time())),
escape(e.uuid()),
escape(e.name()),
escape(e.command())))
.collect(Collectors.joining());
} catch (SQLException e) {
throw new RuntimeException(e);
} }
Javalin app = ((Javalin)Javalin.create((config) -> config.showJavalinBanner = false).get("/", (ctx) -> { byte[] bytes = html.formatted(rows).getBytes(StandardCharsets.UTF_8);
String rows = (String)this.db.getAdminLog().stream().map((e) -> "<tr><td><div>%s</div></td><td title=\"%s\"><div>%s</div></td><td><code>%s</code></td></tr>".formatted(FMT.format(new Date(e.time())), escape(e.uuid()), escape(e.name()), escape(e.command()))).collect(Collectors.joining()); ex.getResponseHeaders().set("Content-Type", "text/html; charset=utf-8");
ctx.contentType(ContentType.TEXT_HTML).result(html.formatted(rows)); ex.sendResponseHeaders(200, bytes.length);
})).start(port); try (OutputStream os = ex.getResponseBody()) { os.write(bytes); }
}
});
srv.setExecutor(null); // default thread-pool
srv.start();
} }
private static String escape(String s) { private static String escape(String s) {
return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;"); return s.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;");
} }
} }