Moved to built in webserver because javalin forced me to add kotlin as a dependancy
Some checks failed
build / build (push) Has been cancelled
Some checks failed
build / build (push) Has been cancelled
This commit is contained in:
parent
739247ebf1
commit
d6c69fad21
3 changed files with 42 additions and 18 deletions
|
@ -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 {
|
||||||
|
|
|
@ -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
|
|
@ -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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Javalin app = ((Javalin)Javalin.create((config) -> config.showJavalinBanner = false).get("/", (ctx) -> {
|
HttpServer srv = HttpServer.create(new InetSocketAddress(port), 0);
|
||||||
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());
|
srv.createContext("/", new HttpHandler() {
|
||||||
ctx.contentType(ContentType.TEXT_HTML).result(html.formatted(rows));
|
@Override
|
||||||
})).start(port);
|
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) {
|
private static String escape(String s) {
|
||||||
return s.replace("&", "&").replace("<", "<").replace(">", ">");
|
return s.replace("&", "&")
|
||||||
|
.replace("<", "<")
|
||||||
|
.replace(">", ">");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue