/
IndexConfigLoader.scala
42 lines (35 loc) · 1.58 KB
/
IndexConfigLoader.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package tech.beshu.ror.configuration
import io.circe.yaml.parser
import monix.eval.Task
import tech.beshu.ror.configuration.ConfigLoader.{ConfigLoaderError, RawRorConfig}
import tech.beshu.ror.configuration.ConfigLoader.ConfigLoaderError.SpecializedError
import tech.beshu.ror.configuration.IndexConfigLoader.IndexConfigError
import tech.beshu.ror.configuration.IndexConfigLoader.IndexConfigError.{IndexConfigNotExist, InvalidIndexContent}
import tech.beshu.ror.es.IndexContentProvider
class IndexConfigLoader(indexContentProvider: IndexContentProvider)
extends ConfigLoader[IndexConfigError] {
override def load(): Task[Either[ConfigLoaderError[IndexConfigError], RawRorConfig]] =
indexContentProvider
.contentOf(".readonlyrest", "settings", "1")
.map {
case Right(content) =>
parseIndexContent(content).map(RawRorConfig.apply)
case Left(IndexContentProvider.Error.CannotReachContentSource) =>
Left(SpecializedError[IndexConfigError](IndexConfigNotExist))
case Left(IndexContentProvider.Error.ContentNotFound) =>
Left(SpecializedError[IndexConfigError](IndexConfigNotExist))
}
private def parseIndexContent(content: String) = {
parser
.parse(content)
.left.map(ex => SpecializedError(InvalidIndexContent(ex)))
.flatMap { json => validateRorJson(json) }
}
}
object IndexConfigLoader {
sealed trait IndexConfigError
object IndexConfigError {
case object IndexConfigNotExist extends IndexConfigError
final case class InvalidIndexContent(throwable: Throwable) extends IndexConfigError
}
}