From 0bc09914a8bbd395732637fe5ca7c8743f273d2f Mon Sep 17 00:00:00 2001 From: Alexander Petrov Date: Thu, 21 Sep 2023 15:39:14 +0200 Subject: [PATCH] sette opp database i ebms-provider appen --- ebms-provider/build.gradle.kts | 10 ++++++ .../main/kotlin/no/nav/emottak/ebms/App.kt | 5 +++ .../kotlin/no/nav/emottak/ebms/db/Database.kt | 33 +++++++++++++++++++ .../no/nav/emottak/ebms/db/DatabaseConfig.kt | 25 ++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 ebms-provider/src/main/kotlin/no/nav/emottak/ebms/db/Database.kt create mode 100644 ebms-provider/src/main/kotlin/no/nav/emottak/ebms/db/DatabaseConfig.kt diff --git a/ebms-provider/build.gradle.kts b/ebms-provider/build.gradle.kts index 48dfb79b..a729e930 100644 --- a/ebms-provider/build.gradle.kts +++ b/ebms-provider/build.gradle.kts @@ -23,15 +23,25 @@ tasks.test { } dependencies { + implementation(project(":felles")) implementation("io.ktor:ktor-server-core:2.3.4") implementation("io.ktor:ktor-server-netty:2.3.4") implementation("com.github.labai:labai-jsr305x-annotations:0.0.2") implementation("jakarta.xml.bind:jakarta.xml.bind-api:4.0.0", ) implementation("org.glassfish.jaxb:jaxb-runtime:2.4.0-b180830.0438") + implementation("com.zaxxer:HikariCP:5.0.1") + implementation("org.flywaydb:flyway-core:9.16.3") + implementation("org.jetbrains.exposed:exposed-core:0.43.0") + implementation("org.jetbrains.exposed:exposed-dao:0.43.0") + implementation("org.jetbrains.exposed:exposed-jdbc:0.43.0") + implementation("org.jetbrains.exposed:exposed-java-time:0.43.0") //implementation("org.glassfish.jaxb:jaxb-runtime:4.0.3") // TODO: Latest. Krever at protokoll oppdateres implementation("no.nav.emottak:ebxml-protokoll:0.0.4") testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1") + + runtimeOnly("org.postgresql:postgresql:42.6.0") + } application { diff --git a/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/App.kt b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/App.kt index df4be7bb..e0a668f1 100644 --- a/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/App.kt +++ b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/App.kt @@ -11,6 +11,9 @@ import io.ktor.server.netty.* import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* +import no.nav.emottak.ebms.db.Database +import no.nav.emottak.ebms.db.DatabaseConfig +import no.nav.emottak.ebms.db.mapHikariConfig import no.nav.emottak.ebms.model.EbMSAttachment import no.nav.emottak.ebms.model.EbMSDocument import no.nav.emottak.ebms.processing.EbmsMessageProcessor @@ -20,6 +23,8 @@ import kotlin.io.encoding.ExperimentalEncodingApi fun main() { val processor = EbmsMessageProcessor() + val database = Database(mapHikariConfig(DatabaseConfig())) + database.migrate() embeddedServer(Netty, port = 8080) { routing { diff --git a/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/db/Database.kt b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/db/Database.kt new file mode 100644 index 00000000..c97c9587 --- /dev/null +++ b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/db/Database.kt @@ -0,0 +1,33 @@ +package no.nav.emottak.ebms.db + +import com.zaxxer.hikari.HikariConfig +import com.zaxxer.hikari.HikariDataSource +import org.flywaydb.core.Flyway +import org.jetbrains.exposed.sql.Database + +class Database( + dbConfig: HikariConfig +) { + val dataSource by lazy { HikariDataSource(dbConfig) } + val db by lazy { Database.connect(dataSource) } + private val config = dbConfig + fun migrate() { + migrationConfig(config) + .let(::HikariDataSource) + .also { + Flyway.configure() + .dataSource(it) + .lockRetryCount(50) + .load() + .migrate() + }.close() + } + + private fun migrationConfig(conf: HikariConfig): HikariConfig = + HikariConfig().apply { + jdbcUrl = conf.jdbcUrl + username = conf.username + password = conf.password + maximumPoolSize = 3 + } +} \ No newline at end of file diff --git a/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/db/DatabaseConfig.kt b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/db/DatabaseConfig.kt new file mode 100644 index 00000000..eeaa5466 --- /dev/null +++ b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/db/DatabaseConfig.kt @@ -0,0 +1,25 @@ +package no.nav.emottak.ebms.db + +import com.zaxxer.hikari.HikariConfig +import no.nav.emottak.util.fromEnv + +private const val prefix = "NAIS_DATABASE_CPA_REPO_CPA_REPO_DB" + +data class DatabaseConfig( + val host: String = "${prefix}_HOST".fromEnv(), + val port: String = "${prefix}_PORT".fromEnv(), + val name: String = "${prefix}_DATABASE".fromEnv(), + val username: String = "${prefix}_USERNAME".fromEnv(), + val password: String = "${prefix}_PASSWORD".fromEnv(), + val url: String = "jdbc:postgresql://%s:%s/%s".format(host, port, name) +) + + +fun mapHikariConfig(databaseConfig: DatabaseConfig): HikariConfig { + return HikariConfig().apply { + jdbcUrl = databaseConfig.url + username = databaseConfig.username + password = databaseConfig.password + maximumPoolSize = 5 + } +} \ No newline at end of file