Skip to content

Commit

Permalink
Merge pull request #4 from navikt/dev/cpa-db
Browse files Browse the repository at this point in the history
Dev/cpa db
  • Loading branch information
alpet authored Sep 18, 2023
2 parents f463134 + 510bc73 commit edf2409
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ build/
# Ignore Gradle build output directory
build
buildSrc/
ebms-provider/out/
cpa-repo/out/
8 changes: 8 additions & 0 deletions cpa-repo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions cpa-repo/src/main/kotlin/no/nav/emottak/cpa/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
33 changes: 33 additions & 0 deletions cpa-repo/src/main/kotlin/no/nav/emottak/cpa/Database.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
7 changes: 7 additions & 0 deletions cpa-repo/src/main/kotlin/no/nav/emottak/cpa/EnvUtils.kt
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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
}
}
5 changes: 5 additions & 0 deletions cpa-repo/src/main/resources/db/migration/V1___cpa.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE cpa
(
cpa_id VARCHAR(256) NOT NULL UNIQUE,
cpa TEXT NOT NULL
);
Original file line number Diff line number Diff line change
Expand Up @@ -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<EbMSAttachment>) : EbMSBaseMessage

0 comments on commit edf2409

Please sign in to comment.