Files
Backend/src/main/java/xyz/mcutils/backend/controller/ServerController.java

74 lines
3.4 KiB
Java
Raw Normal View History

2024-04-13 20:53:31 +01:00
package xyz.mcutils.backend.controller;
2024-04-10 07:43:38 +01:00
2024-04-10 10:26:24 +01:00
import io.swagger.v3.oas.annotations.Parameter;
2024-04-10 09:51:31 +01:00
import io.swagger.v3.oas.annotations.tags.Tag;
2024-04-10 07:43:38 +01:00
import org.springframework.beans.factory.annotation.Autowired;
2024-04-19 20:33:12 +01:00
import org.springframework.http.CacheControl;
2024-04-10 09:19:02 +01:00
import org.springframework.http.HttpHeaders;
2024-04-10 07:43:38 +01:00
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
2024-04-13 20:53:31 +01:00
import xyz.mcutils.backend.model.cache.CachedMinecraftServer;
import xyz.mcutils.backend.service.MojangService;
import xyz.mcutils.backend.service.ServerService;
2024-04-10 07:43:38 +01:00
2024-04-19 20:46:30 +01:00
import java.util.Arrays;
import java.util.Map;
2024-04-19 20:33:12 +01:00
import java.util.concurrent.TimeUnit;
2024-04-10 07:43:38 +01:00
@RestController
2024-04-10 09:51:31 +01:00
@Tag(name = "Server Controller", description = "The Server Controller is used to get information about a server.")
2024-04-10 07:43:38 +01:00
@RequestMapping(value = "/server/")
public class ServerController {
private final ServerService serverService;
private final MojangService mojangService;
2024-04-10 07:43:38 +01:00
@Autowired
public ServerController(ServerService serverService, MojangService mojangService) {
this.serverService = serverService;
this.mojangService = mojangService;
}
2024-04-10 07:43:38 +01:00
@ResponseBody
2024-04-10 12:08:12 +01:00
@GetMapping(value = "/{platform}/{hostname}", produces = MediaType.APPLICATION_JSON_VALUE)
2024-04-19 20:33:12 +01:00
public ResponseEntity<CachedMinecraftServer> getServer(
2024-04-10 11:09:09 +01:00
@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) {
2024-04-19 20:46:30 +01:00
CachedMinecraftServer server = serverService.getServer(platform, hostname);
2024-04-19 20:33:12 +01:00
return ResponseEntity.ok()
2024-04-19 20:51:33 +01:00
.cacheControl(CacheControl.maxAge(5, TimeUnit.MINUTES).cachePublic())
2024-04-19 20:46:30 +01:00
.eTag(String.valueOf(server.hashCode()))
.body(server);
2024-04-10 09:19:02 +01:00
}
@ResponseBody
2024-04-10 12:08:12 +01:00
@GetMapping(value = "/icon/{hostname}", produces = MediaType.IMAGE_PNG_VALUE)
2024-04-10 10:26:24 +01:00
public ResponseEntity<?> getServerIcon(
@Parameter(description = "The hostname and port of the server", example = "aetheria.cc") @PathVariable String hostname,
2024-04-10 11:09:09 +01:00
@Parameter(description = "Whether to download the image") @RequestParam(required = false, defaultValue = "false") boolean download) {
String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png";
2024-04-19 20:46:30 +01:00
byte[] favicon = serverService.getServerFavicon(hostname);
2024-04-10 09:19:02 +01:00
return ResponseEntity.ok()
2024-04-19 20:51:33 +01:00
.cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic())
2024-04-10 09:19:02 +01:00
.contentType(MediaType.IMAGE_PNG)
.header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(hostname))
2024-04-19 20:46:30 +01:00
.eTag(String.valueOf(Arrays.hashCode(favicon)))
.body(favicon);
2024-04-10 07:43:38 +01:00
}
@ResponseBody
@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) {
2024-04-19 20:33:12 +01:00
return ResponseEntity.ok()
2024-04-19 20:51:33 +01:00
.cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic())
2024-04-19 20:46:30 +01:00
.eTag(String.valueOf(hostname.hashCode()))
2024-04-19 20:33:12 +01:00
.body(Map.of(
"blocked", mojangService.isServerBlocked(hostname)
));
}
2024-04-10 07:43:38 +01:00
}