From 21d6537129ae6c92001905ad1d2682e5356c2476 Mon Sep 17 00:00:00 2001 From: mic050r Date: Thu, 15 Aug 2024 21:30:15 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8FEAT(#1)=20::=20PostgreSQL=20?= =?UTF-8?q?=EC=97=B0=EB=8F=99=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/example/com/Application.kt | 31 ++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/example/com/Application.kt b/src/main/kotlin/example/com/Application.kt index d545f1e..08d7ef4 100644 --- a/src/main/kotlin/example/com/Application.kt +++ b/src/main/kotlin/example/com/Application.kt @@ -2,15 +2,42 @@ package example.com import example.com.plugins.* import io.ktor.server.application.* +import io.ktor.server.netty.* +import com.zaxxer.hikari.HikariConfig +import com.zaxxer.hikari.HikariDataSource +import org.jetbrains.exposed.sql.Database fun main(args: Array) { - io.ktor.server.netty.EngineMain.main(args) + EngineMain.main(args) } fun Application.module() { + // 데이터베이스 설정을 여기서 초기화 + configureDatabase() + + // 다른 설정들 configureSerialization() - configureDatabases() configureHTTP() configureSecurity() configureRouting() } + +fun Application.configureDatabase() { + val hikariConfig = HikariConfig().apply { + jdbcUrl = "jdbc:postgresql://localhost:5432/test" + driverClassName = "org.postgresql.Driver" + username = "mic050r" + password = "123456" + maximumPoolSize = 10 + } + + try { + val dataSource = HikariDataSource(hikariConfig) + Database.connect(dataSource) + // 연결 성공 시 로그 출력 + environment.log.info("Database connected successfully!") + } catch (e: Exception) { + // 연결 실패 시 오류 로그 출력 + environment.log.error("Database connection failed: ${e.message}") + } +} From 83c5b6235e69118baa499a03058cba6895539771 Mon Sep 17 00:00:00 2001 From: mic050r Date: Thu, 15 Aug 2024 21:37:01 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8FEAT(#1)=20::=20DB=20=EC=97=B0?= =?UTF-8?q?=EA=B2=B0=20=EC=A0=95=EB=B3=B4=20=EC=99=B8=EB=B6=80=20=ED=99=98?= =?UTF-8?q?=EA=B2=BD=20=EB=B3=80=EC=88=98=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ build.gradle.kts | 3 +++ src/main/kotlin/example/com/Application.kt | 15 +++++++++++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index c426c32..5ded82a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.env + .gradle build/ !gradle/wrapper/gradle-wrapper.jar diff --git a/build.gradle.kts b/build.gradle.kts index 7777f1f..77bf063 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -43,6 +43,9 @@ dependencies { // HikariCP (커넥션 풀) implementation("com.zaxxer:HikariCP:5.0.1") + // env + implementation("io.github.cdimascio:dotenv-kotlin:6.4.0") + implementation("com.h2database:h2:$h2_version") implementation("io.ktor:ktor-server-openapi") implementation("io.ktor:ktor-server-auth-jvm") diff --git a/src/main/kotlin/example/com/Application.kt b/src/main/kotlin/example/com/Application.kt index 08d7ef4..37cc46e 100644 --- a/src/main/kotlin/example/com/Application.kt +++ b/src/main/kotlin/example/com/Application.kt @@ -5,6 +5,7 @@ import io.ktor.server.application.* import io.ktor.server.netty.* import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource +import io.github.cdimascio.dotenv.dotenv import org.jetbrains.exposed.sql.Database fun main(args: Array) { @@ -23,11 +24,17 @@ fun Application.module() { } fun Application.configureDatabase() { + val dotenv = dotenv() + + val dbUrl = dotenv["DB_URL"] ?: "jdbc:postgresql://localhost:5432/defaultdb" + val dbUser = dotenv["DB_USER"] ?: "defaultuser" + val dbPassword = dotenv["DB_PASSWORD"] ?: "defaultpassword" + val hikariConfig = HikariConfig().apply { - jdbcUrl = "jdbc:postgresql://localhost:5432/test" + jdbcUrl = dbUrl driverClassName = "org.postgresql.Driver" - username = "mic050r" - password = "123456" + username = dbUser + password = dbPassword maximumPoolSize = 10 } @@ -35,7 +42,7 @@ fun Application.configureDatabase() { val dataSource = HikariDataSource(hikariConfig) Database.connect(dataSource) // 연결 성공 시 로그 출력 - environment.log.info("Database connected successfully!") + environment.log.info("Database connected successfully! : $dbUrl") } catch (e: Exception) { // 연결 실패 시 오류 로그 출력 environment.log.error("Database connection failed: ${e.message}")