Hooking the new pubsub impl into the application

This commit is contained in:
eikek
2021-11-05 21:01:02 +01:00
parent 4d5c695882
commit f38d520a1d
25 changed files with 379 additions and 188 deletions

View File

@ -11,6 +11,7 @@ import docspell.backend.{Config => BackendConfig}
import docspell.common._
import docspell.ftssolr.SolrConfig
import docspell.oidc.ProviderConfig
import docspell.pubsub.naive.PubSubConfig
import docspell.restserver.Config.OpenIdConfig
import docspell.restserver.auth.OpenId
@ -33,6 +34,9 @@ case class Config(
) {
def openIdEnabled: Boolean =
openid.exists(_.enabled)
def pubSubConfig: PubSubConfig =
PubSubConfig(appId, baseUrl / "internal" / "pubsub", 100)
}
object Config {

View File

@ -10,8 +10,6 @@ import docspell.backend.BackendApp
trait RestApp[F[_]] {
def init: F[Unit]
def config: Config
def backend: BackendApp[F]

View File

@ -6,41 +6,52 @@
package docspell.restserver
import scala.concurrent.ExecutionContext
import cats.effect._
import cats.implicits._
import docspell.backend.BackendApp
import docspell.common.NodeType
import docspell.backend.msg.{JobDone, Ping}
import docspell.common.Logger
import docspell.ftsclient.FtsClient
import docspell.ftssolr.SolrFtsClient
import docspell.pubsub.api.{PubSub, PubSubT}
import docspell.store.Store
import org.http4s.client.Client
final class RestAppImpl[F[_]](val config: Config, val backend: BackendApp[F])
extends RestApp[F] {
def init: F[Unit] =
backend.node.register(config.appId, NodeType.Restserver, config.baseUrl)
def shutdown: F[Unit] =
backend.node.unregister(config.appId)
}
extends RestApp[F] {}
object RestAppImpl {
def create[F[_]: Async](
cfg: Config,
connectEC: ExecutionContext
): Resource[F, RestApp[F]] =
store: Store[F],
httpClient: Client[F],
pubSub: PubSub[F]
): Resource[F, RestApp[F]] = {
val logger = Logger.log4s(org.log4s.getLogger(s"restserver-${cfg.appId.id}"))
for {
backend <- BackendApp(cfg.backend, connectEC)(
createFtsClient[F](cfg)
)
ftsClient <- createFtsClient(cfg)(httpClient)
pubSubT = PubSubT(pubSub, logger)
backend <- BackendApp.create[F](cfg.backend, store, ftsClient, pubSubT)
_ <- Resource.eval(subscriptions(backend, logger))
app = new RestAppImpl[F](cfg, backend)
appR <- Resource.make(app.init.map(_ => app))(_.shutdown)
} yield appR
} yield app
}
private def subscriptions[F[_]: Async](
backend: BackendApp[F],
logger: Logger[F]
): F[Unit] =
for {
_ <- Async[F].start(backend.pubSub.subscribeSink(Ping.topic) { msg =>
logger.info(s">>>> PING $msg")
})
_ <- Async[F].start(backend.pubSub.subscribeSink(JobDone.topic) { msg =>
logger.info(s">>>> Job Done $msg")
})
} yield ()
private def createFtsClient[F[_]: Async](
cfg: Config

View File

@ -11,12 +11,15 @@ import cats.implicits._
import fs2.Stream
import docspell.backend.auth.{AuthToken, ShareToken}
import docspell.backend.msg.Topics
import docspell.common._
import docspell.oidc.CodeFlowRoutes
import docspell.pubsub.naive.NaivePubSub
import docspell.restserver.auth.OpenId
import docspell.restserver.http4s.EnvMiddleware
import docspell.restserver.routes._
import docspell.restserver.webapp._
import docspell.store.Store
import org.http4s._
import org.http4s.blaze.client.BlazeClientBuilder
@ -34,9 +37,17 @@ object RestServer {
val templates = TemplateRoutes[F](cfg)
val app = for {
restApp <- RestAppImpl.create[F](cfg, pools.connectEC)
store <- Store.create[F](
cfg.backend.jdbc,
cfg.backend.files.chunkSize,
pools.connectEC
)
httpClient <- BlazeClientBuilder[F].resource
pubSub <- NaivePubSub(cfg.pubSubConfig, store, httpClient)(Topics.all.map(_.topic))
restApp <- RestAppImpl.create[F](cfg, store, httpClient, pubSub)
httpClient <- BlazeClientBuilder[F].resource
httpApp = Router(
"/internal/pubsub" -> pubSub.receiveRoute,
"/api/info" -> routes.InfoRoutes(),
"/api/v1/open/" -> openRoutes(cfg, httpClient, restApp),
"/api/v1/sec/" -> Authenticate(restApp.backend.login, cfg.auth) { token =>