Files
kotlin-server-web-undertow/src/jvmMain/kotlin/nl/astraeus/tmpl/Main.kt
rnentjes 0eb853050b Refactor placeholder constants into dedicated file
Moved REPO_NAME and related constants into Placeholders.kt to improve code organization and maintainability. The code now dynamically references these constants where needed, simplifying updates and ensuring consistency across modules. Updated relevant imports and references to accommodate these changes.
2024-12-01 12:12:33 +01:00

68 lines
1.7 KiB
Kotlin

package nl.astraeus.tmpl
import com.zaxxer.hikari.HikariConfig
import io.undertow.Undertow
import io.undertow.UndertowOptions
import io.undertow.predicate.Predicates
import io.undertow.server.handlers.encoding.ContentEncodingRepository
import io.undertow.server.handlers.encoding.EncodingHandler
import io.undertow.server.handlers.encoding.GzipEncodingProvider
import nl.astraeus.logger.Logger
import nl.astraeus.tmpl.db.Database
import nl.astraeus.tmpl.web.RequestHandler
val log = Logger()
val SERVER_PORT = 7001
val JDBC_PORT = 8001
fun main() {
Thread.currentThread().uncaughtExceptionHandler = Thread.UncaughtExceptionHandler { t, e ->
log.warn(e) {
e.message
}
}
Runtime.getRuntime().addShutdownHook(
object : Thread() {
override fun run() {
Database.vacuumDatabase()
Database.closeDatabase()
}
}
)
Class.forName("nl.astraeus.jdbc.Driver")
Database.initialize(HikariConfig().apply {
driverClassName = "nl.astraeus.jdbc.Driver"
jdbcUrl = "jdbc:stat:webServerPort=$JDBC_PORT:jdbc:sqlite:data/$repoName.db"
username = "sa"
password = ""
maximumPoolSize = 25
isAutoCommit = false
validate()
})
val compressionHandler =
EncodingHandler(
ContentEncodingRepository()
.addEncodingHandler(
"gzip",
GzipEncodingProvider(), 50,
Predicates.parse("max-content-size(5)")
)
).setNext(RequestHandler)
val server = Undertow.builder()
.addHttpListener(SERVER_PORT, "localhost")
.setIoThreads(4)
.setHandler(compressionHandler)
.setServerOption(UndertowOptions.SHUTDOWN_TIMEOUT, 1000)
.build()
println("Starting undertow server at port 6007...")
server?.start()
}