Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database connection #375

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
val version = "0.0.1"
val group = "one.oktw"

val galaxyLibVersion = "dc1e26cd"
val galaxyLibVersion = "78eec6fa"

repositories {
mavenCentral()
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/one/oktw/galaxy/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import one.oktw.galaxy.command.commands.Join
import one.oktw.galaxy.command.commands.Spawn
import one.oktw.galaxy.event.EventManager
import one.oktw.galaxy.event.type.ProxyResponseEvent
import one.oktw.galaxy.internal.DatabaseManager
import one.oktw.galaxy.item.event.CustomItemEventHandler
import one.oktw.galaxy.mixin.interfaces.CustomRecipeManager
import one.oktw.galaxy.player.Harvest
Expand All @@ -55,6 +56,9 @@ class Main : DedicatedServerModInitializer {
private set
lateinit var eventManager: EventManager
private set
lateinit var dbManager: DatabaseManager
private set


companion object {
val PROXY_IDENTIFIER = Identifier("galaxy", "proxy")
Expand All @@ -67,6 +71,7 @@ class Main : DedicatedServerModInitializer {
null
} ?: ProxyAPI.dummyUUID
}
val dbPath = System.getenv("dbPath") ?: ""
}

override fun onInitializeServer() {
Expand Down Expand Up @@ -114,5 +119,10 @@ class Main : DedicatedServerModInitializer {

// server.log("current server id is $selfUID
println("current server id is $selfUUID")

dbManager = DatabaseManager(dbPath)

// server.log("current server id is $selfUID
println("database is ready")
}
}
48 changes: 48 additions & 0 deletions src/main/kotlin/one/oktw/galaxy/internal/DatabaseManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2021
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.internal

import com.mongodb.ConnectionString
import com.mongodb.MongoClientSettings
import com.mongodb.reactivestreams.client.MongoClients
import org.bson.codecs.configuration.CodecRegistries.fromRegistries
import org.bson.codecs.pojo.Conventions.*
import org.bson.codecs.pojo.PojoCodecProvider

class DatabaseManager(dbPath: String) {
val database = PojoCodecProvider.builder() // POJO settings
.automatic(true)
.conventions(listOf(SET_PRIVATE_FIELDS_CONVENTION, ANNOTATION_CONVENTION, CLASS_AND_PROPERTY_CONVENTION))
.build()
.let {
// register codec
fromRegistries(
MongoClientSettings.getDefaultCodecRegistry()
)
}
.let {
// connect settings
MongoClientSettings.builder()
.codecRegistry(it)
.applyConnectionString(ConnectionString(dbPath))
.build()
}
.let(MongoClients::create) // connect
.getDatabase(ConnectionString(dbPath).database!!) // get database
}