Add database client as common for other components.

This commit is contained in:
Alessandro Proto 2024-08-05 22:52:34 +02:00
parent 5e9485eb3e
commit 2fb98ef64c
6 changed files with 73 additions and 25 deletions

View file

@ -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")
}
}
}
}

View file

@ -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
owo_version=0.12.11+1.21

View file

@ -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() {

View file

@ -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";
}

View file

@ -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();
}
}

View file

@ -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();
}