Refactor config loading and add config from environment

Issue: #1121
This commit is contained in:
eikek
2021-10-24 23:02:39 +02:00
parent 252741dc64
commit 4e5924d796
11 changed files with 339 additions and 110 deletions

View File

@ -8,9 +8,12 @@ package docspell.joex
import cats.data.Validated
import cats.data.ValidatedNec
import cats.effect.Async
import cats.implicits._
import docspell.common.config.Implicits._
import docspell.common.Logger
import docspell.config.ConfigFactory
import docspell.config.Implicits._
import docspell.joex.scheduler.CountingScheme
import emil.MailAddress
@ -22,8 +25,12 @@ import yamusca.imports._
object ConfigFile {
import Implicits._
def loadConfig: Config =
validOrThrow(ConfigSource.default.at("docspell.joex").loadOrThrow[Config])
def loadConfig[F[_]: Async](args: List[String]): F[Config] = {
val logger = Logger.log4s[F](org.log4s.getLogger)
ConfigFactory
.default[F, Config](logger, "docspell.joex")(args)
.map(cfg => validOrThrow(cfg))
}
private def validOrThrow(cfg: Config): Config =
validate(cfg).fold(err => sys.error(err.toList.mkString("- ", "\n", "")), identity)

View File

@ -6,67 +6,45 @@
package docspell.joex
import java.nio.file.{Files, Paths}
import cats.effect._
import cats.implicits._
import docspell.common._
import org.log4s._
import org.log4s.getLogger
object Main extends IOApp {
private[this] val logger = getLogger
val blockingEC =
ThreadFactories.cached[IO](ThreadFactories.ofName("docspell-joex-blocking"))
val connectEC =
private val logger: Logger[IO] = Logger.log4s[IO](getLogger)
private val connectEC =
ThreadFactories.fixed[IO](5, ThreadFactories.ofName("docspell-joex-dbconnect"))
val restserverEC =
ThreadFactories.workSteal[IO](ThreadFactories.ofNameFJ("docspell-joex-server"))
def run(args: List[String]) = {
args match {
case file :: Nil =>
val path = Paths.get(file).toAbsolutePath.normalize
logger.info(s"Using given config file: $path")
System.setProperty("config.file", file)
case _ =>
Option(System.getProperty("config.file")) match {
case Some(f) if f.nonEmpty =>
val path = Paths.get(f).toAbsolutePath.normalize
if (!Files.exists(path)) {
logger.info(s"Not using config file '$f' because it doesn't exist")
System.clearProperty("config.file")
} else
logger.info(s"Using config file from system properties: $f")
case _ =>
}
}
def run(args: List[String]): IO[ExitCode] =
for {
cfg <- ConfigFile.loadConfig[IO](args)
banner = Banner(
"JOEX",
BuildInfo.version,
BuildInfo.gitHeadCommit,
cfg.jdbc.url,
Option(System.getProperty("config.file")),
cfg.appId,
cfg.baseUrl,
Some(cfg.fullTextSearch.solr.url).filter(_ => cfg.fullTextSearch.enabled)
)
_ <- logger.info(s"\n${banner.render("***>")}")
_ <-
if (EnvMode.current.isDev) {
logger.warn(">>>>> Docspell is running in DEV mode! <<<<<")
} else IO(())
val cfg = ConfigFile.loadConfig
val banner = Banner(
"JOEX",
BuildInfo.version,
BuildInfo.gitHeadCommit,
cfg.jdbc.url,
Option(System.getProperty("config.file")),
cfg.appId,
cfg.baseUrl,
Some(cfg.fullTextSearch.solr.url).filter(_ => cfg.fullTextSearch.enabled)
)
logger.info(s"\n${banner.render("***>")}")
if (EnvMode.current.isDev) {
logger.warn(">>>>> Docspell is running in DEV mode! <<<<<")
}
val pools = connectEC.map(Pools.apply)
pools.use(p =>
JoexServer
.stream[IO](cfg, p)
.compile
.drain
.as(ExitCode.Success)
)
}
pools = connectEC.map(Pools.apply)
rc <- pools.use(p =>
JoexServer
.stream[IO](cfg, p)
.compile
.drain
.as(ExitCode.Success)
)
} yield rc
}