Add support for sqlite
This commit is contained in:
parent
fb2e04f90e
commit
96b833cb52
6 changed files with 24 additions and 9 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -29,3 +29,5 @@ HELP.md
|
||||||
.vscode/
|
.vscode/
|
||||||
.gradle
|
.gradle
|
||||||
*.sql
|
*.sql
|
||||||
|
|
||||||
|
twigs.db
|
|
@ -23,6 +23,7 @@ import kotlinx.coroutines.isActive
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
import java.lang.RuntimeException
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
fun main(args: Array<String>): Unit = io.ktor.server.cio.EngineMain.main(args)
|
fun main(args: Array<String>): Unit = io.ktor.server.cio.EngineMain.main(args)
|
||||||
|
@ -30,13 +31,25 @@ fun main(args: Array<String>): Unit = io.ktor.server.cio.EngineMain.main(args)
|
||||||
private const val DATABASE_VERSION = 3
|
private const val DATABASE_VERSION = 3
|
||||||
|
|
||||||
fun Application.module() {
|
fun Application.module() {
|
||||||
|
val dbType = environment.config.propertyOrNull("twigs.database.type")?.getString() ?: "sqlite"
|
||||||
val dbHost = environment.config.propertyOrNull("twigs.database.host")?.getString() ?: "localhost"
|
val dbHost = environment.config.propertyOrNull("twigs.database.host")?.getString() ?: "localhost"
|
||||||
val dbPort = environment.config.propertyOrNull("twigs.database.port")?.getString() ?: "5432"
|
val dbPort = environment.config.propertyOrNull("twigs.database.port")?.getString() ?: "5432"
|
||||||
val dbName = environment.config.propertyOrNull("twigs.database.name")?.getString() ?: "twigs"
|
val dbName = environment.config.propertyOrNull("twigs.database.name")?.getString() ?: "twigs"
|
||||||
val dbUser = environment.config.propertyOrNull("twigs.database.user")?.getString() ?: "twigs"
|
val dbUser = environment.config.propertyOrNull("twigs.database.user")?.getString() ?: "twigs"
|
||||||
val dbPass = environment.config.propertyOrNull("twigs.database.password")?.getString() ?: "twigs"
|
val dbPass = environment.config.propertyOrNull("twigs.database.password")?.getString() ?: "twigs"
|
||||||
val jdbcUrl = "jdbc:postgresql://$dbHost:$dbPort/$dbName?stringtype=unspecified"
|
val jdbcUrl = when (dbType) {
|
||||||
(LoggerFactory.getLogger("com.zaxxer.hikari") as ch.qos.logback.classic.Logger).level = Level.ERROR
|
"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 {
|
HikariDataSource(HikariConfig().apply {
|
||||||
setJdbcUrl(jdbcUrl)
|
setJdbcUrl(jdbcUrl)
|
||||||
username = dbUser
|
username = dbUser
|
||||||
|
|
|
@ -10,15 +10,11 @@ ktor {
|
||||||
|
|
||||||
twigs {
|
twigs {
|
||||||
database {
|
database {
|
||||||
host = localhost
|
|
||||||
host = ${?TWIGS_DB_HOST}
|
host = ${?TWIGS_DB_HOST}
|
||||||
port = 5432
|
|
||||||
port = ${?TWIGS_DB_PORT}
|
port = ${?TWIGS_DB_PORT}
|
||||||
name = twigs
|
name = twigs.db
|
||||||
name = ${?TWIGS_DB_NAME}
|
name = ${?TWIGS_DB_NAME}
|
||||||
user = twigs
|
|
||||||
user = ${?TWIGS_DB_USER}
|
user = ${?TWIGS_DB_USER}
|
||||||
password = twigs
|
|
||||||
password = ${?TWIGS_DB_PASS}
|
password = ${?TWIGS_DB_PASS}
|
||||||
}
|
}
|
||||||
password {
|
password {
|
||||||
|
|
|
@ -8,7 +8,8 @@ val ktorVersion: String by rootProject.extra
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(kotlin("stdlib"))
|
implementation(kotlin("stdlib"))
|
||||||
api(project(":storage"))
|
api(project(":storage"))
|
||||||
implementation(libs.postgres)
|
runtimeOnly(libs.postgres)
|
||||||
|
runtimeOnly(libs.sqlite)
|
||||||
api(libs.hikari)
|
api(libs.hikari)
|
||||||
implementation(libs.logback)
|
implementation(libs.logback)
|
||||||
testImplementation(libs.junit.jupiter.api)
|
testImplementation(libs.junit.jupiter.api)
|
||||||
|
|
|
@ -15,6 +15,7 @@ class MetadataRepository(dataSource: DataSource) :
|
||||||
.getResource("/sql/$toVersion.sql")
|
.getResource("/sql/$toVersion.sql")
|
||||||
?.readText()
|
?.readText()
|
||||||
?.split(";")
|
?.split(";")
|
||||||
|
?.filterNot { it.isBlank() }
|
||||||
?: throw Error("No migration found for version $toVersion")
|
?: throw Error("No migration found for version $toVersion")
|
||||||
dataSource.connection.use { conn ->
|
dataSource.connection.use { conn ->
|
||||||
queries.forEach { query ->
|
queries.forEach { query ->
|
||||||
|
|
|
@ -7,8 +7,9 @@ kotlinx-coroutines = "1.6.2"
|
||||||
ktor = "2.0.2"
|
ktor = "2.0.2"
|
||||||
logback = "1.2.11"
|
logback = "1.2.11"
|
||||||
mail = "1.6.2"
|
mail = "1.6.2"
|
||||||
postgres = "42.3.4"
|
postgres = "42.3.8"
|
||||||
shadow = "7.0.0"
|
shadow = "7.0.0"
|
||||||
|
sqlite = "3.42.0.0"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
bcrypt = { module = "at.favre.lib:bcrypt", version.ref = "bcrypt" }
|
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" }
|
logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
|
||||||
mail = { module = "com.sun.mail:javax.mail", version.ref = "mail" }
|
mail = { module = "com.sun.mail:javax.mail", version.ref = "mail" }
|
||||||
postgres = { module = "org.postgresql:postgresql", version.ref = "postgres" }
|
postgres = { module = "org.postgresql:postgresql", version.ref = "postgres" }
|
||||||
|
sqlite = { module = "org.xerial:sqlite-jdbc", version.ref = "sqlite" }
|
||||||
|
|
||||||
[bundles]
|
[bundles]
|
||||||
ktor-server = [
|
ktor-server = [
|
||||||
|
|
Loading…
Reference in a new issue