Skip to content

Commit

Permalink
Add acquiring config from environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Sep 17, 2023
1 parent f96f7a1 commit 2e093ec
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
53 changes: 41 additions & 12 deletions common/src/main/java/cn/zbx1425/worldcomment/Config.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.zbx1425.worldcomment;

import com.google.common.base.CaseFormat;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
Expand All @@ -10,29 +11,57 @@

public class Config {

public String redisUrl = "";
public boolean syncIsHost = true;
public ConfigItem redisUrl;
public ConfigItem syncRole;

public String uplinkUrl = "";
public String uplinkAuthKey = "";
public ConfigItem uplinkUrl;
public ConfigItem uplinkAuthKey;

public void load(Path configPath) throws IOException {
if (!Files.exists(configPath)) save(configPath);

JsonObject json = JsonParser.parseString(Files.readString(configPath)).getAsJsonObject();
redisUrl = json.get("redisUrl").getAsString();
syncIsHost = json.get("syncIsHost").getAsBoolean();
uplinkUrl = json.get("uplinkUrl").getAsString();
uplinkAuthKey = json.get("uplinkAuthKey").getAsString();
redisUrl = new ConfigItem(json, "redisUrl", "");
syncRole = new ConfigItem(json, "redisSyncRole", "host");
uplinkUrl = new ConfigItem(json, "uplinkUrl", "");
uplinkAuthKey = new ConfigItem(json, "uplinkAuthKey", "");
}

public void save(Path configPath) throws IOException {
JsonObject json = new JsonObject();
json.addProperty("redisUrl", redisUrl);
json.addProperty("syncIsHost", syncIsHost);
json.addProperty("uplinkUrl", uplinkUrl);
json.addProperty("uplinkAuthKey", uplinkAuthKey);
redisUrl.writeJson(json);
syncRole.writeJson(json);
uplinkUrl.writeJson(json);
uplinkAuthKey.writeJson(json);

Files.writeString(configPath, new GsonBuilder().setPrettyPrinting().create().toJson(json));
}

public static class ConfigItem {

private final String camelKey;
public String value;
public boolean isFromJson;

public ConfigItem(JsonObject jsonObject, String camelKey, String defaultValue) {
String snakeKey = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, camelKey);
this.camelKey = camelKey;
if (System.getenv("SUBNOTEICA_" + snakeKey) != null) {
this.value = System.getenv("SUBNOTEICA_" + snakeKey);
this.isFromJson = false;
} else if (jsonObject.has(camelKey)) {
this.value = jsonObject.get(camelKey).getAsString();
this.isFromJson = true;
} else {
this.value = defaultValue;
this.isFromJson = false;
}
}

public void writeJson(JsonObject jsonObject) {
if (isFromJson) {
jsonObject.addProperty(camelKey, value);
}
}
}
}
6 changes: 3 additions & 3 deletions common/src/main/java/cn/zbx1425/worldcomment/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public static void init(RegistriesWrapper registries) {
CONFIG.load(server.getServerDirectory().toPath()
.resolve("config").resolve("world-comment.json"));

DATABASE = new ServerWorldData(server, CONFIG.syncIsHost);
if (!CONFIG.redisUrl.isEmpty()) {
DATABASE.peerChannel = new RedisSynchronizer(CONFIG.redisUrl, DATABASE);
DATABASE = new ServerWorldData(server, CONFIG.syncRole.value.equalsIgnoreCase("host"));
if (!CONFIG.redisUrl.value.isEmpty()) {
DATABASE.peerChannel = new RedisSynchronizer(CONFIG.redisUrl.value, DATABASE);
}
DATABASE.load();
} catch (IOException e) {
Expand Down

0 comments on commit 2e093ec

Please sign in to comment.