Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ object Files {
const val dockerizedServices = "cloudnet-dockerized-services.jar"
const val databaseMongo = "cloudnet-database-mongodb.jar"
const val databaseMysql = "cloudnet-database-mysql.jar"
const val databasePostgres = "cloudnet-database-postgres.jar"
const val labymod = "cloudnet-labymod.jar"
const val npcs = "cloudnet-npcs.jar"
const val rest = "cloudnet-rest.jar"
Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ xodus = "2.0.1"
mongodb = "5.6.1"
hikariCp = "7.0.2"
mysqlConnector = "9.5.0"
postgresql = "42.7.8"

# general
oshi = "6.9.1"
Expand Down Expand Up @@ -162,6 +163,7 @@ hikariCp = { group = "com.zaxxer", name = "HikariCP", version.ref = "hikariCp" }
mongodb = { group = "org.mongodb", name = "mongodb-driver-sync", version.ref = "mongodb" }
xodus = { group = "org.jetbrains.xodus", name = "xodus-environment", version.ref = "xodus" }
mysqlConnector = { group = "com.mysql", name = "mysql-connector-j", version.ref = "mysqlConnector" }
postgresql = { group = "org.postgresql", name = "postgresql", version.ref = "postgresql" }

# platform api
nukkitX = { group = "cn.nukkit", name = "nukkit", version.ref = "nukkitX" }
Expand Down Expand Up @@ -202,6 +204,7 @@ aerogelApi = ["aerogel", "aerogelAuto"]
npcLib = ["npcLib", "npcLibLabymod"]
unirest = ["unirest", "unirestGson"]
mysql = ["mysqlConnector", "hikariCp"]
postgresql = ["postgresql", "hikariCp"]
jline = ["jlineReader", "jlineTerminal"]
cloud = ["cloudCore", "cloudAnnotations", "cloudConfirmationProcessor"]
cloudApi = ["cloudCoreApi", "cloudAnnotationsApi", "cloudConfirmationProcessor"]
Expand Down
24 changes: 24 additions & 0 deletions modules/database-postgres/api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2019-2025 CloudNetService team & contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
id("cloudnet-publish")
id("cloudnet-modules-api")
}

dependencies {
compileOnlyApi(projects.driver.driverApi)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019-present CloudNetService team & contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eu.cloudnetservice.modules.postgres.config;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import lombok.NonNull;

public record PostgresConfiguration(
@NonNull String username,
@NonNull String password,
@NonNull String databaseServiceName,
@NonNull List<PostgresConnectionEndpoint> endpoints
) {

public @NonNull PostgresConnectionEndpoint randomEndpoint() {
if (this.endpoints.isEmpty()) {
throw new IllegalStateException("No postgres connection endpoints available");
}
return this.endpoints.get(ThreadLocalRandom.current().nextInt(0, this.endpoints.size()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019-present CloudNetService team & contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eu.cloudnetservice.modules.postgres.config;

import eu.cloudnetservice.driver.network.HostAndPort;
import lombok.NonNull;

public record PostgresConnectionEndpoint(@NonNull String database, @NonNull HostAndPort address) {
}
45 changes: 45 additions & 0 deletions modules/database-postgres/impl/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2019-2025 CloudNetService team & contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import eu.cloudnetservice.cloudnet.gradle.util.Files

plugins {
id("cloudnet-modules")
id("cloudnet-publish")
alias(libs.plugins.shadow)
}

dependencies {
moduleLibrary(libs.bundles.postgresql)

compileOnly(libs.caffeine)
compileOnlyApi(projects.node.nodeImpl)
api(projects.modules.databasePostgres.databasePostgresApi)
}

tasks.shadowJar.configure {
archiveFileName = Files.databasePostgres
}

moduleJson {
author = "CloudNetService"
name = "CloudNet-Database-PostgreSQL"
main = "eu.cloudnetservice.modules.postgres.impl.CloudNetPostgresDatabaseModule"
description = "CloudNet extension, which includes the database support for PostgreSQL"
minJavaVersionId = JavaVersion.VERSION_11
runtimeModule = true
storesSensitiveData = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2019-present CloudNetService team & contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eu.cloudnetservice.modules.postgres.impl;

import eu.cloudnetservice.driver.document.Document;
import eu.cloudnetservice.driver.document.DocumentFactory;
import eu.cloudnetservice.driver.module.ModuleLifeCycle;
import eu.cloudnetservice.driver.module.ModuleTask;
import eu.cloudnetservice.driver.module.driver.DriverModule;
import eu.cloudnetservice.driver.network.HostAndPort;
import eu.cloudnetservice.driver.registry.ServiceRegistry;
import eu.cloudnetservice.modules.postgres.config.PostgresConfiguration;
import eu.cloudnetservice.modules.postgres.config.PostgresConnectionEndpoint;
import eu.cloudnetservice.node.impl.database.NodeDatabaseProvider;
import io.leangen.geantyref.TypeFactory;
import jakarta.inject.Singleton;
import java.util.List;
import lombok.NonNull;

@Singleton
public final class CloudNetPostgresDatabaseModule extends DriverModule {

private volatile PostgresConfiguration configuration;

@ModuleTask(order = 127, lifecycle = ModuleLifeCycle.LOADED)
public void convertConfig() {
var config = this.readConfig(DocumentFactory.json());
if (config.contains("addresses")) {
this.writeConfig(Document.newJsonDocument().appendTree(new PostgresConfiguration(
config.getString("username"),
config.getString("password"),
config.getString("database"),
config.readObject("addresses", TypeFactory.parameterizedClass(List.class, PostgresConnectionEndpoint.class))
)));
}
}

@ModuleTask(order = 125, lifecycle = ModuleLifeCycle.LOADED)
public void registerDatabaseProvider(@NonNull ServiceRegistry serviceRegistry) {
this.configuration = this.readConfig(
PostgresConfiguration.class,
() -> new PostgresConfiguration(
"postgres",
"postgres",
"postgres",
List.of(new PostgresConnectionEndpoint("cloudnet", new HostAndPort("127.0.0.1", 5432)))),
DocumentFactory.json());

serviceRegistry.registerProvider(
NodeDatabaseProvider.class,
this.configuration.databaseServiceName(),
new PostgresDatabaseProvider(this.configuration));
}

@ModuleTask(order = 127, lifecycle = ModuleLifeCycle.STOPPED)
public void unregisterDatabaseProvider(@NonNull ServiceRegistry serviceRegistry) {
var service = serviceRegistry.registration(NodeDatabaseProvider.class, this.configuration.databaseServiceName());
if (service != null) {
service.unregister();
}
}
}
Loading