Merge pull request #13 from usbharu/feature/di-with-annotation

Feature/di with annotation
This commit is contained in:
usbharu 2023-05-04 12:06:40 +09:00 committed by GitHub
commit 20907ca1bc
19 changed files with 54 additions and 25 deletions

View File

@ -12,6 +12,7 @@ plugins {
id("io.ktor.plugin") version "2.3.0" id("io.ktor.plugin") version "2.3.0"
id("org.graalvm.buildtools.native") version "0.9.21" id("org.graalvm.buildtools.native") version "0.9.21"
id("io.gitlab.arturbosch.detekt") version "1.22.0" 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" // 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 { dependencies {
implementation("io.ktor:ktor-server-core-jvm:$ktor_version") implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
implementation("io.ktor:ktor-server-auth:$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-core:$koin_version")
implementation("io.insert-koin:koin-ktor:$koin_version") implementation("io.insert-koin:koin-ktor:$koin_version")
implementation("io.insert-koin:koin-logger-slf4j:$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-client-logging-jvm:$ktor_version")
implementation("io.ktor:ktor-server-host-common-jvm:$ktor_version") implementation("io.ktor:ktor-server-host-common-jvm:$ktor_version")
implementation("io.ktor:ktor-server-status-pages-jvm:$ktor_version") implementation("io.ktor:ktor-server-status-pages-jvm:$ktor_version")

View File

@ -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.DeliverPostJob
import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
import dev.usbharu.hideout.plugins.* 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.routing.register
import dev.usbharu.hideout.service.* 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.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.JobQueueParentService
import dev.usbharu.hideout.service.job.KJobJobQueueParentService import dev.usbharu.hideout.service.job.KJobJobQueueParentService
import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService
import dev.usbharu.hideout.service.signature.HttpSignatureVerifyServiceImpl
import dev.usbharu.kjob.exposed.ExposedKJob import dev.usbharu.kjob.exposed.ExposedKJob
import io.ktor.client.* import io.ktor.client.*
import io.ktor.client.engine.cio.* import io.ktor.client.engine.cio.*
@ -30,6 +27,7 @@ import io.ktor.server.application.*
import kjob.core.kjob import kjob.core.kjob
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
import org.koin.ksp.generated.module
import org.koin.ktor.ext.inject import org.koin.ktor.ext.inject
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -59,10 +57,6 @@ fun Application.parent() {
password = property("hideout.database.password") password = property("hideout.database.password")
) )
} }
single<IUserRepository> { UserRepository(get(), get()) }
single<IUserAuthService> { UserAuthService(get()) }
single<HttpSignatureVerifyService> { HttpSignatureVerifyServiceImpl(get()) }
single<JobQueueParentService> { single<JobQueueParentService> {
val kJobJobQueueService = KJobJobQueueParentService(get()) val kJobJobQueueService = KJobJobQueueParentService(get())
kJobJobQueueService.init(emptyList()) kJobJobQueueService.init(emptyList())
@ -79,19 +73,7 @@ fun Application.parent() {
} }
} }
} }
single<ActivityPubFollowService> { ActivityPubFollowServiceImpl(get(), get(), get(), get()) }
single<ActivityPubService> { ActivityPubServiceImpl(get(), get()) }
single<IUserService> { UserService(get(), get()) }
single<ActivityPubUserService> { ActivityPubUserServiceImpl(get(), get()) }
single<ActivityPubNoteService> { ActivityPubNoteServiceImpl(get(), get(), get()) }
single<IPostService> { PostService(get(), get()) }
single<IPostRepository> { PostRepositoryImpl(get(), get()) }
single<IdGenerateService> { TwitterSnowflakeIdGenerateService } single<IdGenerateService> { TwitterSnowflakeIdGenerateService }
single<IMetaRepository> { MetaRepositoryImpl(get()) }
single<IServerInitialiseService> { ServerInitialiseServiceImpl(get()) }
single<IJwtRefreshTokenRepository> { JwtRefreshTokenRepositoryImpl(get(), get()) }
single<IMetaService> { MetaServiceImpl(get()) }
single<IJwtService> { JwtServiceImpl(get(), get(), get()) }
single<JwkProvider> { single<JwkProvider> {
JwkProviderBuilder(Config.configData.url).cached( JwkProviderBuilder(Config.configData.url).cached(
10, 10,
@ -101,7 +83,7 @@ fun Application.parent() {
.rateLimited(10, 1, TimeUnit.MINUTES).build() .rateLimited(10, 1, TimeUnit.MINUTES).build()
} }
} }
configureKoin(module) configureKoin(module, HideoutModule().module)
runBlocking { runBlocking {
inject<IServerInitialiseService>().value.init() inject<IServerInitialiseService>().value.init()
} }

View File

@ -0,0 +1,8 @@
package dev.usbharu.hideout
import org.koin.core.annotation.ComponentScan
import org.koin.core.annotation.Module
@Module
@ComponentScan
class HideoutModule

View File

@ -5,9 +5,9 @@ import org.koin.core.module.Module
import org.koin.ktor.plugin.Koin import org.koin.ktor.plugin.Koin
import org.koin.logger.slf4jLogger import org.koin.logger.slf4jLogger
fun Application.configureKoin(module: Module) { fun Application.configureKoin(vararg module: Module) {
install(Koin) { install(Koin) {
slf4jLogger() slf4jLogger()
modules(module) modules(*module)
} }
} }

View File

@ -7,8 +7,10 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import org.koin.core.annotation.Single
import java.time.Instant import java.time.Instant
@Single
class JwtRefreshTokenRepositoryImpl( class JwtRefreshTokenRepositoryImpl(
private val database: Database, private val database: Database,
private val idGenerateService: IdGenerateService private val idGenerateService: IdGenerateService

View File

@ -5,8 +5,10 @@ import kotlinx.coroutines.Dispatchers
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import org.koin.core.annotation.Single
import java.util.* import java.util.*
@Single
class MetaRepositoryImpl(private val database: Database) : IMetaRepository { class MetaRepositoryImpl(private val database: Database) : IMetaRepository {
init { init {

View File

@ -11,7 +11,9 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import org.koin.core.annotation.Single
@Single
class PostRepositoryImpl(database: Database, private val idGenerateService: IdGenerateService) : IPostRepository { class PostRepositoryImpl(database: Database, private val idGenerateService: IdGenerateService) : IPostRepository {
init { init {

View File

@ -8,8 +8,10 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import org.koin.core.annotation.Single
import java.time.Instant import java.time.Instant
@Single
class UserRepository(private val database: Database, private val idGenerateService: IdGenerateService) : class UserRepository(private val database: Database, private val idGenerateService: IdGenerateService) :
IUserRepository { IUserRepository {
init { init {

View File

@ -14,11 +14,13 @@ import dev.usbharu.hideout.util.RsaUtil
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async import kotlinx.coroutines.async
import org.koin.core.annotation.Single
import java.time.Instant import java.time.Instant
import java.time.temporal.ChronoUnit import java.time.temporal.ChronoUnit
import java.util.* import java.util.*
@Suppress("InjectDispatcher") @Suppress("InjectDispatcher")
@Single
class JwtServiceImpl( class JwtServiceImpl(
private val metaService: IMetaService, private val metaService: IMetaService,
private val refreshTokenRepository: IJwtRefreshTokenRepository, private val refreshTokenRepository: IJwtRefreshTokenRepository,

View File

@ -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.domain.model.hideout.entity.Meta
import dev.usbharu.hideout.exception.NotInitException import dev.usbharu.hideout.exception.NotInitException
import dev.usbharu.hideout.repository.IMetaRepository import dev.usbharu.hideout.repository.IMetaRepository
import org.koin.core.annotation.Single
@Single
class MetaServiceImpl(private val metaRepository: IMetaRepository) : IMetaService { class MetaServiceImpl(private val metaRepository: IMetaRepository) : IMetaService {
override suspend fun getMeta(): Meta = metaRepository.get() ?: throw NotInitException("Meta is null") override suspend fun getMeta(): Meta = metaRepository.get() ?: throw NotInitException("Meta is null")

View File

@ -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.domain.model.hideout.entity.Meta
import dev.usbharu.hideout.repository.IMetaRepository import dev.usbharu.hideout.repository.IMetaRepository
import dev.usbharu.hideout.util.ServerUtil import dev.usbharu.hideout.util.ServerUtil
import org.koin.core.annotation.Single
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.security.KeyPairGenerator import java.security.KeyPairGenerator
import java.util.* import java.util.*
@Single
class ServerInitialiseServiceImpl(private val metaRepository: IMetaRepository) : IServerInitialiseService { class ServerInitialiseServiceImpl(private val metaRepository: IMetaRepository) : IServerInitialiseService {
val logger: Logger = LoggerFactory.getLogger(ServerInitialiseServiceImpl::class.java) val logger: Logger = LoggerFactory.getLogger(ServerInitialiseServiceImpl::class.java)

View File

@ -13,7 +13,9 @@ import dev.usbharu.hideout.service.job.JobQueueParentService
import io.ktor.client.* import io.ktor.client.*
import io.ktor.http.* import io.ktor.http.*
import kjob.core.job.JobProps import kjob.core.job.JobProps
import org.koin.core.annotation.Single
@Single
class ActivityPubFollowServiceImpl( class ActivityPubFollowServiceImpl(
private val jobQueueParentService: JobQueueParentService, private val jobQueueParentService: JobQueueParentService,
private val activityPubUserService: ActivityPubUserService, private val activityPubUserService: ActivityPubUserService,

View File

@ -11,9 +11,11 @@ import dev.usbharu.hideout.service.impl.IUserService
import dev.usbharu.hideout.service.job.JobQueueParentService import dev.usbharu.hideout.service.job.JobQueueParentService
import io.ktor.client.* import io.ktor.client.*
import kjob.core.job.JobProps import kjob.core.job.JobProps
import org.koin.core.annotation.Single
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.time.Instant import java.time.Instant
@Single
class ActivityPubNoteServiceImpl( class ActivityPubNoteServiceImpl(
private val httpClient: HttpClient, private val httpClient: HttpClient,
private val jobQueueParentService: JobQueueParentService, private val jobQueueParentService: JobQueueParentService,

View File

@ -10,9 +10,11 @@ import dev.usbharu.hideout.domain.model.job.ReceiveFollowJob
import dev.usbharu.hideout.exception.JsonParseException import dev.usbharu.hideout.exception.JsonParseException
import kjob.core.dsl.JobContextWithProps import kjob.core.dsl.JobContextWithProps
import kjob.core.job.JobProps import kjob.core.job.JobProps
import org.koin.core.annotation.Single
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@Single
class ActivityPubServiceImpl( class ActivityPubServiceImpl(
private val activityPubFollowService: ActivityPubFollowService, private val activityPubFollowService: ActivityPubFollowService,
private val activityPubNoteService: ActivityPubNoteService private val activityPubNoteService: ActivityPubNoteService

View File

@ -15,7 +15,9 @@ import io.ktor.client.*
import io.ktor.client.request.* import io.ktor.client.request.*
import io.ktor.client.statement.* import io.ktor.client.statement.*
import io.ktor.http.* import io.ktor.http.*
import org.koin.core.annotation.Single
@Single
class ActivityPubUserServiceImpl( class ActivityPubUserServiceImpl(
private val userService: IUserService, private val userService: IUserService,
private val httpClient: HttpClient private val httpClient: HttpClient

View File

@ -4,8 +4,10 @@ import dev.usbharu.hideout.domain.model.Post
import dev.usbharu.hideout.repository.IPostRepository import dev.usbharu.hideout.repository.IPostRepository
import dev.usbharu.hideout.service.IPostService import dev.usbharu.hideout.service.IPostService
import dev.usbharu.hideout.service.activitypub.ActivityPubNoteService import dev.usbharu.hideout.service.activitypub.ActivityPubNoteService
import org.koin.core.annotation.Single
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@Single
class PostService( class PostService(
private val postRepository: IPostRepository, private val postRepository: IPostRepository,
private val activityPubNoteService: ActivityPubNoteService private val activityPubNoteService: ActivityPubNoteService

View File

@ -4,9 +4,11 @@ import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.repository.IUserRepository
import dev.usbharu.hideout.service.IUserAuthService import dev.usbharu.hideout.service.IUserAuthService
import io.ktor.util.* import io.ktor.util.*
import org.koin.core.annotation.Single
import java.security.* import java.security.*
import java.util.* import java.util.*
@Single
class UserAuthService( class UserAuthService(
val userRepository: IUserRepository val userRepository: IUserRepository
) : IUserAuthService { ) : IUserAuthService {

View File

@ -7,9 +7,11 @@ import dev.usbharu.hideout.domain.model.hideout.entity.User
import dev.usbharu.hideout.exception.UserNotFoundException import dev.usbharu.hideout.exception.UserNotFoundException
import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.repository.IUserRepository
import dev.usbharu.hideout.service.IUserAuthService import dev.usbharu.hideout.service.IUserAuthService
import org.koin.core.annotation.Single
import java.lang.Integer.min import java.lang.Integer.min
import java.time.Instant import java.time.Instant
@Single
class UserService(private val userRepository: IUserRepository, private val userAuthService: IUserAuthService) : class UserService(private val userRepository: IUserRepository, private val userAuthService: IUserAuthService) :
IUserService { IUserService {

View File

@ -3,8 +3,10 @@ package dev.usbharu.hideout.service.signature
import dev.usbharu.hideout.plugins.KtorKeyMap import dev.usbharu.hideout.plugins.KtorKeyMap
import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.repository.IUserRepository
import io.ktor.http.* import io.ktor.http.*
import org.koin.core.annotation.Single
import tech.barbero.http.message.signing.SignatureHeaderVerifier import tech.barbero.http.message.signing.SignatureHeaderVerifier
@Single
class HttpSignatureVerifyServiceImpl(private val userAuthService: IUserRepository) : HttpSignatureVerifyService { class HttpSignatureVerifyServiceImpl(private val userAuthService: IUserRepository) : HttpSignatureVerifyService {
override fun verify(headers: Headers): Boolean { override fun verify(headers: Headers): Boolean {
val build = SignatureHeaderVerifier.builder().keyMap(KtorKeyMap(userAuthService)).build() val build = SignatureHeaderVerifier.builder().keyMap(KtorKeyMap(userAuthService)).build()