diff --git a/.gitignore b/.gitignore index c4a5352..d80a535 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,5 @@ HELP.md .vscode/ .gradle *.sql + +twigs.db \ No newline at end of file diff --git a/app/src/main/kotlin/com/wbrawner/twigs/server/Application.kt b/app/src/main/kotlin/com/wbrawner/twigs/server/Application.kt index 758b4f4..edcdd38 100644 --- a/app/src/main/kotlin/com/wbrawner/twigs/server/Application.kt +++ b/app/src/main/kotlin/com/wbrawner/twigs/server/Application.kt @@ -23,6 +23,7 @@ import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import kotlinx.serialization.json.Json import org.slf4j.LoggerFactory +import java.lang.RuntimeException import java.util.concurrent.TimeUnit fun main(args: Array): Unit = io.ktor.server.cio.EngineMain.main(args) @@ -30,13 +31,25 @@ fun main(args: Array): Unit = io.ktor.server.cio.EngineMain.main(args) private const val DATABASE_VERSION = 3 fun Application.module() { + val dbType = environment.config.propertyOrNull("twigs.database.type")?.getString() ?: "sqlite" val dbHost = environment.config.propertyOrNull("twigs.database.host")?.getString() ?: "localhost" val dbPort = environment.config.propertyOrNull("twigs.database.port")?.getString() ?: "5432" val dbName = environment.config.propertyOrNull("twigs.database.name")?.getString() ?: "twigs" val dbUser = environment.config.propertyOrNull("twigs.database.user")?.getString() ?: "twigs" val dbPass = environment.config.propertyOrNull("twigs.database.password")?.getString() ?: "twigs" - val jdbcUrl = "jdbc:postgresql://$dbHost:$dbPort/$dbName?stringtype=unspecified" - (LoggerFactory.getLogger("com.zaxxer.hikari") as ch.qos.logback.classic.Logger).level = Level.ERROR + val jdbcUrl = when (dbType) { + "postgresql" -> { + "jdbc:$dbType://$dbHost:$dbPort/$dbName?stringtype=unspecified" + } + "sqlite" -> { + Class.forName("org.sqlite.JDBC") + "jdbc:$dbType:$dbName" + } + else -> { + throw RuntimeException("Unsupported DB type: $dbType") + } + } + (LoggerFactory.getLogger("com.zaxxer.hikari") as ch.qos.logback.classic.Logger).level = Level.DEBUG HikariDataSource(HikariConfig().apply { setJdbcUrl(jdbcUrl) username = dbUser diff --git a/app/src/main/resources/application.conf b/app/src/main/resources/application.conf index f51b743..8dc5a8e 100644 --- a/app/src/main/resources/application.conf +++ b/app/src/main/resources/application.conf @@ -10,15 +10,11 @@ ktor { twigs { database { - host = localhost host = ${?TWIGS_DB_HOST} - port = 5432 port = ${?TWIGS_DB_PORT} - name = twigs + name = twigs.db name = ${?TWIGS_DB_NAME} - user = twigs user = ${?TWIGS_DB_USER} - password = twigs password = ${?TWIGS_DB_PASS} } password { diff --git a/db/build.gradle.kts b/db/build.gradle.kts index f2681c8..1891934 100644 --- a/db/build.gradle.kts +++ b/db/build.gradle.kts @@ -8,7 +8,8 @@ val ktorVersion: String by rootProject.extra dependencies { implementation(kotlin("stdlib")) api(project(":storage")) - implementation(libs.postgres) + runtimeOnly(libs.postgres) + runtimeOnly(libs.sqlite) api(libs.hikari) implementation(libs.logback) testImplementation(libs.junit.jupiter.api) diff --git a/db/src/main/kotlin/com/wbrawner/twigs/db/MetadataRepository.kt b/db/src/main/kotlin/com/wbrawner/twigs/db/MetadataRepository.kt index eae2829..d256ca0 100644 --- a/db/src/main/kotlin/com/wbrawner/twigs/db/MetadataRepository.kt +++ b/db/src/main/kotlin/com/wbrawner/twigs/db/MetadataRepository.kt @@ -15,6 +15,7 @@ class MetadataRepository(dataSource: DataSource) : .getResource("/sql/$toVersion.sql") ?.readText() ?.split(";") + ?.filterNot { it.isBlank() } ?: throw Error("No migration found for version $toVersion") dataSource.connection.use { conn -> queries.forEach { query -> diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0967818..adaa675 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -7,8 +7,9 @@ kotlinx-coroutines = "1.6.2" ktor = "2.0.2" logback = "1.2.11" mail = "1.6.2" -postgres = "42.3.4" +postgres = "42.3.8" shadow = "7.0.0" +sqlite = "3.42.0.0" [libraries] bcrypt = { module = "at.favre.lib:bcrypt", version.ref = "bcrypt" } @@ -30,6 +31,7 @@ ktor-server-sessions = { module = "io.ktor:ktor-server-sessions", version.ref = logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" } mail = { module = "com.sun.mail:javax.mail", version.ref = "mail" } postgres = { module = "org.postgresql:postgresql", version.ref = "postgres" } +sqlite = { module = "org.xerial:sqlite-jdbc", version.ref = "sqlite" } [bundles] ktor-server = [