Skip to content

Commit

Permalink
Add server-side config
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Aug 20, 2023
1 parent d341a4b commit b94ca09
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
34 changes: 33 additions & 1 deletion common/src/main/java/cn/zbx1425/worldcomment/Config.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
package cn.zbx1425.worldcomment;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Config {


public String redisUrl = "";

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

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

JsonObject json = JsonParser.parseString(Files.readString(configPath)).getAsJsonObject();
redisUrl = json.get("redisUrl").getAsString();
uplinkUrl = json.get("uplinkUrl").getAsString();
uplinkAuthKey = json.get("uplinkAuthKey").getAsString();
}

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

Files.writeString(configPath, new GsonBuilder().setPrettyPrinting().create().toJson(json.toString()));
}
}
10 changes: 8 additions & 2 deletions common/src/main/java/cn/zbx1425/worldcomment/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class Main {

public static ServerWorldData DATABASE;

public static Config CONFIG = new Config();

public static final RegistryObject<Item> ITEM_COMMENT_TOOL = new RegistryObject<>(CommentToolItem::new);

public static void init(RegistriesWrapper registries) {
Expand All @@ -43,9 +45,13 @@ public static void init(RegistriesWrapper registries) {

ServerPlatform.registerServerStartingEvent(server -> {
try {
//Todo: config inject here
CONFIG.load(server.getServerDirectory().toPath()
.resolve("config").resolve("world-comment.json"));

DATABASE = new ServerWorldData(server);
DATABASE.peerChannel = new RedisSynchronizer("redis://192.168.1.148:6379/0", DATABASE);
if (!CONFIG.redisUrl.isEmpty()) {
DATABASE.peerChannel = new RedisSynchronizer(CONFIG.redisUrl, DATABASE);
}
DATABASE.load();
} catch (IOException e) {
LOGGER.error("Failed to open data storage", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import cn.zbx1425.worldcomment.data.CommentEntry;
import it.unimi.dsi.fastutil.longs.Long2ObjectSortedMap;

//Todo: doing nothing currently
public class NoopSynchronizer implements Synchronizer {

public NoopSynchronizer() {
Expand Down

0 comments on commit b94ca09

Please sign in to comment.