Skip to content

Commit

Permalink
allow running without db for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
anderssonw committed Jun 25, 2024
1 parent 01d8559 commit 8caea56
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
18 changes: 10 additions & 8 deletions src/main/kotlin/no/kartverket/matrikkel/bygning/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ fun Application.internalModule() {

DatabaseSingleton.init()
val dbConnection = DatabaseSingleton.getConnection()
val healthRepository = HealthRepository(dbConnection)
val healthService = HealthService(healthRepository)

routing {
get("/metrics") {
call.respondText(appMicrometerRegistry.scrape())
}
if (dbConnection != null) {
val healthRepository = HealthRepository(dbConnection)
val healthService = HealthService(healthRepository)

probeRouting(healthService)
}
routing {
get("/metrics") {
call.respondText(appMicrometerRegistry.scrape())
}

probeRouting(healthService)
}
}
}
17 changes: 12 additions & 5 deletions src/main/kotlin/no/kartverket/matrikkel/bygning/db/DBSingleton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ object DatabaseSingleton {
}
}

fun getConnection(): Connection {
if (connection != null) {
return connection as Connection
}
fun getConnection(): Connection? {
try {
if (connection != null) {
return connection as Connection
}

throw RuntimeException("Kunne ikke koble til database")
throw RuntimeException("Kunne ikke koble til database")

} catch (e: Exception) {
LOGGER.error(e.stackTraceToString())

return null
}
}
}

0 comments on commit 8caea56

Please sign in to comment.