move some part of KoinModule in components

This commit is contained in:
2021-01-23 23:26:01 +01:00
parent dd6433306d
commit aa95de7a6a
15 changed files with 219 additions and 160 deletions

View File

@@ -0,0 +1,8 @@
package fr.dcproject.component.article
import org.koin.dsl.module
val articleKoinModule = module {
single { ArticleRepository(get()) }
single { ArticleAccessControl(get()) }
}

View File

@@ -0,0 +1,11 @@
package fr.dcproject.component.auth
import fr.dcproject.application.Configuration
import fr.dcproject.messages.Mailer
import org.koin.dsl.module
val authKoinModule = module {
single { UserRepository(get()) }
// Used to send a connexion link by email
single { PasswordlessAuth(get<Mailer>(), Configuration.domain, get()) }
}

View File

@@ -0,0 +1,8 @@
package fr.dcproject.component.citizen
import org.koin.dsl.module
val citizenKoinModule = module {
single { CitizenRepository(get()) }
single { CitizenAccessControl() }
}

View File

@@ -0,0 +1,14 @@
package fr.dcproject.component.comment
import fr.dcproject.component.comment.article.CommentArticleRepository
import fr.dcproject.component.comment.constitution.CommentConstitutionRepository
import fr.dcproject.component.comment.generic.CommentAccessControl
import fr.dcproject.component.comment.generic.CommentRepository
import org.koin.dsl.module
val commentKoinModule = module {
single { CommentRepository(get()) }
single { CommentArticleRepository(get()) }
single { CommentConstitutionRepository(get()) }
single { CommentAccessControl() }
}

View File

@@ -0,0 +1,8 @@
package fr.dcproject.component.constitution
import org.koin.dsl.module
val constitutionKoinModule = module {
single { ConstitutionRepository(get()) }
single { ConstitutionAccessControl() }
}

View File

@@ -0,0 +1,9 @@
package fr.dcproject.component.follow
import org.koin.dsl.module
val followKoinModule = module {
single { FollowArticleRepository(get()) }
single { FollowConstitutionRepository(get()) }
single { FollowAccessControl() }
}

View File

@@ -0,0 +1,11 @@
package fr.dcproject.component.opinion
import org.koin.dsl.module
val opinionKoinModule = module {
single { OpinionChoiceRepository(get()) }
single { OpinionRepositoryArticle(get()) }
single { OpinionAccessControl() }
single { OpinionChoiceAccessControl() }
}

View File

@@ -1,85 +0,0 @@
package fr.dcproject.component.views
import org.elasticsearch.client.Request
import org.elasticsearch.client.RestClient
import org.slf4j.Logger
import org.slf4j.LoggerFactory
object ConfigViews {
private fun waitElasticsearchIsUp(client: RestClient) {
val logger: Logger = LoggerFactory.getLogger("fr.dcproject.elasticsearch")
val request = Request("GET", "/_cluster/health")
repeat(5 * 60 / 2) { // 5 minutes
runCatching {
client.performRequest(request).statusLine.statusCode
}.onSuccess {
if (it == 200) {
logger.debug("Elasticsearch is Ready! Continue...")
return
} else {
logger.debug("sleep 2s and retry...")
Thread.sleep(2000)
}
}.onFailure {
logger.debug("${it.message}, sleep 2s and retry...")
Thread.sleep(2000)
}
}
error("Elasticsearch is not ready")
}
fun createEsIndexForViews(client: RestClient) {
waitElasticsearchIsUp(client)
/* Create index if not exist */
client.run {
if (performRequest(Request("HEAD", "/views?include_type_name=false")).statusLine.statusCode == 404) {
Request(
"PUT",
"/views?include_type_name=false"
).apply {
//language=JSON
setJsonEntity(
"""
{
"settings": {
"number_of_shards": 5
},
"mappings": {
"properties": {
"logged": {
"type": "boolean"
},
"type": {
"type": "keyword"
},
"user_ref": {
"type": "keyword"
},
"id": {
"type": "keyword"
},
"version_id": {
"type": "keyword"
},
"ip": {
"type": "keyword"
},
"citizen_id": {
"type": "keyword"
},
"view_at": {
"type": "date"
}
}
}
}
""".trimIndent()
)
}.let {
performRequest(it)
}
}
}
}
}

View File

@@ -0,0 +1,19 @@
package fr.dcproject.component.views
import fr.dcproject.application.Configuration
import fr.dcproject.component.article.ArticleForView
import fr.dcproject.component.article.ArticleViewManager
import org.apache.http.HttpHost
import org.elasticsearch.client.RestClient
import org.koin.dsl.module
val viewKoinModule = module {
// Elasticsearch Client
val esClient = RestClient.builder(
HttpHost.create(Configuration.elasticsearch)
).build().apply {
createEsIndexForViews()
}
single { ArticleViewManager<ArticleForView>(esClient) }
}

View File

@@ -0,0 +1,58 @@
package fr.dcproject.component.views
import fr.dcproject.utils.waitElasticsearchIsUp
import org.elasticsearch.client.Request
import org.elasticsearch.client.RestClient
fun RestClient.createEsIndexForViews() {
waitElasticsearchIsUp()
/* Create index if not exist */
if (performRequest(Request("HEAD", "/views?include_type_name=false")).statusLine.statusCode == 404) {
Request(
"PUT",
"/views?include_type_name=false"
).apply {
//language=JSON
setJsonEntity(
"""
{
"settings": {
"number_of_shards": 5
},
"mappings": {
"properties": {
"logged": {
"type": "boolean"
},
"type": {
"type": "keyword"
},
"user_ref": {
"type": "keyword"
},
"id": {
"type": "keyword"
},
"version_id": {
"type": "keyword"
},
"ip": {
"type": "keyword"
},
"citizen_id": {
"type": "keyword"
},
"view_at": {
"type": "date"
}
}
}
}
""".trimIndent()
)
}.let {
performRequest(it)
}
}
}

View File

@@ -0,0 +1,12 @@
package fr.dcproject.component.vote
import org.koin.dsl.module
val voteKoinModule = module {
single { VoteRepository(get()) }
single { VoteArticleRepository(get()) }
single { VoteConstitutionRepository(get()) }
single { VoteCommentRepository(get()) }
single { VoteAccessControl() }
}

View File

@@ -0,0 +1,8 @@
package fr.dcproject.component.workgroup
import org.koin.dsl.module
val workgroupKoinModule = module {
single { WorkgroupRepository(get()) }
single { WorkgroupAccessControl() }
}