debug discordHook logics, session fix.

- DiscordHook normal respond is not defined
- add MariadbSessionStorage
This commit is contained in:
dalbodeule 2024-08-10 17:16:36 +09:00
parent e951ccee56
commit 2335a09391
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
5 changed files with 55 additions and 4 deletions

View File

@ -34,7 +34,8 @@ object Connector {
TimerConfigs, TimerConfigs,
LiveStatuses, LiveStatuses,
SongLists, SongLists,
SongConfigs SongConfigs,
Sessions
) )
transaction { transaction {

View File

@ -0,0 +1,18 @@
package space.mori.chzzk_bot.common.models
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
object Sessions: IntIdTable("session") {
val key = text("key")
val value = text("value")
}
class Session(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Session>(Sessions)
var key by Sessions.key
var value by Sessions.value
}

View File

@ -47,7 +47,7 @@ val server = embeddedServer(Netty, port = 8080, ) {
}) })
} }
install(Sessions) { install(Sessions) {
cookie<UserSession>("user_session", storage = SessionStorageMemory()) {} cookie<UserSession>("user_session", storage = MariadbSessionStorage()) {}
} }
install(Authentication) { install(Authentication) {
oauth("auth-oauth-naver") { oauth("auth-oauth-naver") {

View File

@ -0,0 +1,32 @@
package space.mori.chzzk_bot.webserver
import io.ktor.server.sessions.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
import space.mori.chzzk_bot.common.models.Session
import space.mori.chzzk_bot.common.models.Sessions as SessionTable
class MariadbSessionStorage: SessionStorage {
override suspend fun invalidate(id: String) {
return transaction {
val session = Session.find(
SessionTable.key eq id
).firstOrNull()
session?.delete()
}
}
override suspend fun read(id: String): String {
return transaction {
val session = Session.find(SessionTable.key eq id).firstOrNull()
?: throw NoSuchElementException("Session $id not found")
session.value
}
}
override suspend fun write(id: String, value: String) {
Session.findSingleByAndUpdate(SessionTable.key eq id) {
it.value = value
}
}
}

View File

@ -49,12 +49,12 @@ fun Route.apiDiscordRoutes() {
return@get return@get
} }
call.respond(HttpStatusCode.OK)
} }
} }
} }
data class DiscordRequireRegisterDTO( data class DiscordRequireRegisterDTO(
val user: String, val user: String,
val passkey: String val token: String
) )