From a4140f708cf6a798006ae276c8197d1edc08cc57 Mon Sep 17 00:00:00 2001 From: Kevin Belisle Date: Wed, 30 Jun 2021 15:24:34 -0400 Subject: [PATCH] Prep work for using a configuration file --- app/src/main/kotlin/xyz/etztech/stonks/App.kt | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/xyz/etztech/stonks/App.kt b/app/src/main/kotlin/xyz/etztech/stonks/App.kt index a78204b..e46c04f 100644 --- a/app/src/main/kotlin/xyz/etztech/stonks/App.kt +++ b/app/src/main/kotlin/xyz/etztech/stonks/App.kt @@ -14,34 +14,48 @@ fun main() = runBlocking { println("Starting Stonks...") + val databaseBaseDir = "./databases" + val databaseName = "statistics" val h2StartWebServer = true val h2tWebServerPort = 8082 val h2TcpServerPort = 9092 - val database = initH2Server(h2TcpServerPort, h2StartWebServer, h2tWebServerPort) + val database = + initH2Server( + databaseBaseDir, + databaseName, + h2TcpServerPort, + h2StartWebServer, + h2tWebServerPort) val apiServerPort = 7000 initApiServer(apiServerPort, database) val periodicFetchingInterval = 15 * 60 * 1000L - val statisticsFolder = "../test_data" + val minecraftStatsFolder = "../test_data" - initPeriodicFetching(periodicFetchingInterval, statisticsFolder, database) + initPeriodicFetching(periodicFetchingInterval, minecraftStatsFolder, database) delay(60 * 1000L) println("END") } -fun initH2Server(h2TcpServerPort: Int, h2StartWebServer: Boolean, h2WebServerPort: Int): Database { +fun initH2Server( + databaseBaseDir: String, + databaseName: String, + h2TcpServerPort: Int, + h2StartWebServer: Boolean, + h2WebServerPort: Int +): Database { val webServer = Server.createWebServer( - "-trace", "-baseDir", "./databases", "-webPort", h2WebServerPort.toString()) + "-trace", "-baseDir", databaseBaseDir, "-webPort", h2WebServerPort.toString()) val tcpServer = Server.createTcpServer( - "-trace", "-baseDir", "./databases", "-webPort", h2TcpServerPort.toString()) + "-trace", "-baseDir", databaseBaseDir, "-webPort", h2TcpServerPort.toString()) if (h2StartWebServer) { webServer.start() @@ -51,17 +65,14 @@ fun initH2Server(h2TcpServerPort: Int, h2StartWebServer: Boolean, h2WebServerPor tcpServer.start() println("H2 TCP endpoint started: ${tcpServer.getURL()}") - val database = Database.connect("jdbc:h2:./databases/test", "org.h2.Driver") - - Statistics.index(false, Statistics.playerId) - Statistics.index("idx_type_name", false, Statistics.type, Statistics.name) - Statistics.index("idx_timestamp", false, Statistics.timestamp) - Statistics.index("idx_value", false, Statistics.value) + val database = Database.connect("jdbc:h2:$databaseBaseDir/$databaseName", "org.h2.Driver") transaction { addLogger(StdOutSqlLogger) SchemaUtils.create(Statistics) + // Create indexes with explicit SQL because I can't figure out how to do it with exposed + // Wrap it in a try block because it throws an exception if index already exists try { TransactionManager.current() .exec("CREATE INDEX idx_playerid ON Statistics (\"PlayerId\")")