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}"
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 {

View file

@ -10,11 +10,10 @@ loader_version=0.17.2
loom_version=1.11-SNAPSHOT
# Mod Properties
mod_version=1.0.0
mod_version=1.0.1
maven_group=tf.jsw.mcadmintracker
archives_base_name=mcadmintracker
# Dependencies
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;
import io.javalin.Javalin;
import io.javalin.http.ContentType;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.stream.Collectors;
@ -19,21 +24,42 @@ public class Web {
public void start(int port) throws IOException {
String html;
try (InputStream in = this.getClass().getResourceAsStream("/assets/mcadmintracker/index.html")) {
if (in == null) {
throw new IllegalStateException("file not found");
}
html = new String(in.readAllBytes(), StandardCharsets.US_ASCII);
try (InputStream in = getClass().getResourceAsStream("/assets/mcadmintracker/index.html")) {
if (in == null) throw new IllegalStateException("template not found");
html = new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
Javalin app = ((Javalin)Javalin.create((config) -> config.showJavalinBanner = false).get("/", (ctx) -> {
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());
ctx.contentType(ContentType.TEXT_HTML).result(html.formatted(rows));
})).start(port);
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);
}
byte[] bytes = html.formatted(rows).getBytes(StandardCharsets.UTF_8);
ex.getResponseHeaders().set("Content-Type", "text/html; charset=utf-8");
ex.sendResponseHeaders(200, bytes.length);
try (OutputStream os = ex.getResponseBody()) { os.write(bytes); }
}
});
srv.setExecutor(null); // default thread-pool
srv.start();
}
private static String escape(String s) {
return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
return s.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;");
}
}