add cache control to endpoints

This commit is contained in:
Lee 2024-04-19 20:33:12 +01:00
parent 8a8c6b542a
commit 4dc263961d
4 changed files with 26 additions and 13 deletions

@ -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) {

@ -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<CachedEndpointStatus> getStatus() {
return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(1, TimeUnit.MINUTES))
.body(mojangService.getMojangApiStatus());
}
}

@ -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<CachedPlayerName> 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());

@ -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<CachedMinecraftServer> 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,7 +57,9 @@ 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(
return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS))
.body(Map.of(
"blocked", mojangService.isServerBlocked(hostname)
));
}