diff --git a/build.gradle.kts b/build.gradle.kts index e28896a3..63d100c7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,6 +12,7 @@ plugins { id("io.ktor.plugin") version "2.3.0" id("org.graalvm.buildtools.native") version "0.9.21" id("io.gitlab.arturbosch.detekt") version "1.22.0" + id("com.google.devtools.ksp") version "1.8.21-1.0.11" // id("org.jetbrains.kotlin.plugin.serialization") version "1.8.10" } @@ -57,6 +58,10 @@ kotlin { } } +sourceSets.main { + java.srcDirs("build/generated/ksp/main/kotlin") +} + dependencies { implementation("io.ktor:ktor-server-core-jvm:$ktor_version") implementation("io.ktor:ktor-server-auth:$ktor_version") @@ -80,6 +85,10 @@ dependencies { implementation("io.insert-koin:koin-core:$koin_version") implementation("io.insert-koin:koin-ktor:$koin_version") implementation("io.insert-koin:koin-logger-slf4j:$koin_version") + implementation("io.insert-koin:koin-annotations:1.2.0") + ksp("io.insert-koin:koin-ksp-compiler:1.2.0") + + implementation("io.ktor:ktor-client-logging-jvm:$ktor_version") implementation("io.ktor:ktor-server-host-common-jvm:$ktor_version") implementation("io.ktor:ktor-server-status-pages-jvm:$ktor_version") diff --git a/src/main/kotlin/dev/usbharu/hideout/Application.kt b/src/main/kotlin/dev/usbharu/hideout/Application.kt index 1bc2747e..6a6dbf15 100644 --- a/src/main/kotlin/dev/usbharu/hideout/Application.kt +++ b/src/main/kotlin/dev/usbharu/hideout/Application.kt @@ -10,18 +10,15 @@ import dev.usbharu.hideout.config.ConfigData import dev.usbharu.hideout.domain.model.job.DeliverPostJob import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob import dev.usbharu.hideout.plugins.* -import dev.usbharu.hideout.repository.* +import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.routing.register import dev.usbharu.hideout.service.* -import dev.usbharu.hideout.service.activitypub.* +import dev.usbharu.hideout.service.activitypub.ActivityPubService +import dev.usbharu.hideout.service.activitypub.ActivityPubUserService import dev.usbharu.hideout.service.impl.IUserService -import dev.usbharu.hideout.service.impl.PostService -import dev.usbharu.hideout.service.impl.UserAuthService -import dev.usbharu.hideout.service.impl.UserService import dev.usbharu.hideout.service.job.JobQueueParentService import dev.usbharu.hideout.service.job.KJobJobQueueParentService import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService -import dev.usbharu.hideout.service.signature.HttpSignatureVerifyServiceImpl import dev.usbharu.kjob.exposed.ExposedKJob import io.ktor.client.* import io.ktor.client.engine.cio.* @@ -30,6 +27,7 @@ import io.ktor.server.application.* import kjob.core.kjob import kotlinx.coroutines.runBlocking import org.jetbrains.exposed.sql.Database +import org.koin.ksp.generated.module import org.koin.ktor.ext.inject import java.util.concurrent.TimeUnit @@ -59,10 +57,6 @@ fun Application.parent() { password = property("hideout.database.password") ) } - - single { UserRepository(get(), get()) } - single { UserAuthService(get()) } - single { HttpSignatureVerifyServiceImpl(get()) } single { val kJobJobQueueService = KJobJobQueueParentService(get()) kJobJobQueueService.init(emptyList()) @@ -79,19 +73,7 @@ fun Application.parent() { } } } - single { ActivityPubFollowServiceImpl(get(), get(), get(), get()) } - single { ActivityPubServiceImpl(get(), get()) } - single { UserService(get(), get()) } - single { ActivityPubUserServiceImpl(get(), get()) } - single { ActivityPubNoteServiceImpl(get(), get(), get()) } - single { PostService(get(), get()) } - single { PostRepositoryImpl(get(), get()) } single { TwitterSnowflakeIdGenerateService } - single { MetaRepositoryImpl(get()) } - single { ServerInitialiseServiceImpl(get()) } - single { JwtRefreshTokenRepositoryImpl(get(), get()) } - single { MetaServiceImpl(get()) } - single { JwtServiceImpl(get(), get(), get()) } single { JwkProviderBuilder(Config.configData.url).cached( 10, @@ -101,7 +83,7 @@ fun Application.parent() { .rateLimited(10, 1, TimeUnit.MINUTES).build() } } - configureKoin(module) + configureKoin(module, HideoutModule().module) runBlocking { inject().value.init() } diff --git a/src/main/kotlin/dev/usbharu/hideout/HideoutModule.kt b/src/main/kotlin/dev/usbharu/hideout/HideoutModule.kt new file mode 100644 index 00000000..f91506f8 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/HideoutModule.kt @@ -0,0 +1,8 @@ +package dev.usbharu.hideout + +import org.koin.core.annotation.ComponentScan +import org.koin.core.annotation.Module + +@Module +@ComponentScan +class HideoutModule diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/Koin.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/Koin.kt index f880852b..e7a9e249 100644 --- a/src/main/kotlin/dev/usbharu/hideout/plugins/Koin.kt +++ b/src/main/kotlin/dev/usbharu/hideout/plugins/Koin.kt @@ -5,9 +5,9 @@ import org.koin.core.module.Module import org.koin.ktor.plugin.Koin import org.koin.logger.slf4jLogger -fun Application.configureKoin(module: Module) { +fun Application.configureKoin(vararg module: Module) { install(Koin) { slf4jLogger() - modules(module) + modules(*module) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImpl.kt index 74bf019c..fccc8c38 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImpl.kt @@ -7,8 +7,10 @@ import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.transactions.transaction +import org.koin.core.annotation.Single import java.time.Instant +@Single class JwtRefreshTokenRepositoryImpl( private val database: Database, private val idGenerateService: IdGenerateService diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt index f58e555e..a479512d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt @@ -5,8 +5,10 @@ import kotlinx.coroutines.Dispatchers import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.transactions.transaction +import org.koin.core.annotation.Single import java.util.* +@Single class MetaRepositoryImpl(private val database: Database) : IMetaRepository { init { diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt index 669a0df7..9a57f4e7 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt @@ -11,7 +11,9 @@ import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.transactions.transaction +import org.koin.core.annotation.Single +@Single class PostRepositoryImpl(database: Database, private val idGenerateService: IdGenerateService) : IPostRepository { init { diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt index 8dc92ba0..763f71eb 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt @@ -8,8 +8,10 @@ import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.transactions.transaction +import org.koin.core.annotation.Single import java.time.Instant +@Single class UserRepository(private val database: Database, private val idGenerateService: IdGenerateService) : IUserRepository { init { diff --git a/src/main/kotlin/dev/usbharu/hideout/service/JwtServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/JwtServiceImpl.kt index 3548c84d..e8519f2c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/JwtServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/JwtServiceImpl.kt @@ -14,11 +14,13 @@ import dev.usbharu.hideout.util.RsaUtil import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async +import org.koin.core.annotation.Single import java.time.Instant import java.time.temporal.ChronoUnit import java.util.* @Suppress("InjectDispatcher") +@Single class JwtServiceImpl( private val metaService: IMetaService, private val refreshTokenRepository: IJwtRefreshTokenRepository, diff --git a/src/main/kotlin/dev/usbharu/hideout/service/MetaServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/MetaServiceImpl.kt index 4e5eb17a..db9412e4 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/MetaServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/MetaServiceImpl.kt @@ -4,7 +4,9 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Jwt import dev.usbharu.hideout.domain.model.hideout.entity.Meta import dev.usbharu.hideout.exception.NotInitException import dev.usbharu.hideout.repository.IMetaRepository +import org.koin.core.annotation.Single +@Single class MetaServiceImpl(private val metaRepository: IMetaRepository) : IMetaService { override suspend fun getMeta(): Meta = metaRepository.get() ?: throw NotInitException("Meta is null") diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ServerInitialiseServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/ServerInitialiseServiceImpl.kt index 35f53843..78bc0192 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ServerInitialiseServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ServerInitialiseServiceImpl.kt @@ -4,11 +4,13 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Jwt import dev.usbharu.hideout.domain.model.hideout.entity.Meta import dev.usbharu.hideout.repository.IMetaRepository import dev.usbharu.hideout.util.ServerUtil +import org.koin.core.annotation.Single import org.slf4j.Logger import org.slf4j.LoggerFactory import java.security.KeyPairGenerator import java.util.* +@Single class ServerInitialiseServiceImpl(private val metaRepository: IMetaRepository) : IServerInitialiseService { val logger: Logger = LoggerFactory.getLogger(ServerInitialiseServiceImpl::class.java) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImpl.kt index 2eb571ff..787a0fcc 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubFollowServiceImpl.kt @@ -13,7 +13,9 @@ import dev.usbharu.hideout.service.job.JobQueueParentService import io.ktor.client.* import io.ktor.http.* import kjob.core.job.JobProps +import org.koin.core.annotation.Single +@Single class ActivityPubFollowServiceImpl( private val jobQueueParentService: JobQueueParentService, private val activityPubUserService: ActivityPubUserService, diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImpl.kt index d49bc03c..401367d7 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImpl.kt @@ -11,9 +11,11 @@ import dev.usbharu.hideout.service.impl.IUserService import dev.usbharu.hideout.service.job.JobQueueParentService import io.ktor.client.* import kjob.core.job.JobProps +import org.koin.core.annotation.Single import org.slf4j.LoggerFactory import java.time.Instant +@Single class ActivityPubNoteServiceImpl( private val httpClient: HttpClient, private val jobQueueParentService: JobQueueParentService, diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt index aa95000a..e7299c7f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt @@ -10,9 +10,11 @@ import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob import dev.usbharu.hideout.exception.JsonParseException import kjob.core.dsl.JobContextWithProps import kjob.core.job.JobProps +import org.koin.core.annotation.Single import org.slf4j.Logger import org.slf4j.LoggerFactory +@Single class ActivityPubServiceImpl( private val activityPubFollowService: ActivityPubFollowService, private val activityPubNoteService: ActivityPubNoteService diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt index 767a6bdf..fc90ac97 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt @@ -15,7 +15,9 @@ import io.ktor.client.* import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* +import org.koin.core.annotation.Single +@Single class ActivityPubUserServiceImpl( private val userService: IUserService, private val httpClient: HttpClient diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt index e14694a2..71dd6be3 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt @@ -4,8 +4,10 @@ import dev.usbharu.hideout.domain.model.Post import dev.usbharu.hideout.repository.IPostRepository import dev.usbharu.hideout.service.IPostService import dev.usbharu.hideout.service.activitypub.ActivityPubNoteService +import org.koin.core.annotation.Single import org.slf4j.LoggerFactory +@Single class PostService( private val postRepository: IPostRepository, private val activityPubNoteService: ActivityPubNoteService diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/UserAuthService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/UserAuthService.kt index 51356cff..4450fe4e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/UserAuthService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/UserAuthService.kt @@ -4,9 +4,11 @@ import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.service.IUserAuthService import io.ktor.util.* +import org.koin.core.annotation.Single import java.security.* import java.util.* +@Single class UserAuthService( val userRepository: IUserRepository ) : IUserAuthService { diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/UserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/UserService.kt index 3118967c..599e274f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/UserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/UserService.kt @@ -7,9 +7,11 @@ import dev.usbharu.hideout.domain.model.hideout.entity.User import dev.usbharu.hideout.exception.UserNotFoundException import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.service.IUserAuthService +import org.koin.core.annotation.Single import java.lang.Integer.min import java.time.Instant +@Single class UserService(private val userRepository: IUserRepository, private val userAuthService: IUserAuthService) : IUserService { diff --git a/src/main/kotlin/dev/usbharu/hideout/service/signature/HttpSignatureVerifyServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/signature/HttpSignatureVerifyServiceImpl.kt index 923231c6..d44c4326 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/signature/HttpSignatureVerifyServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/signature/HttpSignatureVerifyServiceImpl.kt @@ -3,8 +3,10 @@ package dev.usbharu.hideout.service.signature import dev.usbharu.hideout.plugins.KtorKeyMap import dev.usbharu.hideout.repository.IUserRepository import io.ktor.http.* +import org.koin.core.annotation.Single import tech.barbero.http.message.signing.SignatureHeaderVerifier +@Single class HttpSignatureVerifyServiceImpl(private val userAuthService: IUserRepository) : HttpSignatureVerifyService { override fun verify(headers: Headers): Boolean { val build = SignatureHeaderVerifier.builder().keyMap(KtorKeyMap(userAuthService)).build()