Stonks/app/src/main/kotlin/xyz/etztech/stonks/App.kt

117 lines
4.0 KiB
Kotlin
Raw Normal View History

2021-06-25 19:12:54 +00:00
package xyz.etztech.stonks
2021-06-30 19:59:27 +00:00
import com.natpryce.konfig.*
import java.io.FileInputStream
import java.util.*
import kotlinx.coroutines.*
2021-06-29 21:49:36 +00:00
import org.h2.tools.Server
import org.jetbrains.exposed.exceptions.ExposedSQLException
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transaction
import xyz.etztech.stonks.api.initApiServer
2021-07-01 20:10:44 +00:00
import xyz.etztech.stonks.dsl.LiveStatistics
2021-06-29 21:49:36 +00:00
import xyz.etztech.stonks.dsl.Statistics
import xyz.etztech.stonks.statisticsimporter.StatisticsImporter
2021-06-25 19:12:54 +00:00
2021-06-29 21:49:36 +00:00
fun main() =
runBlocking {
println("Starting Stonks...")
2021-06-30 19:59:27 +00:00
val fis = FileInputStream("./stonks.config")
val config = Properties()
config.load(fis)
val databaseBaseDir = config.getProperty("databaseBaseDir")
val databaseName = config.getProperty("databaseName")
val h2StartWebServer = config.getProperty("h2StartWebServer").toBoolean()
val h2tWebServerPort = config.getProperty("h2tWebServerPort").toInt()
val h2TcpServerPort = config.getProperty("h2TcpServerPort").toInt()
val apiServerPort = config.getProperty("apiServerPort").toInt()
2021-06-30 20:59:47 +00:00
val statisticsUpdateInterval = config.getProperty("statisticsUpdateInterval").toLong()
2021-06-30 19:59:27 +00:00
val minecraftStatsFolder = config.getProperty("minecraftStatsFolder")
val database =
initH2Server(
databaseBaseDir,
databaseName,
h2TcpServerPort,
h2StartWebServer,
h2tWebServerPort)
2021-06-29 21:49:36 +00:00
initApiServer(apiServerPort, database)
2021-06-30 20:59:47 +00:00
initPeriodicFetching(statisticsUpdateInterval, minecraftStatsFolder, database)
2021-06-29 21:49:36 +00:00
delay(60 * 1000L)
println("END")
}
fun initH2Server(
databaseBaseDir: String,
databaseName: String,
h2TcpServerPort: Int,
h2StartWebServer: Boolean,
h2WebServerPort: Int
): Database {
2021-06-29 21:49:36 +00:00
val webServer =
Server.createWebServer(
"-trace", "-baseDir", databaseBaseDir, "-webPort", h2WebServerPort.toString())
2021-06-29 21:49:36 +00:00
val tcpServer =
Server.createTcpServer(
"-trace", "-baseDir", databaseBaseDir, "-webPort", h2TcpServerPort.toString())
2021-06-29 21:49:36 +00:00
if (h2StartWebServer) {
webServer.start()
println("H2 web interface started: ${webServer.getURL()}")
}
tcpServer.start()
println("H2 TCP endpoint started: ${tcpServer.getURL()}")
val database = Database.connect("jdbc:h2:$databaseBaseDir/$databaseName", "org.h2.Driver")
2021-06-29 21:49:36 +00:00
transaction {
addLogger(StdOutSqlLogger)
SchemaUtils.create(Statistics)
2021-07-01 20:10:44 +00:00
SchemaUtils.create(LiveStatistics)
2021-06-29 21:49:36 +00:00
// 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
2021-06-29 21:49:36 +00:00
try {
TransactionManager.current()
.exec("CREATE INDEX idx_playerid ON Statistics (\"PlayerId\")")
} catch (e: ExposedSQLException) {}
try {
TransactionManager.current()
.exec("CREATE INDEX idx_type_name ON Statistics (\"Type\", \"Name\")")
} catch (e: ExposedSQLException) {}
2021-07-01 20:10:44 +00:00
try {
TransactionManager.current()
.exec("CREATE INDEX idx_playerid ON LiveStatistics (\"PlayerId\")")
} catch (e: ExposedSQLException) {}
try {
TransactionManager.current()
.exec("CREATE INDEX idx_type_name ON LiveStatistics (\"Type\", \"Name\")")
} catch (e: ExposedSQLException) {}
2021-06-29 21:49:36 +00:00
}
return database
}
2021-06-29 21:49:36 +00:00
suspend fun initPeriodicFetching(interval: Long, folder: String, db: Database) =
coroutineScope {
launch {
while (true) {
2021-06-29 21:49:36 +00:00
StatisticsImporter.importStatistics(folder, db)
delay(interval)
}
}
}