add cache control to endpoints
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 2m0s

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") .setDateFormat("MM-dd-yyyy HH:mm:ss")
.create(); .create();
public static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient(); public static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient();
public static final ExecutorService EXECUTOR_POOL = Executors.newFixedThreadPool(8);
@SneakyThrows @SneakyThrows
public static void main(String[] args) { public static void main(String[] args) {

@ -2,7 +2,9 @@ package xyz.mcutils.backend.controller;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; 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.model.cache.CachedEndpointStatus;
import xyz.mcutils.backend.service.MojangService; import xyz.mcutils.backend.service.MojangService;
import java.util.concurrent.TimeUnit;
@RestController @RestController
@Tag(name = "Mojang Controller", description = "The Mojang Controller is used to get information about the Mojang APIs.") @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) @RequestMapping(value = "/mojang/", produces = MediaType.APPLICATION_JSON_VALUE)
@ -20,7 +24,9 @@ public class MojangController {
@ResponseBody @ResponseBody
@GetMapping(value = "/status") @GetMapping(value = "/status")
public CachedEndpointStatus getStatus() { public ResponseEntity<CachedEndpointStatus> getStatus() {
return mojangService.getMojangApiStatus(); return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(1, TimeUnit.MINUTES))
.body(mojangService.getMojangApiStatus());
} }
} }

@ -20,7 +20,6 @@ import java.util.concurrent.TimeUnit;
@RequestMapping(value = "/player/") @RequestMapping(value = "/player/")
public class PlayerController { public class PlayerController {
private final CacheControl cacheControl = CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic();
private final PlayerService playerService; private final PlayerService playerService;
@Autowired @Autowired
@ -33,15 +32,17 @@ public class PlayerController {
public ResponseEntity<?> getPlayer( public ResponseEntity<?> getPlayer(
@Parameter(description = "The UUID or Username of the player", example = "ImFascinated") @PathVariable String id) { @Parameter(description = "The UUID or Username of the player", example = "ImFascinated") @PathVariable String id) {
return ResponseEntity.ok() return ResponseEntity.ok()
.cacheControl(cacheControl) .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS))
.body(playerService.getPlayer(id)); .body(playerService.getPlayer(id));
} }
@ResponseBody @ResponseBody
@GetMapping(value = "/uuid/{id}", produces = MediaType.APPLICATION_JSON_VALUE) @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) { @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}") @GetMapping(value = "/{part}/{id}")
@ -57,7 +58,7 @@ public class PlayerController {
// Return the part image // Return the part image
return ResponseEntity.ok() return ResponseEntity.ok()
.cacheControl(cacheControl) .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS))
.contentType(MediaType.IMAGE_PNG) .contentType(MediaType.IMAGE_PNG)
.header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(player.getUsername())) .header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(player.getUsername()))
.body(playerService.getSkinPart(player, part, overlays, size).getBytes()); .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.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -12,6 +13,7 @@ import xyz.mcutils.backend.service.MojangService;
import xyz.mcutils.backend.service.ServerService; import xyz.mcutils.backend.service.ServerService;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
@RestController @RestController
@Tag(name = "Server Controller", description = "The Server Controller is used to get information about a server.") @Tag(name = "Server Controller", description = "The Server Controller is used to get information about a server.")
@ -29,10 +31,12 @@ public class ServerController {
@ResponseBody @ResponseBody
@GetMapping(value = "/{platform}/{hostname}", produces = MediaType.APPLICATION_JSON_VALUE) @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 platform of the server", example = "java") @PathVariable String platform,
@Parameter(description = "The hostname and port of the server", example = "aetheria.cc") @PathVariable String hostname) { @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 @ResponseBody
@ -43,6 +47,7 @@ public class ServerController {
String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png"; String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png";
return ResponseEntity.ok() return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS))
.contentType(MediaType.IMAGE_PNG) .contentType(MediaType.IMAGE_PNG)
.header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(hostname)) .header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(hostname))
.body(serverService.getServerFavicon(hostname)); .body(serverService.getServerFavicon(hostname));
@ -52,8 +57,10 @@ public class ServerController {
@GetMapping(value = "/blocked/{hostname}", produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(value = "/blocked/{hostname}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> getServerBlockedStatus( public ResponseEntity<?> getServerBlockedStatus(
@Parameter(description = "The hostname of the server", example = "aetheria.cc") @PathVariable String hostname) { @Parameter(description = "The hostname of the server", example = "aetheria.cc") @PathVariable String hostname) {
return ResponseEntity.ok(Map.of( return ResponseEntity.ok()
"blocked", mojangService.isServerBlocked(hostname) .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS))
)); .body(Map.of(
"blocked", mojangService.isServerBlocked(hostname)
));
} }
} }