mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-21 18:08:25 +00:00
Initial application stub
This commit is contained in:
3
modules/joex/src/main/resources/reference.conf
Normal file
3
modules/joex/src/main/resources/reference.conf
Normal file
@ -0,0 +1,3 @@
|
||||
docspell.joex {
|
||||
|
||||
}
|
18
modules/joex/src/main/scala/docspell/joex/Config.scala
Normal file
18
modules/joex/src/main/scala/docspell/joex/Config.scala
Normal 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)
|
||||
}
|
26
modules/joex/src/main/scala/docspell/joex/InfoRoutes.scala
Normal file
26
modules/joex/src/main/scala/docspell/joex/InfoRoutes.scala
Normal 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("")))
|
||||
}
|
||||
}
|
||||
}
|
6
modules/joex/src/main/scala/docspell/joex/JoexApp.scala
Normal file
6
modules/joex/src/main/scala/docspell/joex/JoexApp.scala
Normal file
@ -0,0 +1,6 @@
|
||||
package docspell.joex
|
||||
|
||||
trait JoexApp[F[_]] {
|
||||
|
||||
def init: F[Unit]
|
||||
}
|
16
modules/joex/src/main/scala/docspell/joex/JoexAppImpl.scala
Normal file
16
modules/joex/src/main/scala/docspell/joex/JoexAppImpl.scala
Normal 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)))
|
||||
}
|
38
modules/joex/src/main/scala/docspell/joex/JoexServer.scala
Normal file
38
modules/joex/src/main/scala/docspell/joex/JoexServer.scala
Normal 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
|
||||
}
|
38
modules/joex/src/main/scala/docspell/joex/Main.scala
Normal file
38
modules/joex/src/main/scala/docspell/joex/Main.scala
Normal 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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user