From 4eaedae0b795c1b68975fd5eb1dac833d2ff174e Mon Sep 17 00:00:00 2001 From: Melledy <52122272+Melledy@users.noreply.github.com> Date: Thu, 1 Sep 2022 13:17:21 -0700 Subject: [PATCH] Move the remaining `HttpUtils` function over to `Router` --- .../grasscutter/server/http/HttpServer.java | 1 - .../emu/grasscutter/server/http/Router.java | 18 ++++++++++++++++++ .../GachaMappingRequestHandler.java | 1 - .../documentation/HandbookRequestHandler.java | 1 - .../http/documentation/RootRequestHandler.java | 1 - .../http/handlers/AnnouncementsHandler.java | 11 +++++------ .../server/http/handlers/GachaHandler.java | 1 - .../server/http/handlers/GenericHandler.java | 12 +++++------- .../http/objects/WebStaticVersionResponse.java | 1 - .../java/emu/grasscutter/utils/HttpUtils.java | 18 ------------------ 10 files changed, 28 insertions(+), 37 deletions(-) delete mode 100644 src/main/java/emu/grasscutter/utils/HttpUtils.java diff --git a/src/main/java/emu/grasscutter/server/http/HttpServer.java b/src/main/java/emu/grasscutter/server/http/HttpServer.java index 2f3617f24..2678d8f04 100644 --- a/src/main/java/emu/grasscutter/server/http/HttpServer.java +++ b/src/main/java/emu/grasscutter/server/http/HttpServer.java @@ -3,7 +3,6 @@ package emu.grasscutter.server.http; import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter.ServerDebugMode; import emu.grasscutter.utils.FileUtils; -import emu.grasscutter.utils.HttpUtils; import io.javalin.Javalin; import io.javalin.core.util.JavalinLogger; import io.javalin.http.ContentType; diff --git a/src/main/java/emu/grasscutter/server/http/Router.java b/src/main/java/emu/grasscutter/server/http/Router.java index 3e3f2877b..1525344be 100644 --- a/src/main/java/emu/grasscutter/server/http/Router.java +++ b/src/main/java/emu/grasscutter/server/http/Router.java @@ -1,6 +1,7 @@ package emu.grasscutter.server.http; import io.javalin.Javalin; +import io.javalin.http.Handler; /** * Defines routes for an {@link Javalin} instance. @@ -12,4 +13,21 @@ public interface Router { * @param javalin A Javalin instance. */ void applyRoutes(Javalin javalin); + + /** + * Applies this handler to all endpoint types + * @param javalin A Javalin instance. + * @param path + * @param ctx + * @return The Javalin instance. + */ + public default Javalin allRoutes(Javalin javalin, String path, Handler ctx) { + javalin.get(path, ctx); + javalin.post(path, ctx); + javalin.put(path, ctx); + javalin.patch(path, ctx); + javalin.delete(path, ctx); + + return javalin; + } } diff --git a/src/main/java/emu/grasscutter/server/http/documentation/GachaMappingRequestHandler.java b/src/main/java/emu/grasscutter/server/http/documentation/GachaMappingRequestHandler.java index 0ec277dc3..0a9bb3bfa 100644 --- a/src/main/java/emu/grasscutter/server/http/documentation/GachaMappingRequestHandler.java +++ b/src/main/java/emu/grasscutter/server/http/documentation/GachaMappingRequestHandler.java @@ -1,7 +1,6 @@ package emu.grasscutter.server.http.documentation; import emu.grasscutter.tools.Tools; -import emu.grasscutter.utils.HttpUtils; import emu.grasscutter.utils.Language; import io.javalin.http.ContentType; import io.javalin.http.Context; diff --git a/src/main/java/emu/grasscutter/server/http/documentation/HandbookRequestHandler.java b/src/main/java/emu/grasscutter/server/http/documentation/HandbookRequestHandler.java index 0bf7fdc6b..97acc4f12 100644 --- a/src/main/java/emu/grasscutter/server/http/documentation/HandbookRequestHandler.java +++ b/src/main/java/emu/grasscutter/server/http/documentation/HandbookRequestHandler.java @@ -10,7 +10,6 @@ import emu.grasscutter.data.excels.ItemData; import emu.grasscutter.data.excels.MonsterData; import emu.grasscutter.data.excels.SceneData; import emu.grasscutter.utils.FileUtils; -import emu.grasscutter.utils.HttpUtils; import emu.grasscutter.utils.Language; import emu.grasscutter.utils.Utils; import io.javalin.http.ContentType; diff --git a/src/main/java/emu/grasscutter/server/http/documentation/RootRequestHandler.java b/src/main/java/emu/grasscutter/server/http/documentation/RootRequestHandler.java index 7165498e0..5df75287c 100644 --- a/src/main/java/emu/grasscutter/server/http/documentation/RootRequestHandler.java +++ b/src/main/java/emu/grasscutter/server/http/documentation/RootRequestHandler.java @@ -5,7 +5,6 @@ import static emu.grasscutter.utils.Language.translate; import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.FileUtils; -import emu.grasscutter.utils.HttpUtils; import emu.grasscutter.utils.Utils; import io.javalin.http.ContentType; import io.javalin.http.Context; diff --git a/src/main/java/emu/grasscutter/server/http/handlers/AnnouncementsHandler.java b/src/main/java/emu/grasscutter/server/http/handlers/AnnouncementsHandler.java index 00ac7b5ca..f49f4b513 100644 --- a/src/main/java/emu/grasscutter/server/http/handlers/AnnouncementsHandler.java +++ b/src/main/java/emu/grasscutter/server/http/handlers/AnnouncementsHandler.java @@ -5,7 +5,6 @@ import emu.grasscutter.data.DataLoader; import emu.grasscutter.server.http.objects.HttpJsonResponse; import emu.grasscutter.server.http.Router; import emu.grasscutter.utils.FileUtils; -import emu.grasscutter.utils.HttpUtils; import emu.grasscutter.utils.Utils; import io.javalin.Javalin; import io.javalin.http.ContentType; @@ -23,15 +22,15 @@ import java.util.Objects; public final class AnnouncementsHandler implements Router { @Override public void applyRoutes(Javalin javalin) { // hk4e-api-os.hoyoverse.com - HttpUtils.allRoutes(javalin, "/common/hk4e_global/announcement/api/getAlertPic", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"total\":0,\"list\":[]}}")); + this.allRoutes(javalin, "/common/hk4e_global/announcement/api/getAlertPic", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"total\":0,\"list\":[]}}")); // hk4e-api-os.hoyoverse.com - HttpUtils.allRoutes(javalin,"/common/hk4e_global/announcement/api/getAlertAnn", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"alert\":false,\"alert_id\":0,\"remind\":true}}")); + this.allRoutes(javalin,"/common/hk4e_global/announcement/api/getAlertAnn", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"alert\":false,\"alert_id\":0,\"remind\":true}}")); // hk4e-api-os.hoyoverse.com - HttpUtils.allRoutes(javalin,"/common/hk4e_global/announcement/api/getAnnList", AnnouncementsHandler::getAnnouncement); + this.allRoutes(javalin,"/common/hk4e_global/announcement/api/getAnnList", AnnouncementsHandler::getAnnouncement); // hk4e-api-os-static.hoyoverse.com - HttpUtils.allRoutes(javalin,"/common/hk4e_global/announcement/api/getAnnContent", AnnouncementsHandler::getAnnouncement); + this.allRoutes(javalin,"/common/hk4e_global/announcement/api/getAnnContent", AnnouncementsHandler::getAnnouncement); // hk4e-sdk-os.hoyoverse.com - HttpUtils.allRoutes(javalin,"/hk4e_global/mdk/shopwindow/shopwindow/listPriceTier", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"suggest_currency\":\"USD\",\"tiers\":[]}}")); + this.allRoutes(javalin,"/hk4e_global/mdk/shopwindow/shopwindow/listPriceTier", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"suggest_currency\":\"USD\",\"tiers\":[]}}")); javalin.get("/hk4e/announcement/*", AnnouncementsHandler::getPageResources); } diff --git a/src/main/java/emu/grasscutter/server/http/handlers/GachaHandler.java b/src/main/java/emu/grasscutter/server/http/handlers/GachaHandler.java index 0ce69f9e7..59afe02c3 100644 --- a/src/main/java/emu/grasscutter/server/http/handlers/GachaHandler.java +++ b/src/main/java/emu/grasscutter/server/http/handlers/GachaHandler.java @@ -8,7 +8,6 @@ import emu.grasscutter.game.gacha.GachaSystem; import emu.grasscutter.game.player.Player; import emu.grasscutter.server.http.Router; import emu.grasscutter.utils.FileUtils; -import emu.grasscutter.utils.HttpUtils; import emu.grasscutter.utils.Utils; import io.javalin.Javalin; import io.javalin.http.ContentType; diff --git a/src/main/java/emu/grasscutter/server/http/handlers/GenericHandler.java b/src/main/java/emu/grasscutter/server/http/handlers/GenericHandler.java index 222d3ec8f..172ab2d9d 100644 --- a/src/main/java/emu/grasscutter/server/http/handlers/GenericHandler.java +++ b/src/main/java/emu/grasscutter/server/http/handlers/GenericHandler.java @@ -7,7 +7,6 @@ import emu.grasscutter.Grasscutter; import emu.grasscutter.server.http.objects.HttpJsonResponse; import emu.grasscutter.server.http.Router; import emu.grasscutter.server.http.objects.WebStaticVersionResponse; -import emu.grasscutter.utils.HttpUtils; import io.javalin.Javalin; import io.javalin.http.Context; @@ -18,9 +17,8 @@ public final class GenericHandler implements Router { @Override public void applyRoutes(Javalin javalin) { // hk4e-sdk-os.hoyoverse.com javalin.get("/hk4e_global/mdk/agreement/api/getAgreementInfos", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"marketing_agreements\":[]}}")); - // hk4e-sdk-os.hoyoverse.com - // this could be either GET or POST based on the observation of different clients - HttpUtils.allRoutes(javalin, "/hk4e_global/combo/granter/api/compareProtocolVersion", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"modified\":true,\"protocol\":{\"id\":0,\"app_id\":4,\"language\":\"en\",\"user_proto\":\"\",\"priv_proto\":\"\",\"major\":7,\"minimum\":0,\"create_time\":\"0\",\"teenager_proto\":\"\",\"third_proto\":\"\"}}}")); + // hk4e-sdk-os.hoyoverse.com (this could be either GET or POST based on the observation of different clients) + this.allRoutes(javalin, "/hk4e_global/combo/granter/api/compareProtocolVersion", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"modified\":true,\"protocol\":{\"id\":0,\"app_id\":4,\"language\":\"en\",\"user_proto\":\"\",\"priv_proto\":\"\",\"major\":7,\"minimum\":0,\"create_time\":\"0\",\"teenager_proto\":\"\",\"third_proto\":\"\"}}}")); // api-account-os.hoyoverse.com javalin.post("/account/risky/api/check", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"id\":\"none\",\"action\":\"ACTION_NONE\",\"geetest\":null}}")); @@ -36,11 +34,11 @@ public final class GenericHandler implements Router { javalin.post("/data_abtest_api/config/experiment/list", new HttpJsonResponse("{\"retcode\":0,\"success\":true,\"message\":\"\",\"data\":[{\"code\":1000,\"type\":2,\"config_id\":\"14\",\"period_id\":\"6036_99\",\"version\":\"1\",\"configs\":{\"cardType\":\"old\"}}]}")); // log-upload-os.mihoyo.com - HttpUtils.allRoutes(javalin, "/log/sdk/upload", new HttpJsonResponse("{\"code\":0}")); - HttpUtils.allRoutes(javalin, "/sdk/upload", new HttpJsonResponse("{\"code\":0}")); + this.allRoutes(javalin, "/log/sdk/upload", new HttpJsonResponse("{\"code\":0}")); + this.allRoutes(javalin, "/sdk/upload", new HttpJsonResponse("{\"code\":0}")); javalin.post("/sdk/dataUpload", new HttpJsonResponse("{\"code\":0}")); // /perf/config/verify?device_id=xxx&platform=x&name=xxx - HttpUtils.allRoutes(javalin, "/perf/config/verify", new HttpJsonResponse("{\"code\":0}")); + this.allRoutes(javalin, "/perf/config/verify", new HttpJsonResponse("{\"code\":0}")); // webstatic-sea.hoyoverse.com javalin.get("/admin/mi18n/plat_oversea/*", new WebStaticVersionResponse()); diff --git a/src/main/java/emu/grasscutter/server/http/objects/WebStaticVersionResponse.java b/src/main/java/emu/grasscutter/server/http/objects/WebStaticVersionResponse.java index 1007118d4..6217138e5 100644 --- a/src/main/java/emu/grasscutter/server/http/objects/WebStaticVersionResponse.java +++ b/src/main/java/emu/grasscutter/server/http/objects/WebStaticVersionResponse.java @@ -2,7 +2,6 @@ package emu.grasscutter.server.http.objects; import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.FileUtils; -import emu.grasscutter.utils.HttpUtils; import io.javalin.http.ContentType; import io.javalin.http.Context; import io.javalin.http.Handler; diff --git a/src/main/java/emu/grasscutter/utils/HttpUtils.java b/src/main/java/emu/grasscutter/utils/HttpUtils.java deleted file mode 100644 index c5384fac7..000000000 --- a/src/main/java/emu/grasscutter/utils/HttpUtils.java +++ /dev/null @@ -1,18 +0,0 @@ -package emu.grasscutter.utils; - -import io.javalin.Javalin; -import io.javalin.http.Handler; - -public final class HttpUtils { - - public static Javalin allRoutes(Javalin javalin, String path, Handler ctx) { - javalin.get(path, ctx); - javalin.post(path, ctx); - javalin.put(path, ctx); - javalin.patch(path, ctx); - javalin.delete(path, ctx); - - return javalin; - } - -}