Skip to content

Commit

Permalink
sette opp database i ebms-provider appen
Browse files Browse the repository at this point in the history
  • Loading branch information
alpet committed Sep 21, 2023
1 parent df6c998 commit 0bc0991
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ebms-provider/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions ebms-provider/src/main/kotlin/no/nav/emottak/ebms/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
33 changes: 33 additions & 0 deletions ebms-provider/src/main/kotlin/no/nav/emottak/ebms/db/Database.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 0bc0991

Please sign in to comment.