Skip to content

Commit

Permalink
Updated web server lib
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Oct 13, 2024
1 parent b9195fa commit 37ee3b2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ neoforge_version=21.1.42
parchment_mc_version=1.21
parchment_mapping_version=2024.07.28
rhino_version=2101.2.5-build.54
tiny_server_version=1.0.0-build.6
tiny_server_version=1.0.0-build.15
gif_lib_version=1.7

architectury_version=13.0.6
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/web/JsonContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.latvian.apps.tinyserver.content.ResponseContent;
import dev.latvian.mods.kubejs.util.Lazy;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;
Expand Down Expand Up @@ -43,7 +44,7 @@ public String type() {
}

@Override
public void write(OutputStream out) throws Exception {
public void write(OutputStream out) throws IOException {
out.write(json.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.List;

public record LocalWebServer(HTTPServer<KJSHTTPRequest> server, String url, List<Endpoint> endpoints) {
public static final String SERVER_NAME = "KubeJS " + KubeJS.VERSION;

public record Endpoint(String method, String path) implements Comparable<Endpoint> {
@Override
public int compareTo(@NotNull LocalWebServer.Endpoint o) {
Expand All @@ -37,10 +39,11 @@ public static void start(BlockableEventLoop<?> eventLoop) {
var publicAddress = WebServerProperties.get().publicAddress;

registry.server.setDaemon(true);
registry.server.setServerName("KubeJS " + KubeJS.VERSION);
registry.server.setServerName(SERVER_NAME);
registry.server.setAddress(publicAddress.isEmpty() ? "127.0.0.1" : "0.0.0.0");
registry.server.setPort(WebServerProperties.get().port);
registry.server.setMaxPortShift(10);
registry.server.setMaxKeepAliveConnections(3);

var url = "http://localhost:" + registry.server.start();
KubeJS.LOGGER.info("Started the local web server at " + url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import dev.latvian.apps.tinyserver.ServerRegistry;
import dev.latvian.apps.tinyserver.http.response.HTTPPayload;
import dev.latvian.apps.tinyserver.http.response.HTTPResponse;
import dev.latvian.apps.tinyserver.http.response.HTTPStatus;
import dev.latvian.apps.tinyserver.ws.Frame;
Expand Down Expand Up @@ -129,6 +130,7 @@ private static void reloadInternalServer() {
private static HTTPResponse getHomepage(KJSHTTPRequest req) {
var list = new ArrayList<String>();
list.add("KubeJS Local Web Server [" + KubeJS.PROXY.getWebServerWindowTitle() + "]");
list.add(HTTPPayload.DATE_TIME_FORMATTER.format(req.startTime()));
list.add("");

list.add("Loaded Plugins:");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.blaze3d.vertex.VertexSorting;
import dev.latvian.apps.tinyserver.content.ResponseContent;
import dev.latvian.apps.tinyserver.http.response.HTTPPayload;
import dev.latvian.apps.tinyserver.http.response.HTTPResponse;
import dev.latvian.apps.tinyserver.http.response.HTTPResponseBuilder;
import dev.latvian.apps.tinyserver.http.response.HTTPStatus;
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.KubeJSPaths;
Expand All @@ -22,6 +22,7 @@
import dev.latvian.mods.kubejs.util.CachedComponentObject;
import dev.latvian.mods.kubejs.util.Cast;
import dev.latvian.mods.kubejs.web.KJSHTTPRequest;
import dev.latvian.mods.kubejs.web.LocalWebServer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
Expand Down Expand Up @@ -61,6 +62,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.file.Files;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -233,7 +235,7 @@ private static HTTPResponse renderAnimated(KJSHTTPRequest req, String dir, @Null
req.runInMainThread(() -> {
for (var response : responses) {
try {
var content = new ContentGrabber();
var content = new ContentGrabber(LocalWebServer.SERVER_NAME, req.startTime());
response.build(content);

if (content.body != null && bodyKeys.add(new BodyKey(content.body))) {
Expand Down Expand Up @@ -444,9 +446,13 @@ public static HTTPResponse fluidTag(KJSHTTPRequest req) throws Exception {
return renderAnimated(req, "fluid_tag", buf, list);
}

private static class ContentGrabber extends HTTPResponseBuilder {
private static class ContentGrabber extends HTTPPayload {
private byte[] body = null;

public ContentGrabber(String serverName, Instant serverTime) {
super(serverName, serverTime);
}

@Override
public void setBody(ResponseContent body) {
try {
Expand Down

0 comments on commit 37ee3b2

Please sign in to comment.