From 2fb98ef64c91cf535d663c4bb89c94a1519a442e Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Mon, 5 Aug 2024 22:52:34 +0200 Subject: [PATCH] Add database client as common for other components. --- build.gradle | 12 +++++ gradle.properties | 6 +-- src/main/java/ct/server/CtServer.java | 44 ++++++++++++------- .../java/ct/server/CtServerConfigModel.java | 1 + .../ct/server/database/DatabaseClient.java | 21 +++++++++ .../ServiceServer.java} | 14 +++--- 6 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 src/main/java/ct/server/database/DatabaseClient.java rename src/main/java/ct/server/{ServiceHttpServer.java => http/ServiceServer.java} (85%) diff --git a/build.gradle b/build.gradle index e75a268..06915f1 100644 --- a/build.gradle +++ b/build.gradle @@ -42,6 +42,7 @@ dependencies { modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" annotationProcessor modImplementation("io.wispforest:owo-lib:${project.owo_version}") + include "io.wispforest:owo-sentinel:${project.owo_version}" } @@ -88,5 +89,16 @@ publishing { // Notice: This block does NOT have the same function as the block in the top level. // The repositories here will be used for publishing your artifact, not for // retrieving dependencies. + + + // TODO: change to CT server once it's all ready + maven { + name = "AlexDevs" + url = "https://maven.alexdevs.me/releases" + credentials { + username = System.getenv("MAVEN_USERNAME") + password = System.getenv("MAVEN_PASSWORD") + } + } } } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 20eaf89..ff5832e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,11 +9,11 @@ yarn_mappings=1.21+build.9 loader_version=0.15.11 # Mod Properties -mod_version=1.2 -maven_group=ct.server +mod_version=1.3 +maven_group=ct archives_base_name=ct-server # Dependencies fabric_version=0.100.8+1.21 -owo_version=0.12.8-alpha.7+1.21 \ No newline at end of file +owo_version=0.12.11+1.21 \ No newline at end of file diff --git a/src/main/java/ct/server/CtServer.java b/src/main/java/ct/server/CtServer.java index cd84ce1..3f83276 100644 --- a/src/main/java/ct/server/CtServer.java +++ b/src/main/java/ct/server/CtServer.java @@ -1,23 +1,18 @@ package ct.server; +import ct.server.database.DatabaseClient; +import ct.server.http.ServiceServer; import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; -import net.fabricmc.fabric.api.networking.v1.ServerLoginConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; -import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; -import java.io.OutputStream; -import java.net.*; -import java.nio.charset.StandardCharsets; -import java.time.Instant; -import java.util.concurrent.CompletableFuture; +import java.sql.SQLException; public class CtServer implements ModInitializer { @@ -30,17 +25,38 @@ public class CtServer implements ModInitializer { private static float currentMspt = 0; private static int currentPlayerCount = 0; - private static CtServer INSTANCE; - public static CtServer getInstance() { return INSTANCE; } + private static CtServer INSTANCE; + + private ServiceServer serviceServer; + public ServiceServer serviceServer() { + return serviceServer; + } + + private DatabaseClient database; + public DatabaseClient database() { + return database; + } @Override public void onInitialize() { INSTANCE = this; - LOGGER.info("Starting ct-client"); + LOGGER.info("Starting ct-server"); + + try { + database = new DatabaseClient(); + } catch(SQLException e) { + LOGGER.error("Could not connect to the database", e); + } + + try { + serviceServer = new ServiceServer(); + } catch (IOException e) { + LOGGER.error("Unable to start HTTP server", e); + } ServerTickEvents.END_SERVER_TICK.register(server -> { currentMspt = server.getAverageTickTime(); @@ -58,12 +74,6 @@ public class CtServer implements ModInitializer { ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> { currentPlayerCount = server.getCurrentPlayerCount() - 1; }); - - try { - var httpServer = new ServiceHttpServer(); - } catch (IOException e) { - LOGGER.error("Unable to start HTTP server", e); - } } public static float getTPS() { diff --git a/src/main/java/ct/server/CtServerConfigModel.java b/src/main/java/ct/server/CtServerConfigModel.java index 37ca471..f2c9b52 100644 --- a/src/main/java/ct/server/CtServerConfigModel.java +++ b/src/main/java/ct/server/CtServerConfigModel.java @@ -5,4 +5,5 @@ import io.wispforest.owo.config.annotation.Config; @Config(name = "ct-server-config", wrapperName = "CtServerConfig") public class CtServerConfigModel { public short httpPort = 25581; + public String databaseUrl = "jdbc:postgresql://127.0.0.1:5432/ct?user=myuser&password=mypassword"; } diff --git a/src/main/java/ct/server/database/DatabaseClient.java b/src/main/java/ct/server/database/DatabaseClient.java new file mode 100644 index 0000000..9d9e0fc --- /dev/null +++ b/src/main/java/ct/server/database/DatabaseClient.java @@ -0,0 +1,21 @@ +package ct.server.database; + +import ct.server.CtServer; + +import java.sql.*; + +public class DatabaseClient { + private Connection connection; + public Connection connection() throws SQLException { + if (connection == null || connection.isClosed()) { + connection = DriverManager.getConnection(CtServer.CONFIG.databaseUrl()); + } + return connection; + } + + // Prepare a connection to the DB ready for use + // TODO: Consider creating a pool + public DatabaseClient() throws SQLException { + connection(); + } +} diff --git a/src/main/java/ct/server/ServiceHttpServer.java b/src/main/java/ct/server/http/ServiceServer.java similarity index 85% rename from src/main/java/ct/server/ServiceHttpServer.java rename to src/main/java/ct/server/http/ServiceServer.java index 513ba0f..590cd8e 100644 --- a/src/main/java/ct/server/ServiceHttpServer.java +++ b/src/main/java/ct/server/http/ServiceServer.java @@ -1,23 +1,27 @@ -package ct.server; +package ct.server.http; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; +import ct.server.CtServer; import java.io.IOException; import java.net.InetSocketAddress; -public class ServiceHttpServer { - private HttpServer server; +public class ServiceServer { + private final HttpServer server; + public HttpServer httpServer() { + return this.server; + } - public ServiceHttpServer() throws IOException { + public ServiceServer() throws IOException { server = HttpServer.create(new InetSocketAddress(CtServer.CONFIG.httpPort()), 0); server.createContext("/tps", new TPSHandler()); server.createContext("/mspt", new MSPTHandler()); server.createContext("/player", new PlayerCountHandler()); server.setExecutor(null); - var httpThread = new Thread(() -> server.start()); + var httpThread = new Thread(server::start); httpThread.start(); }