From 4dc263961d259eb7989098469a704a5c1959b55a Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 19 Apr 2024 20:33:12 +0100 Subject: [PATCH] add cache control to endpoints --- src/main/java/xyz/mcutils/backend/Main.java | 1 - .../backend/controller/MojangController.java | 10 ++++++++-- .../backend/controller/PlayerController.java | 11 ++++++----- .../backend/controller/ServerController.java | 17 ++++++++++++----- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/main/java/xyz/mcutils/backend/Main.java b/src/main/java/xyz/mcutils/backend/Main.java index 6d0703f..c543423 100644 --- a/src/main/java/xyz/mcutils/backend/Main.java +++ b/src/main/java/xyz/mcutils/backend/Main.java @@ -22,7 +22,6 @@ public class Main { .setDateFormat("MM-dd-yyyy HH:mm:ss") .create(); public static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient(); - public static final ExecutorService EXECUTOR_POOL = Executors.newFixedThreadPool(8); @SneakyThrows public static void main(String[] args) { diff --git a/src/main/java/xyz/mcutils/backend/controller/MojangController.java b/src/main/java/xyz/mcutils/backend/controller/MojangController.java index 8f4e82f..085e0a0 100644 --- a/src/main/java/xyz/mcutils/backend/controller/MojangController.java +++ b/src/main/java/xyz/mcutils/backend/controller/MojangController.java @@ -2,7 +2,9 @@ package xyz.mcutils.backend.controller; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.CacheControl; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @@ -10,6 +12,8 @@ import org.springframework.web.bind.annotation.RestController; import xyz.mcutils.backend.model.cache.CachedEndpointStatus; import xyz.mcutils.backend.service.MojangService; +import java.util.concurrent.TimeUnit; + @RestController @Tag(name = "Mojang Controller", description = "The Mojang Controller is used to get information about the Mojang APIs.") @RequestMapping(value = "/mojang/", produces = MediaType.APPLICATION_JSON_VALUE) @@ -20,7 +24,9 @@ public class MojangController { @ResponseBody @GetMapping(value = "/status") - public CachedEndpointStatus getStatus() { - return mojangService.getMojangApiStatus(); + public ResponseEntity getStatus() { + return ResponseEntity.ok() + .cacheControl(CacheControl.maxAge(1, TimeUnit.MINUTES)) + .body(mojangService.getMojangApiStatus()); } } diff --git a/src/main/java/xyz/mcutils/backend/controller/PlayerController.java b/src/main/java/xyz/mcutils/backend/controller/PlayerController.java index e9bc75f..e0cc56a 100644 --- a/src/main/java/xyz/mcutils/backend/controller/PlayerController.java +++ b/src/main/java/xyz/mcutils/backend/controller/PlayerController.java @@ -20,7 +20,6 @@ import java.util.concurrent.TimeUnit; @RequestMapping(value = "/player/") public class PlayerController { - private final CacheControl cacheControl = CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic(); private final PlayerService playerService; @Autowired @@ -33,15 +32,17 @@ public class PlayerController { public ResponseEntity getPlayer( @Parameter(description = "The UUID or Username of the player", example = "ImFascinated") @PathVariable String id) { return ResponseEntity.ok() - .cacheControl(cacheControl) + .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS)) .body(playerService.getPlayer(id)); } @ResponseBody @GetMapping(value = "/uuid/{id}", produces = MediaType.APPLICATION_JSON_VALUE) - public CachedPlayerName getPlayerUuid( + public ResponseEntity getPlayerUuid( @Parameter(description = "The UUID or Username of the player", example = "ImFascinated") @PathVariable String id) { - return playerService.usernameToUuid(id); + return ResponseEntity.ok() + .cacheControl(CacheControl.maxAge(6, TimeUnit.HOURS)) + .body(playerService.usernameToUuid(id)); } @GetMapping(value = "/{part}/{id}") @@ -57,7 +58,7 @@ public class PlayerController { // Return the part image return ResponseEntity.ok() - .cacheControl(cacheControl) + .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS)) .contentType(MediaType.IMAGE_PNG) .header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(player.getUsername())) .body(playerService.getSkinPart(player, part, overlays, size).getBytes()); diff --git a/src/main/java/xyz/mcutils/backend/controller/ServerController.java b/src/main/java/xyz/mcutils/backend/controller/ServerController.java index 81d490c..3a171dc 100644 --- a/src/main/java/xyz/mcutils/backend/controller/ServerController.java +++ b/src/main/java/xyz/mcutils/backend/controller/ServerController.java @@ -3,6 +3,7 @@ package xyz.mcutils.backend.controller; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.CacheControl; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -12,6 +13,7 @@ import xyz.mcutils.backend.service.MojangService; import xyz.mcutils.backend.service.ServerService; import java.util.Map; +import java.util.concurrent.TimeUnit; @RestController @Tag(name = "Server Controller", description = "The Server Controller is used to get information about a server.") @@ -29,10 +31,12 @@ public class ServerController { @ResponseBody @GetMapping(value = "/{platform}/{hostname}", produces = MediaType.APPLICATION_JSON_VALUE) - public CachedMinecraftServer getServer( + public ResponseEntity getServer( @Parameter(description = "The platform of the server", example = "java") @PathVariable String platform, @Parameter(description = "The hostname and port of the server", example = "aetheria.cc") @PathVariable String hostname) { - return serverService.getServer(platform, hostname); + return ResponseEntity.ok() + .cacheControl(CacheControl.maxAge(5, TimeUnit.MINUTES)) + .body(serverService.getServer(platform, hostname)); } @ResponseBody @@ -43,6 +47,7 @@ public class ServerController { String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png"; return ResponseEntity.ok() + .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS)) .contentType(MediaType.IMAGE_PNG) .header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(hostname)) .body(serverService.getServerFavicon(hostname)); @@ -52,8 +57,10 @@ public class ServerController { @GetMapping(value = "/blocked/{hostname}", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getServerBlockedStatus( @Parameter(description = "The hostname of the server", example = "aetheria.cc") @PathVariable String hostname) { - return ResponseEntity.ok(Map.of( - "blocked", mojangService.isServerBlocked(hostname) - )); + return ResponseEntity.ok() + .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS)) + .body(Map.of( + "blocked", mojangService.isServerBlocked(hostname) + )); } }