mirror of
https://github.com/Suwayomi/Tachidesk.git
synced 2025-12-10 06:42:07 +01:00
multi-user experiment
This commit is contained in:
@@ -25,10 +25,10 @@ import org.kodein.di.conf.global
|
|||||||
import org.kodein.di.instance
|
import org.kodein.di.instance
|
||||||
import suwayomi.tachidesk.global.GlobalAPI
|
import suwayomi.tachidesk.global.GlobalAPI
|
||||||
import suwayomi.tachidesk.manga.MangaAPI
|
import suwayomi.tachidesk.manga.MangaAPI
|
||||||
|
import suwayomi.tachidesk.server.database.DBManager.databaseForUser
|
||||||
import suwayomi.tachidesk.server.util.Browser
|
import suwayomi.tachidesk.server.util.Browser
|
||||||
import suwayomi.tachidesk.server.util.setupWebInterface
|
import suwayomi.tachidesk.server.util.setupWebInterface
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.lang.IllegalArgumentException
|
|
||||||
import java.util.concurrent.CompletableFuture
|
import java.util.concurrent.CompletableFuture
|
||||||
import kotlin.concurrent.thread
|
import kotlin.concurrent.thread
|
||||||
|
|
||||||
@@ -66,6 +66,8 @@ object JavalinSetup {
|
|||||||
ctx.header("WWW-Authenticate", "Basic")
|
ctx.header("WWW-Authenticate", "Basic")
|
||||||
ctx.status(401).json("Unauthorized")
|
ctx.status(401).json("Unauthorized")
|
||||||
} else {
|
} else {
|
||||||
|
val username = if (serverConfig.basicAuthEnabled) serverConfig.basicAuthUsername else "default-user"
|
||||||
|
ctx.attribute("db", databaseForUser(username))
|
||||||
handler.handle(ctx)
|
handler.handle(ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,27 +19,50 @@ import suwayomi.tachidesk.server.ApplicationDirs
|
|||||||
import suwayomi.tachidesk.server.ServerConfig
|
import suwayomi.tachidesk.server.ServerConfig
|
||||||
|
|
||||||
object DBManager {
|
object DBManager {
|
||||||
|
private val logger = KotlinLogging.logger {}
|
||||||
|
|
||||||
val db by lazy {
|
val masterDB by lazy {
|
||||||
val applicationDirs by DI.global.instance<ApplicationDirs>()
|
val applicationDirs by DI.global.instance<ApplicationDirs>()
|
||||||
|
|
||||||
Database.connect(
|
Database.connect(
|
||||||
"jdbc:h2:${applicationDirs.dataRoot}/database",
|
"jdbc:h2:${applicationDirs.dataRoot}/server",
|
||||||
"org.h2.Driver",
|
"org.h2.Driver",
|
||||||
databaseConfig = DatabaseConfig {
|
databaseConfig = DatabaseConfig {
|
||||||
useNestedTransactions = true
|
useNestedTransactions = true
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private val logger = KotlinLogging.logger {}
|
fun masterDbUp() {
|
||||||
|
val db = masterDB
|
||||||
fun databaseUp(db: Database = DBManager.db) {
|
logger.info { "Initialized masterDb: ${masterDB.url}" }
|
||||||
// call db to initialize the lazy object
|
|
||||||
logger.info {
|
|
||||||
"Using ${db.vendor} database version ${db.version}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val migrations = loadMigrationsFrom("suwayomi.tachidesk.server.database.migration", ServerConfig::class.java)
|
private val usersDatabases = mutableMapOf<String, Database>()
|
||||||
runMigrations(migrations)
|
fun databaseForUser(username: String) {
|
||||||
|
usersDatabases.getOrElse(username) {
|
||||||
|
val applicationDirs by DI.global.instance<ApplicationDirs>()
|
||||||
|
|
||||||
|
// call db to initialize the lazy object
|
||||||
|
logger.info {
|
||||||
|
"initializing database for user $username"
|
||||||
|
}
|
||||||
|
|
||||||
|
val database = Database.connect(
|
||||||
|
"jdbc:h2:${applicationDirs.dataRoot}/server",
|
||||||
|
"org.h2.Driver",
|
||||||
|
databaseConfig = DatabaseConfig {
|
||||||
|
useNestedTransactions = true
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
val migrations =
|
||||||
|
loadMigrationsFrom("suwayomi.tachidesk.server.database.migration", ServerConfig::class.java)
|
||||||
|
runMigrations(migrations, database)
|
||||||
|
|
||||||
|
usersDatabases[username] = database
|
||||||
|
|
||||||
|
database
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package suwayomi.tachidesk.server.model.table
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (C) Contributors to the Suwayomi project
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
class User
|
||||||
Reference in New Issue
Block a user