Commit 5d7edc38 authored by KingRainbow44's avatar KingRainbow44
Browse files

Implement PR #657

parent 0dcf0862
package emu.grasscutter; package emu.grasscutter;
import emu.grasscutter.utils.ConfigContainer; import emu.grasscutter.utils.ConfigContainer;
import emu.grasscutter.utils.ConfigContainer.*;
import java.util.Locale; import java.util.Locale;
...@@ -40,6 +41,7 @@ public final class Configuration extends ConfigContainer { ...@@ -40,6 +41,7 @@ public final class Configuration extends ConfigContainer {
public static final Encryption HTTP_ENCRYPTION = config.server.http.encryption; public static final Encryption HTTP_ENCRYPTION = config.server.http.encryption;
public static final Policies HTTP_POLICIES = config.server.http.policies; public static final Policies HTTP_POLICIES = config.server.http.policies;
public static final Files HTTP_STATIC_FILES = config.server.http.files;
public static final GameOptions GAME_OPTIONS = config.server.game.gameOptions; public static final GameOptions GAME_OPTIONS = config.server.game.gameOptions;
public static final GameOptions.InventoryLimits INVENTORY_LIMITS = config.server.game.gameOptions.inventoryLimits; public static final GameOptions.InventoryLimits INVENTORY_LIMITS = config.server.game.gameOptions.inventoryLimits;
......
...@@ -2,13 +2,16 @@ package emu.grasscutter.server.http; ...@@ -2,13 +2,16 @@ package emu.grasscutter.server.http;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.Grasscutter.ServerDebugMode; import emu.grasscutter.Grasscutter.ServerDebugMode;
import emu.grasscutter.utils.FileUtils;
import express.Express; import express.Express;
import express.http.MediaType;
import io.javalin.Javalin; import io.javalin.Javalin;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.ssl.SslContextFactory;
import java.io.File; import java.io.File;
import java.io.IOException;
import static emu.grasscutter.Configuration.*; import static emu.grasscutter.Configuration.*;
import static emu.grasscutter.utils.Language.translate; import static emu.grasscutter.utils.Language.translate;
...@@ -62,7 +65,7 @@ public final class HttpServer { ...@@ -62,7 +65,7 @@ public final class HttpServer {
var sslContextFactory = new SslContextFactory.Server(); var sslContextFactory = new SslContextFactory.Server();
var keystoreFile = new File(HTTP_ENCRYPTION.keystore); var keystoreFile = new File(HTTP_ENCRYPTION.keystore);
if(!keystoreFile.exists()) {; if(!keystoreFile.exists()) {
HTTP_ENCRYPTION.useEncryption = false; HTTP_ENCRYPTION.useEncryption = false;
HTTP_ENCRYPTION.useInRouting = false; HTTP_ENCRYPTION.useInRouting = false;
...@@ -137,15 +140,25 @@ public final class HttpServer { ...@@ -137,15 +140,25 @@ public final class HttpServer {
*/ */
public static class DefaultRequestRouter implements Router { public static class DefaultRequestRouter implements Router {
@Override public void applyRoutes(Express express, Javalin handle) { @Override public void applyRoutes(Express express, Javalin handle) {
express.get("/", (req, res) -> res.send(""" express.get("/", (request, response) -> {
<!DOCTYPE html> File file = new File(HTTP_STATIC_FILES.errorFile);
<html> if(!file.exists())
<head> response.send("""
<meta charset="utf8"> <!DOCTYPE html>
</head> <html>
<body>%s</body> <head>
</html> <meta charset="utf8">
""".formatted(translate("messages.status.welcome")))); </head>
<body>%s</body>
</html>
""".formatted(translate("messages.status.welcome")));
else {
final var filePath = file.getPath();
final MediaType fromExtension = MediaType.getByExtension(filePath.substring(filePath.lastIndexOf(".") + 1));
response.type((fromExtension != null) ? fromExtension.getMIME() : "text/plain")
.send(FileUtils.read(filePath));
}
});
} }
} }
...@@ -158,7 +171,10 @@ public final class HttpServer { ...@@ -158,7 +171,10 @@ public final class HttpServer {
if(SERVER.debugLevel == ServerDebugMode.MISSING) if(SERVER.debugLevel == ServerDebugMode.MISSING)
Grasscutter.getLogger().info(translate("messages.dispatch.unhandled_request_error", context.method(), context.url())); Grasscutter.getLogger().info(translate("messages.dispatch.unhandled_request_error", context.method(), context.url()));
context.contentType("text/html"); context.contentType("text/html");
context.result("""
File file = new File(HTTP_STATIC_FILES.errorFile);
if(!file.exists())
context.result("""
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
...@@ -170,6 +186,12 @@ public final class HttpServer { ...@@ -170,6 +186,12 @@ public final class HttpServer {
</body> </body>
</html> </html>
"""); """);
else {
final var filePath = file.getPath();
final MediaType fromExtension = MediaType.getByExtension(filePath.substring(filePath.lastIndexOf(".") + 1));
context.contentType((fromExtension != null) ? fromExtension.getMIME() : "text/plain")
.result(FileUtils.read(filePath));
}
}); });
} }
} }
......
...@@ -17,7 +17,7 @@ import static emu.grasscutter.Grasscutter.config; ...@@ -17,7 +17,7 @@ import static emu.grasscutter.Grasscutter.config;
*/ */
public class ConfigContainer { public class ConfigContainer {
private static int version() { private static int version() {
return 2; return 3;
} }
/** /**
...@@ -125,6 +125,7 @@ public class ConfigContainer { ...@@ -125,6 +125,7 @@ public class ConfigContainer {
public Encryption encryption = new Encryption(); public Encryption encryption = new Encryption();
public Policies policies = new Policies(); public Policies policies = new Policies();
public Files files = new Files();
} }
public static class Game { public static class Game {
...@@ -227,6 +228,11 @@ public class ConfigContainer { ...@@ -227,6 +228,11 @@ public class ConfigContainer {
public String nickName = "Server"; public String nickName = "Server";
public String signature = "Welcome to Grasscutter!"; public String signature = "Welcome to Grasscutter!";
} }
public static class Files {
public String indexFile = "./index.html";
public String errorFile = "./404.html";
}
/* Objects. */ /* Objects. */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment