package app.controllers.server; import app.annotations.enums.AuthMethod; import app.annotations.interfaces.CheckWebAccess; import app.entities.Stats; import app.entities.server.request.ServerRequestBody; import app.websocket.handlers.ServersHandler; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("api/server") public class ServerUpdaterController { private Stats stats; private ServersHandler serversHandler; @Autowired public ServerUpdaterController(Stats stats, ServersHandler serversHandler) { this.stats = stats; this.serversHandler = serversHandler; } @PostMapping(value = "/{srv}") @CheckWebAccess(auth_method = AuthMethod.SECRET_KEY) public ResponseEntity updateServer(HttpServletRequest request, @PathVariable String srv, @RequestBody ServerRequestBody serverRequestBody) { if (!stats.getServers().containsKey(srv)) return new ResponseEntity<>(HttpStatus.NOT_FOUND); stats.getServers().get(srv).RefreshServerFromRequest(serverRequestBody); serversHandler.pushServer(srv, stats.getServers().get(srv)); return new ResponseEntity(HttpStatus.OK); } @DeleteMapping(value = "/{srv}") @CheckWebAccess(auth_method = AuthMethod.SECRET_KEY) public ResponseEntity downServer(HttpServletRequest request, @PathVariable String srv) { if (!stats.getServers().containsKey(srv)) return new ResponseEntity<>(HttpStatus.NOT_FOUND); stats.getServers().get(srv).RefreshServerFromRequest(null); serversHandler.pushServer(srv, stats.getServers().get(srv)); return new ResponseEntity(HttpStatus.OK); } }