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 00279312..c4a42ab7 100644 --- a/cpa-repo/build.gradle.kts +++ b/cpa-repo/build.gradle.kts @@ -40,9 +40,17 @@ 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") implementation("ch.qos.logback:logback-classic:1.4.11") 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/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/App.kt b/cpa-repo/src/main/kotlin/no/nav/emottak/cpa/App.kt index 4250c7f8..3248505a 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 @@ -10,6 +10,8 @@ import io.ktor.server.routing.get import io.ktor.server.routing.routing fun main() { + val database = Database(mapHikariConfig(DatabaseConfig())) + database.migrate() embeddedServer(Netty, port = 8080) { 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/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 +); 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