🚧 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
				
			
		|  | @ -1,15 +1,26 @@ | |||
| package no.nav.helse.sprik | ||||
| 
 | ||||
| import io.ktor.server.application.* | ||||
| import io.ktor.server.engine.* | ||||
| import io.ktor.server.netty.* | ||||
| import kotlinx.coroutines.runBlocking | ||||
| import no.nav.helse.sprik.db.Database | ||||
| import no.nav.helse.sprik.plugins.* | ||||
| 
 | ||||
| fun main() { | ||||
|     embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module) | ||||
|             .start(wait = true) | ||||
|     val db = Database() | ||||
|     db.migrate() | ||||
|     val app = Application(db) | ||||
|     app.startBlocking() | ||||
| } | ||||
| 
 | ||||
| fun Application.module() { | ||||
|     configureRouting() | ||||
| class Application(private val db: Database) { | ||||
|     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.name | ||||
| 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) } | ||||
|     fun migrate() { | ||||
|         migrateconfig() | ||||
|             .let { HikariDataSource(it) } | ||||
|             .also { | ||||
|                 Flyway.configure() | ||||
|                         .dataSource(it) | ||||
|                         .lockRetryCount(-1) | ||||
|                         .load() | ||||
|                         .migrate() | ||||
|             } | ||||
|             .close() | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| private fun dbconfig() = HikariConfig().apply { | ||||
|     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) | ||||
|  | @ -19,8 +49,10 @@ val DB_URL = "jdbc:postgresql://%s:%s/%s".format(host, port, name) | |||
| object Environment { | ||||
|     object Database { | ||||
|         private val env = System.getenv() | ||||
|         val host = requireNotNull(env["DATABASE_HOST"]) { "Host må settes" } | ||||
|         val port = requireNotNull(env["DATABASE_PORT"]) { "Port må settes" } | ||||
|         val name = requireNotNull(env["DATABASE_DATABASE"]) { "Databasenavn må settes" } | ||||
|         internal val host = requireNotNull(env["DATABASE_HOST"]) { "Host må settes" } | ||||
|         internal val port = requireNotNull(env["DATABASE_PORT"]) { "Port 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.plugins.contentnegotiation.* | ||||
| 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.request.* | ||||
| import no.nav.helse.sprik.Feil | ||||
| import no.nav.helse.sprik.Test | ||||
| 
 | ||||
| fun Application.configureRouting() { | ||||
|     install(CORS) { | ||||
|         anyHost() | ||||
|         allowMethod(HttpMethod.Get) | ||||
|         allowMethod(HttpMethod.Post) | ||||
|         allowNonSimpleContentTypes = true | ||||
|     } | ||||
|     install(ContentNegotiation) { | ||||
|         json() | ||||
|     } | ||||
|     routing { | ||||
|         get("/") { | ||||
|             call.respondText("Hello World!") | ||||
| fun configureRouting(): ApplicationEngine = embeddedServer(CIO, applicationEngineEnvironment { | ||||
|     module { | ||||
|         install(CORS) { | ||||
|             anyHost() | ||||
|             allowMethod(HttpMethod.Get) | ||||
|             allowMethod(HttpMethod.Post) | ||||
|             allowNonSimpleContentTypes = true | ||||
|         } | ||||
|         post("/test") { | ||||
|             val test = call.receive<Test>() | ||||
|             call.respond(status = HttpStatusCode.Created, message = test) | ||||
|         install(ContentNegotiation) { | ||||
|             json() | ||||
|         } | ||||
|         post("/nyFeil"){ | ||||
|             val test = call.receive<Feil>() | ||||
|             println(test) | ||||
|             call.respond(status = HttpStatusCode.Created, message = test) | ||||
|         routing { | ||||
|             get("/") { | ||||
|                 call.respondText("Hello World!") | ||||
|             } | ||||
|             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
	
	 Amalie Mansåker
						Amalie Mansåker