2024-04-10 07:43:38 +01:00
|
|
|
package cc.fascinated.controller;
|
|
|
|
|
|
|
|
import cc.fascinated.model.cache.CachedMinecraftServer;
|
2024-04-10 11:39:17 +01:00
|
|
|
import cc.fascinated.service.MojangService;
|
2024-04-10 07:43:38 +01:00
|
|
|
import cc.fascinated.service.ServerService;
|
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-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-10 11:39:17 +01:00
|
|
|
import java.util.Map;
|
|
|
|
|
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 {
|
|
|
|
|
2024-04-10 11:39:17 +01:00
|
|
|
private final ServerService serverService;
|
|
|
|
private final MojangService mojangService;
|
|
|
|
|
2024-04-10 07:43:38 +01:00
|
|
|
@Autowired
|
2024-04-10 11:39:17 +01:00
|
|
|
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-10 10:26:24 +01:00
|
|
|
public CachedMinecraftServer getServer(
|
2024-04-10 11:09:09 +01:00
|
|
|
@Parameter(description = "The platform of the server", example = "java") @PathVariable String platform,
|
2024-04-10 12:08:12 +01:00
|
|
|
@Parameter(description = "The hostname and port of the server", example = "play.hypixel.net") @PathVariable String hostname) {
|
2024-04-10 12:26:47 +01:00
|
|
|
return serverService.getServer(platform, hostname);
|
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(
|
2024-04-10 12:08:12 +01:00
|
|
|
@Parameter(description = "The hostname and port of the server", example = "play.hypixel.net") @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) {
|
2024-04-10 10:45:17 +01:00
|
|
|
String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png";
|
|
|
|
|
2024-04-10 09:19:02 +01:00
|
|
|
return ResponseEntity.ok()
|
|
|
|
.contentType(MediaType.IMAGE_PNG)
|
2024-04-10 12:26:47 +01:00
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(hostname))
|
|
|
|
.body(serverService.getServerFavicon(hostname));
|
2024-04-10 07:43:38 +01:00
|
|
|
}
|
2024-04-10 11:39:17 +01:00
|
|
|
|
|
|
|
@ResponseBody
|
|
|
|
@GetMapping(value = "/blocked/{hostname}", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
public ResponseEntity<?> getServerBlockedStatus(
|
|
|
|
@Parameter(description = "The hostname of the server", example = "play.hypixel.net") @PathVariable String hostname) {
|
|
|
|
return ResponseEntity.ok(Map.of(
|
|
|
|
"banned", mojangService.isServerBlocked(hostname)
|
|
|
|
));
|
|
|
|
}
|
2024-04-10 07:43:38 +01:00
|
|
|
}
|