Files
Backend/src/main/java/cc.fascinated/controller/ServerController.java

48 lines
2.4 KiB
Java
Raw Normal View History

2024-04-10 07:43:38 +01:00
package cc.fascinated.controller;
2024-04-10 09:19:02 +01:00
import cc.fascinated.common.ServerUtils;
import cc.fascinated.common.Tuple;
2024-04-10 07:43:38 +01:00
import cc.fascinated.model.cache.CachedMinecraftServer;
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.*;
@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 {
@Autowired
private ServerService serverService;
@ResponseBody
@GetMapping(value = "/{platform}/{hostnameAndPort}", 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,
@Parameter(description = "The hostname and port of the server", example = "play.hypixel.net") @PathVariable String hostnameAndPort) {
2024-04-10 09:19:02 +01:00
Tuple<String, Integer> host = ServerUtils.getHostnameAndPort(hostnameAndPort);
return serverService.getServer(platform, host.getLeft(), host.getRight());
}
@ResponseBody
2024-04-10 11:09:39 +01:00
@GetMapping(value = "/icon/{hostnameAndPort}", produces = MediaType.IMAGE_PNG_VALUE)
2024-04-10 10:26:24 +01:00
public ResponseEntity<?> getServerIcon(
2024-04-10 11:09:09 +01:00
@Parameter(description = "The hostname and port of the server", example = "play.hypixel.net") @PathVariable String hostnameAndPort,
@Parameter(description = "Whether to download the image") @RequestParam(required = false, defaultValue = "false") boolean download) {
2024-04-10 09:19:02 +01:00
Tuple<String, Integer> host = ServerUtils.getHostnameAndPort(hostnameAndPort);
String hostname = host.getLeft();
int port = host.getRight();
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)
.header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(ServerUtils.getAddress(hostname, port)))
2024-04-10 09:19:02 +01:00
.body(serverService.getServerFavicon(hostname, port));
2024-04-10 07:43:38 +01:00
}
}