Initial application stub

This commit is contained in:
Eike Kettner
2019-07-17 22:03:10 +02:00
commit 6154e6a387
54 changed files with 2447 additions and 0 deletions

View File

@ -0,0 +1,3 @@
docspell.joex {
}

View File

@ -0,0 +1,18 @@
package docspell.joex
import docspell.store.JdbcConfig
case class Config(id: String
, bind: Config.Bind
, jdbc: JdbcConfig
)
object Config {
val default: Config =
Config("testid", Config.Bind("localhost", 7878), JdbcConfig("", "", ""))
case class Bind(address: String, port: Int)
}

View File

@ -0,0 +1,26 @@
package docspell.joex
import cats.effect._
import org.http4s._
import org.http4s.HttpRoutes
import org.http4s.dsl.Http4sDsl
import org.http4s.circe.CirceEntityEncoder._
import docspell.joexapi.model._
import docspell.joex.BuildInfo
object InfoRoutes {
def apply[F[_]: Sync](cfg: Config): HttpRoutes[F] = {
val dsl = new Http4sDsl[F]{}
import dsl._
HttpRoutes.of[F] {
case GET -> (Root / "version") =>
Ok(VersionInfo(BuildInfo.version
, BuildInfo.builtAtMillis
, BuildInfo.builtAtString
, BuildInfo.gitHeadCommit.getOrElse("")
, BuildInfo.gitDescribedVersion.getOrElse("")))
}
}
}

View File

@ -0,0 +1,6 @@
package docspell.joex
trait JoexApp[F[_]] {
def init: F[Unit]
}

View File

@ -0,0 +1,16 @@
package docspell.joex
import cats.effect._
final class JoexAppImpl[F[_]: Sync](cfg: Config) extends JoexApp[F] {
def init: F[Unit] =
Sync[F].pure(())
}
object JoexAppImpl {
def create[F[_]: Sync](cfg: Config): Resource[F, JoexApp[F]] =
Resource.liftF(Sync[F].pure(new JoexAppImpl(cfg)))
}

View File

@ -0,0 +1,38 @@
package docspell.joex
import cats.effect._
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.implicits._
import fs2.Stream
import org.http4s.server.middleware.Logger
import org.http4s.server.Router
object JoexServer {
def stream[F[_]: ConcurrentEffect](cfg: Config)
(implicit T: Timer[F]): Stream[F, Nothing] = {
val app = for {
joexApp <- JoexAppImpl.create[F](cfg)
_ <- Resource.liftF(joexApp.init)
httpApp = Router(
"/api/info" -> InfoRoutes(cfg)
).orNotFound
// With Middlewares in place
finalHttpApp = Logger.httpApp(false, false)(httpApp)
} yield finalHttpApp
Stream.resource(app).flatMap(httpApp =>
BlazeServerBuilder[F]
.bindHttp(cfg.bind.port, cfg.bind.address)
.withHttpApp(httpApp)
.serve
)
}.drain
}

View File

@ -0,0 +1,38 @@
package docspell.joex
import cats.effect.{ExitCode, IO, IOApp}
import cats.implicits._
import scala.concurrent.ExecutionContext
import java.util.concurrent.Executors
import java.nio.file.{Files, Paths}
import org.log4s._
object Main extends IOApp {
private[this] val logger = getLogger
val blockingEc: ExecutionContext = ExecutionContext.fromExecutor(Executors.newCachedThreadPool)
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 _ =>
}
}
val cfg = Config.default
JoexServer.stream[IO](cfg).compile.drain.as(ExitCode.Success)
}
}