🚧 Oppsett av database
Co-authored-by: Sindre Kjelsrud <sindre.kjelsrud@nav.no> Co-authored-by: Markus A. R. Johansen <markus.aleksander.rakil.johansen@nav.no> Co-authored-by: Hege Haavaldsen <hege.haavaldsen@nav.no> Co-authored-by: Helene Arnesen <helene.arnesen@nav.no>
This commit is contained in:
parent
23233121d0
commit
0388b06f93
4 changed files with 83 additions and 32 deletions
|
@ -42,6 +42,7 @@ 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")
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in a new issue