From 7e49773c46ee82ab30818f02e4cfe317fca22925 Mon Sep 17 00:00:00 2001 From: Alexander Petrov Date: Mon, 18 Sep 2023 10:26:58 +0200 Subject: [PATCH 1/3] test flyway --- .github/workflows/build.yaml | 3 +- .gitignore | 2 ++ cpa-repo/build.gradle.kts | 6 ++++ .../src/main/kotlin/no/nav/emottak/cpa/App.kt | 4 +++ .../kotlin/no/nav/emottak/cpa/Database.kt | 33 +++++++++++++++++++ .../kotlin/no/nav/emottak/cpa/EnvUtils.kt | 7 ++++ .../nav/emottak/cpa/config/DatabaseConfig.kt | 25 ++++++++++++++ .../no/nav/emottak/ebms/model/EbMSMessage.kt | 2 +- 8 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 cpa-repo/src/main/kotlin/no/nav/emottak/cpa/Database.kt create mode 100644 cpa-repo/src/main/kotlin/no/nav/emottak/cpa/EnvUtils.kt create mode 100644 cpa-repo/src/main/kotlin/no/nav/emottak/cpa/config/DatabaseConfig.kt diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index ae1a69fe..520034ea 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -2,7 +2,8 @@ name: "Build and deploy ebms-provider" on: push: branches: - - "main" + - main + - dev/** env: "IMAGE_EBMS": "ghcr.io/${{ github.repository }}/ebms-provider:${{ github.sha }}" "IMAGE_CPA": "ghcr.io/${{ github.repository }}/cpa-repo:${{ github.sha }}" diff --git a/.gitignore b/.gitignore index 45facba1..e0eef701 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,5 @@ build/ # Ignore Gradle build output directory build buildSrc/ +ebms-provider/out/ +cpa-repo/out/ diff --git a/cpa-repo/build.gradle.kts b/cpa-repo/build.gradle.kts index 5ea0b80a..ca425fc4 100644 --- a/cpa-repo/build.gradle.kts +++ b/cpa-repo/build.gradle.kts @@ -40,6 +40,12 @@ dependencies { implementation("jakarta.xml.bind:jakarta.xml.bind-api:4.0.0", ) implementation("org.glassfish.jaxb:jaxb-runtime:2.4.0-b180830.0438") implementation("no.nav.emottak:ebxml-protokoll:0.0.4") + 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") testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1") } diff --git a/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/App.kt b/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/App.kt index 8f54f8db..990a0075 100644 --- a/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/App.kt +++ b/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/App.kt @@ -9,8 +9,12 @@ import io.ktor.server.engine.* import io.ktor.server.netty.* import io.ktor.server.response.* import io.ktor.server.routing.* +import no.nav.emottak.cpa.config.DatabaseConfig +import no.nav.emottak.cpa.config.mapHikariConfig fun main() { + val database = Database(mapHikariConfig(DatabaseConfig())) + database.migrate() embeddedServer(Netty, port = 8080) { routing { diff --git a/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/Database.kt b/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/Database.kt new file mode 100644 index 00000000..6e6e78ad --- /dev/null +++ b/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/Database.kt @@ -0,0 +1,33 @@ +package no.nav.emottak.cpa + +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/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/EnvUtils.kt b/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/EnvUtils.kt new file mode 100644 index 00000000..4516da80 --- /dev/null +++ b/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/EnvUtils.kt @@ -0,0 +1,7 @@ +package no.nav.emottak.cpa + +fun getEnvVar(varName: String, defaultValue: String? = null) = + System.getProperty(varName) ?: System.getenv(varName) ?: defaultValue ?: throw RuntimeException("Environment: Missing required variable \"$varName\"") + +fun String.fromEnv(): String = + getEnvVar(this) diff --git a/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/config/DatabaseConfig.kt b/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/config/DatabaseConfig.kt new file mode 100644 index 00000000..f01906df --- /dev/null +++ b/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/config/DatabaseConfig.kt @@ -0,0 +1,25 @@ +package no.nav.emottak.cpa.config + +import com.zaxxer.hikari.HikariConfig +import no.nav.emottak.cpa.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 diff --git a/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/model/EbMSMessage.kt b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/model/EbMSMessage.kt index 53537aa6..b70512c4 100644 --- a/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/model/EbMSMessage.kt +++ b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/model/EbMSMessage.kt @@ -3,6 +3,6 @@ package no.nav.emottak.ebms.model import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.AckRequested import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.MessageHeader -data class EbMSMessage(override val messageHeader: MessageHeader, +class EbMSMessage(override val messageHeader: MessageHeader, val ackRequested: AckRequested? = null, val attachments: List) : EbMSBaseMessage From 3ee878386e022d215a4a219879ff1e9bce48fd75 Mon Sep 17 00:00:00 2001 From: Alexander Petrov Date: Mon, 18 Sep 2023 13:03:51 +0200 Subject: [PATCH 2/3] legg til driver --- cpa-repo/build.gradle.kts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpa-repo/build.gradle.kts b/cpa-repo/build.gradle.kts index ca425fc4..b0c0c183 100644 --- a/cpa-repo/build.gradle.kts +++ b/cpa-repo/build.gradle.kts @@ -48,6 +48,8 @@ dependencies { implementation("org.jetbrains.exposed:exposed-java-time:0.43.0") 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 { From a2ebba19befb381dbcf9daa213c38d2f14587deb Mon Sep 17 00:00:00 2001 From: Alexander Petrov Date: Mon, 18 Sep 2023 13:11:19 +0200 Subject: [PATCH 3/3] legg til sql filene --- cpa-repo/src/main/resources/db/migration/V1___cpa.sql | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 cpa-repo/src/main/resources/db/migration/V1___cpa.sql diff --git a/cpa-repo/src/main/resources/db/migration/V1___cpa.sql b/cpa-repo/src/main/resources/db/migration/V1___cpa.sql new file mode 100644 index 00000000..8eb91c83 --- /dev/null +++ b/cpa-repo/src/main/resources/db/migration/V1___cpa.sql @@ -0,0 +1,5 @@ +CREATE TABLE cpa +( + cpa_id VARCHAR(256) NOT NULL UNIQUE, + cpa TEXT NOT NULL +);