From 805d09044c995f68edab0a442dc49c6938f0d80d Mon Sep 17 00:00:00 2001 From: gsd Date: Sat, 25 May 2024 23:00:11 +0300 Subject: [PATCH] exit is config --- .../java/app/controllers/StatsController.java | 18 +++++++++++++++--- .../app/services/io/readers/ServersReader.java | 12 ++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/app/controllers/StatsController.java b/src/main/java/app/controllers/StatsController.java index 0315272..17181fb 100644 --- a/src/main/java/app/controllers/StatsController.java +++ b/src/main/java/app/controllers/StatsController.java @@ -1,11 +1,12 @@ package app.controllers; import app.annotations.enums.CollectStages; -import app.annotations.interfaces.BurstUpdatePlayers; -import app.annotations.interfaces.CollectStatistic; +import app.annotations.interfaces.*; import app.entities.Stats; import app.services.ExternalServices; +import app.services.io.readers.ServersReader; import app.updates.OnlineUpdater; +import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -25,12 +26,14 @@ public class StatsController { private ExternalServices externalServices; private OnlineUpdater onlineUpdater; private HashMap, Long>> cache = new HashMap<>(); + private ServersReader serversReader; @Autowired - public StatsController(Stats stats, ExternalServices externalServices, OnlineUpdater onlineUpdater){ + public StatsController(Stats stats, ExternalServices externalServices, OnlineUpdater onlineUpdater, ServersReader serversReader){ this.stats = stats; this.externalServices = externalServices; this.onlineUpdater = onlineUpdater; + this.serversReader = serversReader; } @GetMapping @@ -61,4 +64,13 @@ public class StatsController { return new ResponseEntity<>(cache.get(limit).keySet().stream().findFirst().orElse(null), HttpStatus.OK); } + + @GetMapping("/reload") + @CheckWebAccess + @CheckPermitionFlag(flag = "z") + @WaitAfterNext(order = "reload_config") + @CollectStatistic(stage = CollectStages.COMBINED) + public ResponseEntity reloadConfig(HttpServletRequest request) { + return new ResponseEntity<>(serversReader.call(false), HttpStatus.OK); + } } diff --git a/src/main/java/app/services/io/readers/ServersReader.java b/src/main/java/app/services/io/readers/ServersReader.java index 52bff5b..3f39a37 100644 --- a/src/main/java/app/services/io/readers/ServersReader.java +++ b/src/main/java/app/services/io/readers/ServersReader.java @@ -20,16 +20,24 @@ public class ServersReader extends BaseReader { public ServersReader(Stats stats, List fileLoaders) { super(fileLoaders); this.stats = stats; + call(true); + } + + public boolean call(boolean exit) { try { + logger.info("Try load base server configuration"); Iterator> iterator = loadConfiguration().fields(); while (iterator.hasNext()) { Map.Entry server = iterator.next(); stats.getServers().put(server.getKey(), objectMapper.treeToValue(server.getValue(), Server.class)); logger.info("{}\n{}",server.getKey() ,stats.getServers().get(server.getKey())); } + return true; } catch (IOException err) { - logger.error("Cannot parse configuration", err); - System.exit(1); + logger.error("Cannot parse configuration, exit enabled: " + (exit?"yes":"no"), err); + if (exit) + System.exit(1); + return false; } }