You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.1 KiB
Kotlin

package moe.odango.index
import com.moandjiezana.toml.Toml
import io.inbot.eskotlinwrapper.IndexRepository
import io.requery.Persistable
import io.requery.sql.KotlinConfiguration
import io.requery.sql.KotlinEntityDataStore
import moe.odango.index.config.IndexConfiguration
import moe.odango.index.entity.Models
import moe.odango.index.es.dto.AnimeDTO
import org.apache.http.HttpHost
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestHighLevelClient
import org.elasticsearch.client.indexRepository
import org.kodein.di.*
import org.sqlite.SQLiteDataSource
import java.io.File
import javax.sql.CommonDataSource
val di = DI {
bind<RestHighLevelClient>() with provider {
RestHighLevelClient(RestClient.builder(HttpHost.create(instance<IndexConfiguration>().elastic.host)))
}
bind<IndexRepository<AnimeDTO>>() with provider {
instance<RestHighLevelClient>().indexRepository<AnimeDTO>(instance<IndexConfiguration>().elastic.index)
}
bind<KotlinConfiguration>() with singleton { KotlinConfiguration(Models.DEFAULT, dataSource = instance()) }
bind<KotlinEntityDataStore<Persistable>>() with singleton { KotlinEntityDataStore<Persistable>(instance()) }
constant(tag = "config-file") with mutableListOf(
"/etc/index/config.toml",
System.getenv("HOME") + "/.index/config.toml"
)
bind<IndexConfiguration>() with singleton {
val toml = instance<MutableList<String>>("config-file")
.find { File(it).isFile }
?.let { File(it).readText(Charsets.UTF_8) }
toml?.let {
Toml()
.read(toml)
.to(IndexConfiguration::class.java)
} ?: IndexConfiguration()
}
bind<CommonDataSource>() with singleton {
val config: IndexConfiguration = instance()
when (config.database.driver) {
"sqlite" -> SQLiteDataSource().apply {
url = config.database.uri
}
else -> throw RuntimeException("Database driver '${config.database.driver}' not recognized")
}
}
}