Merge remote-tracking branch 'refs/remotes/origin/main'
This commit is contained in:
commit
65a1ac5d1a
7 changed files with 152 additions and 32 deletions
|
@ -6,6 +6,7 @@ val postgresql_version: String by project
|
||||||
val hikariCP_version: String by project
|
val hikariCP_version: String by project
|
||||||
val exposed_version: String by project
|
val exposed_version: String by project
|
||||||
val testcontainers_postgresql_version: String by project
|
val testcontainers_postgresql_version: String by project
|
||||||
|
val junit_jupiter_version: String by project
|
||||||
|
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
|
@ -42,7 +43,18 @@ dependencies {
|
||||||
implementation("org.jetbrains.exposed:exposed-core:$exposed_version")
|
implementation("org.jetbrains.exposed:exposed-core:$exposed_version")
|
||||||
implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version")
|
implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version")
|
||||||
implementation("org.jetbrains.exposed:exposed-java-time:$exposed_version")
|
implementation("org.jetbrains.exposed:exposed-java-time:$exposed_version")
|
||||||
|
implementation("io.ktor:ktor-server-cio:$ktor_version")
|
||||||
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
|
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
|
||||||
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
|
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
|
||||||
testImplementation("org.testcontainers:postgresql:$testcontainers_postgresql_version")
|
testImplementation("org.testcontainers:postgresql:$testcontainers_postgresql_version")
|
||||||
|
testImplementation("org.junit.jupiter:junit-jupiter-api:$junit_jupiter_version")
|
||||||
|
testImplementation("org.junit.jupiter:junit-jupiter-params:$junit_jupiter_version")
|
||||||
|
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junit_jupiter_version")
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.named<Test>("test"){
|
||||||
|
useJUnitPlatform()
|
||||||
|
testLogging{
|
||||||
|
events("skipped", "failed")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,3 +7,4 @@ postgresql_version=42.6.0
|
||||||
hikariCP_version=5.0.1
|
hikariCP_version=5.0.1
|
||||||
exposed_version=0.41.1
|
exposed_version=0.41.1
|
||||||
testcontainers_postgresql_version=1.18.3
|
testcontainers_postgresql_version=1.18.3
|
||||||
|
junit_jupiter_version=5.9.3
|
||||||
|
|
|
@ -1,15 +1,26 @@
|
||||||
package no.nav.helse.sprik
|
package no.nav.helse.sprik
|
||||||
|
|
||||||
import io.ktor.server.application.*
|
import kotlinx.coroutines.runBlocking
|
||||||
import io.ktor.server.engine.*
|
import no.nav.helse.sprik.db.Database
|
||||||
import io.ktor.server.netty.*
|
|
||||||
import no.nav.helse.sprik.plugins.*
|
import no.nav.helse.sprik.plugins.*
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module)
|
val db = Database()
|
||||||
.start(wait = true)
|
db.migrate()
|
||||||
|
val app = Application(db)
|
||||||
|
app.startBlocking()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Application.module() {
|
class Application(private val db: Database) {
|
||||||
configureRouting()
|
fun startBlocking() {
|
||||||
|
|
||||||
|
runBlocking {
|
||||||
|
configureRouting().start(wait = false)
|
||||||
|
Runtime.getRuntime().addShutdownHook(
|
||||||
|
Thread {
|
||||||
|
db.dataSource.close()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,13 +5,43 @@ import com.zaxxer.hikari.HikariDataSource
|
||||||
import no.nav.helse.sprik.db.Environment.Database.host
|
import no.nav.helse.sprik.db.Environment.Database.host
|
||||||
import no.nav.helse.sprik.db.Environment.Database.name
|
import no.nav.helse.sprik.db.Environment.Database.name
|
||||||
import no.nav.helse.sprik.db.Environment.Database.port
|
import no.nav.helse.sprik.db.Environment.Database.port
|
||||||
|
import org.flywaydb.core.Flyway
|
||||||
|
import kotlin.time.Duration.Companion.minutes
|
||||||
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
import kotlin.time.toJavaDuration
|
||||||
|
|
||||||
class Database(dbconfig: HikariConfig) {
|
class Database(dbconfig: HikariConfig = dbconfig()) {
|
||||||
val dataSource by lazy { HikariDataSource(dbconfig) }
|
val dataSource by lazy { HikariDataSource(dbconfig) }
|
||||||
|
fun migrate() {
|
||||||
|
migrateconfig()
|
||||||
|
.let { HikariDataSource(it) }
|
||||||
|
.also {
|
||||||
|
Flyway.configure()
|
||||||
|
.dataSource(it)
|
||||||
|
.lockRetryCount(-1)
|
||||||
|
.load()
|
||||||
|
.migrate()
|
||||||
|
}
|
||||||
|
.close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun dbconfig() = HikariConfig().apply {
|
private fun dbconfig() = HikariConfig().apply {
|
||||||
jdbcUrl = DB_URL
|
jdbcUrl = DB_URL
|
||||||
|
username = username
|
||||||
|
password = password
|
||||||
|
maximumPoolSize = 1
|
||||||
|
connectionTimeout = 30.seconds.toJavaDuration().toMillis()
|
||||||
|
initializationFailTimeout = 1.minutes.toJavaDuration().toMillis()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun migrateconfig() = HikariConfig().apply {
|
||||||
|
jdbcUrl = DB_URL
|
||||||
|
username = username
|
||||||
|
password = password
|
||||||
|
maximumPoolSize = 2
|
||||||
|
connectionTimeout = 30.seconds.toJavaDuration().toMillis()
|
||||||
|
initializationFailTimeout = 1.minutes.toJavaDuration().toMillis()
|
||||||
}
|
}
|
||||||
|
|
||||||
val DB_URL = "jdbc:postgresql://%s:%s/%s".format(host, port, name)
|
val DB_URL = "jdbc:postgresql://%s:%s/%s".format(host, port, name)
|
||||||
|
@ -19,8 +49,10 @@ val DB_URL = "jdbc:postgresql://%s:%s/%s".format(host, port, name)
|
||||||
object Environment {
|
object Environment {
|
||||||
object Database {
|
object Database {
|
||||||
private val env = System.getenv()
|
private val env = System.getenv()
|
||||||
val host = requireNotNull(env["DATABASE_HOST"]) { "Host må settes" }
|
internal val host = requireNotNull(env["DATABASE_HOST"]) { "Host må settes" }
|
||||||
val port = requireNotNull(env["DATABASE_PORT"]) { "Port må settes" }
|
internal val port = requireNotNull(env["DATABASE_PORT"]) { "Port må settes" }
|
||||||
val name = requireNotNull(env["DATABASE_DATABASE"]) { "Databasenavn må settes" }
|
internal val name = requireNotNull(env["DATABASE_DATABASE"]) { "Databasenavn må settes" }
|
||||||
|
internal val username = requireNotNull(env["DATABASE_USERNAME"]) { "Brukernavn må settes" }
|
||||||
|
internal val password = requireNotNull(env["DATABASE_PASSWORD"]) { "Passord må settes" }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,33 +6,40 @@ import io.ktor.server.response.*
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
import io.ktor.server.plugins.contentnegotiation.*
|
import io.ktor.server.plugins.contentnegotiation.*
|
||||||
import io.ktor.serialization.kotlinx.json.*
|
import io.ktor.serialization.kotlinx.json.*
|
||||||
|
import io.ktor.server.cio.*
|
||||||
|
import io.ktor.server.engine.*
|
||||||
import io.ktor.server.plugins.cors.routing.*
|
import io.ktor.server.plugins.cors.routing.*
|
||||||
import io.ktor.server.request.*
|
import io.ktor.server.request.*
|
||||||
import no.nav.helse.sprik.Feil
|
import no.nav.helse.sprik.Feil
|
||||||
import no.nav.helse.sprik.Test
|
import no.nav.helse.sprik.Test
|
||||||
|
|
||||||
fun Application.configureRouting() {
|
fun configureRouting(): ApplicationEngine = embeddedServer(CIO, applicationEngineEnvironment {
|
||||||
install(CORS) {
|
module {
|
||||||
anyHost()
|
install(CORS) {
|
||||||
allowMethod(HttpMethod.Get)
|
anyHost()
|
||||||
allowMethod(HttpMethod.Post)
|
allowMethod(HttpMethod.Get)
|
||||||
allowNonSimpleContentTypes = true
|
allowMethod(HttpMethod.Post)
|
||||||
}
|
allowNonSimpleContentTypes = true
|
||||||
install(ContentNegotiation) {
|
|
||||||
json()
|
|
||||||
}
|
|
||||||
routing {
|
|
||||||
get("/") {
|
|
||||||
call.respondText("Hello World!")
|
|
||||||
}
|
}
|
||||||
post("/test") {
|
install(ContentNegotiation) {
|
||||||
val test = call.receive<Test>()
|
json()
|
||||||
call.respond(status = HttpStatusCode.Created, message = test)
|
|
||||||
}
|
}
|
||||||
post("/nyFeil"){
|
routing {
|
||||||
val test = call.receive<Feil>()
|
get("/") {
|
||||||
println(test)
|
call.respondText("Hello World!")
|
||||||
call.respond(status = HttpStatusCode.Created, message = test)
|
}
|
||||||
|
post("/test") {
|
||||||
|
val test = call.receive<Test>()
|
||||||
|
call.respond(status = HttpStatusCode.Created, message = test)
|
||||||
|
}
|
||||||
|
post("/nyFeil") {
|
||||||
|
val test = call.receive<Feil>()
|
||||||
|
println(test)
|
||||||
|
call.respond(status = HttpStatusCode.Created, message = test)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
14
backend/src/test/kotlin/no/nav/helse/sprik/DbTest.kt
Normal file
14
backend/src/test/kotlin/no/nav/helse/sprik/DbTest.kt
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package no.nav.helse.sprik
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
|
||||||
|
class DbTest {
|
||||||
|
@Test
|
||||||
|
fun `Tester noe rart for å sjekke om junit funker`() {
|
||||||
|
val a = 2
|
||||||
|
assertEquals(2, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
43
backend/src/test/kotlin/no/nav/helse/sprik/TestUtils.kt
Normal file
43
backend/src/test/kotlin/no/nav/helse/sprik/TestUtils.kt
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
package no.nav.helse.sprik
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.HikariConfig
|
||||||
|
import no.nav.helse.sprik.db.Database
|
||||||
|
import org.flywaydb.core.Flyway
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.testcontainers.containers.PostgreSQLContainer
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
// Lager Docker-container som databasen kan kjør på, og setter opp env-variabler for oss
|
||||||
|
fun postgres() = PostgreSQLContainer<Nothing>("postgres:15").apply {
|
||||||
|
withReuse(true)
|
||||||
|
withLabel("App", "db")
|
||||||
|
start()
|
||||||
|
println("Databasen har startet opp på port $firstMappedPort")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lager en testconfig for oss for å connecte til databasen
|
||||||
|
fun dbconfig(): HikariConfig {
|
||||||
|
val postgres = postgres()
|
||||||
|
return HikariConfig().apply {
|
||||||
|
jdbcUrl = postgres.jdbcUrl
|
||||||
|
username = postgres.username
|
||||||
|
password = postgres.password
|
||||||
|
maximumPoolSize = 2
|
||||||
|
minimumIdle = 1
|
||||||
|
idleTimeout = 500000
|
||||||
|
connectionTimeout = 10000
|
||||||
|
maxLifetime = 600000
|
||||||
|
initializationFailTimeout = 5000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migrerer databasen
|
||||||
|
fun Database.configureFlyway(): Database = this.also { database ->
|
||||||
|
Flyway.configure()
|
||||||
|
.dataSource(database.dataSource)
|
||||||
|
.failOnMissingLocations(true)
|
||||||
|
.cleanDisabled(false)
|
||||||
|
.load()
|
||||||
|
.also(Flyway::clean)
|
||||||
|
.migrate()
|
||||||
|
}
|
Reference in a new issue