From b3e560d1e22f93f4528ed1cdc5c6fe17b2620c16 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 18 Apr 2024 16:47:48 +0100 Subject: [PATCH] fix the fixy --- .../backend/common/EnvironmentUtils.java | 17 +++++++++++++++++ .../java/xyz/mcutils/backend/config/Config.java | 9 --------- .../mcutils/backend/service/MetricService.java | 3 ++- .../mcutils/backend/service/MojangService.java | 3 ++- .../mcutils/backend/service/PlayerService.java | 11 ++++------- .../mcutils/backend/service/ServerService.java | 3 ++- 6 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 src/main/java/xyz/mcutils/backend/common/EnvironmentUtils.java diff --git a/src/main/java/xyz/mcutils/backend/common/EnvironmentUtils.java b/src/main/java/xyz/mcutils/backend/common/EnvironmentUtils.java new file mode 100644 index 0000000..f1e32b6 --- /dev/null +++ b/src/main/java/xyz/mcutils/backend/common/EnvironmentUtils.java @@ -0,0 +1,17 @@ +package xyz.mcutils.backend.common; + +import lombok.Getter; +import lombok.experimental.UtilityClass; + +@UtilityClass +public final class EnvironmentUtils { + /** + * Is the app running in a production environment? + */ + @Getter + private static final boolean production; + static { // Are we running on production? + String env = System.getenv("ENVIRONMENT"); + production = env != null && (env.equals("production")); + } +} \ No newline at end of file diff --git a/src/main/java/xyz/mcutils/backend/config/Config.java b/src/main/java/xyz/mcutils/backend/config/Config.java index 6657c2e..663d012 100644 --- a/src/main/java/xyz/mcutils/backend/config/Config.java +++ b/src/main/java/xyz/mcutils/backend/config/Config.java @@ -23,18 +23,9 @@ public class Config { @Value("${public-url}") private String webPublicUrl; - /** - * Whether the server is in production mode. - */ - private boolean production = false; - @PostConstruct public void onInitialize() { INSTANCE = this; - - String environmentProperty = environment.getProperty("ENVIRONMENT", "development"); - production = environmentProperty.equalsIgnoreCase("production"); // Set the production mode - log.info("Server is running in {} mode", production ? "production" : "development"); } @Bean diff --git a/src/main/java/xyz/mcutils/backend/service/MetricService.java b/src/main/java/xyz/mcutils/backend/service/MetricService.java index 0411947..3ff1d24 100644 --- a/src/main/java/xyz/mcutils/backend/service/MetricService.java +++ b/src/main/java/xyz/mcutils/backend/service/MetricService.java @@ -6,6 +6,7 @@ import com.influxdb.spring.influx.InfluxDB2AutoConfiguration; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import xyz.mcutils.backend.common.EnvironmentUtils; import xyz.mcutils.backend.common.Timer; import xyz.mcutils.backend.config.Config; import xyz.mcutils.backend.repository.mongo.MetricsRepository; @@ -56,7 +57,7 @@ public class MetricService { collectorEnabled.put(metric, metric.isCollector()); } - if (Config.INSTANCE.isProduction()) { + if (EnvironmentUtils.isProduction()) { // Load the metrics from Redis loadMetrics(); diff --git a/src/main/java/xyz/mcutils/backend/service/MojangService.java b/src/main/java/xyz/mcutils/backend/service/MojangService.java index 2acaac5..cf7d931 100644 --- a/src/main/java/xyz/mcutils/backend/service/MojangService.java +++ b/src/main/java/xyz/mcutils/backend/service/MojangService.java @@ -15,6 +15,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import xyz.mcutils.backend.Main; import xyz.mcutils.backend.common.Endpoint; +import xyz.mcutils.backend.common.EnvironmentUtils; import xyz.mcutils.backend.common.ExpiringSet; import xyz.mcutils.backend.common.WebRequest; import xyz.mcutils.backend.config.Config; @@ -190,7 +191,7 @@ public class MojangService { public CachedEndpointStatus getMojangApiStatus() { log.info("Getting Mojang API status"); Optional endpointStatus = mojangEndpointStatusRepository.findById(MOJANG_ENDPOINT_STATUS_KEY); - if (endpointStatus.isPresent() && Config.INSTANCE.isProduction()) { + if (endpointStatus.isPresent() && EnvironmentUtils.isProduction()) { log.info("Got cached Mojang API status"); return endpointStatus.get(); } diff --git a/src/main/java/xyz/mcutils/backend/service/PlayerService.java b/src/main/java/xyz/mcutils/backend/service/PlayerService.java index d8b7db5..e5dc913 100644 --- a/src/main/java/xyz/mcutils/backend/service/PlayerService.java +++ b/src/main/java/xyz/mcutils/backend/service/PlayerService.java @@ -3,10 +3,7 @@ package xyz.mcutils.backend.service; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import xyz.mcutils.backend.common.ImageUtils; -import xyz.mcutils.backend.common.PlayerUtils; -import xyz.mcutils.backend.common.Tuple; -import xyz.mcutils.backend.common.UUIDUtils; +import xyz.mcutils.backend.common.*; import xyz.mcutils.backend.config.Config; import xyz.mcutils.backend.exception.impl.BadRequestException; import xyz.mcutils.backend.exception.impl.MojangAPIRateLimitException; @@ -67,7 +64,7 @@ public class PlayerService { ((UniquePlayerLookupsMetric) metricService.getMetric(UniquePlayerLookupsMetric.class)).addLookup(id); // Add the lookup to the unique player lookups Optional cachedPlayer = playerCacheRepository.findById(uuid); - if (cachedPlayer.isPresent() && Config.INSTANCE.isProduction()) { // Return the cached player if it exists + if (cachedPlayer.isPresent() && EnvironmentUtils.isProduction()) { // Return the cached player if it exists log.info("Player {} is cached", id); return cachedPlayer.get(); } @@ -107,7 +104,7 @@ public class PlayerService { log.info("Getting UUID from username: {}", username); String id = username.toUpperCase(); Optional cachedPlayerName = playerNameCacheRepository.findById(id); - if (cachedPlayerName.isPresent() && Config.INSTANCE.isProduction()) { + if (cachedPlayerName.isPresent() && EnvironmentUtils.isProduction()) { return cachedPlayerName.get(); } try { @@ -157,7 +154,7 @@ public class PlayerService { Optional cache = playerSkinPartCacheRepository.findById(key); // The skin part is cached - if (cache.isPresent() && Config.INSTANCE.isProduction()) { + if (cache.isPresent() && EnvironmentUtils.isProduction()) { log.info("Skin part {} for player {} is cached", name, player.getUniqueId()); return cache.get(); } diff --git a/src/main/java/xyz/mcutils/backend/service/ServerService.java b/src/main/java/xyz/mcutils/backend/service/ServerService.java index 2c9e767..e5df3a0 100644 --- a/src/main/java/xyz/mcutils/backend/service/ServerService.java +++ b/src/main/java/xyz/mcutils/backend/service/ServerService.java @@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import xyz.mcutils.backend.common.DNSUtils; import xyz.mcutils.backend.common.EnumUtils; +import xyz.mcutils.backend.common.EnvironmentUtils; import xyz.mcutils.backend.config.Config; import xyz.mcutils.backend.exception.impl.BadRequestException; import xyz.mcutils.backend.exception.impl.ResourceNotFoundException; @@ -69,7 +70,7 @@ public class ServerService { // Check if the server is cached Optional cached = serverCacheRepository.findById(key); - if (cached.isPresent() && Config.INSTANCE.isProduction()) { + if (cached.isPresent() && EnvironmentUtils.isProduction()) { log.info("Server {}:{} is cached", hostname, port); return cached.get(); }