From 11c9330c62941a67405e51c2d243c0d21078b4d9 Mon Sep 17 00:00:00 2001 From: Fabio Pinheiro Date: Thu, 6 Jul 2023 12:52:54 +0100 Subject: [PATCH] feat: fix mongo config when no port & support gzip (#35) Signed-off-by: Fabio Pinheiro Signed-off-by: Shailesh Patil --- build.sbt | 2 +- .../atala/mediator/app/MediatorAgent.scala | 49 ++++++++++++++----- .../mediator/app/MediatorStandalone.scala | 7 +-- project/plugins.sbt | 1 + 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/build.sbt b/build.sbt index d96fe0a2..d2aacd64 100644 --- a/build.sbt +++ b/build.sbt @@ -239,7 +239,7 @@ lazy val mediator = project * all tasks for production, including Scala.js fullOptJS task and source maps scalaJSDev task runs all tasks for * development, including Scala.js fastOptJS task and source maps. */ - Assets / pipelineStages := Seq(scalaJSPipeline), + Assets / pipelineStages := Seq(scalaJSPipeline, gzip), // pipelineStages ++= Seq(digest, gzip), //Compression - If you serve your Scala.js application from a web server, you should additionally gzip the resulting .js files. Compile / unmanagedResourceDirectories += baseDirectory.value / "src" / "main" / "extra-resources", // Compile / unmanagedResourceDirectories += (baseDirectory.value.toPath.getParent.getParent / "docs-build" / "target" / "mdoc").toFile, diff --git a/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorAgent.scala b/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorAgent.scala index 432506a0..bfa1553f 100644 --- a/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorAgent.scala +++ b/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorAgent.scala @@ -219,6 +219,11 @@ object MediatorAgent { def didCommApp = { Http.collectZIO[Request] { + case req @ Method.GET -> !! / "headers" => + val data = req.headersAsList.toSeq.map(e => (e.key.toString(), e.value.toString())) + ZIO.succeed(Response.text("HEADERS:\n" + data.mkString("\n") + "\nRemoteAddress:" + req.remoteAddress)).debug + case req @ Method.GET -> !! / "health" => ZIO.succeed(Response.ok) + case req @ Method.GET -> !! if req.headersAsList.exists { h => h.key.toString.toLowerCase == "content-type" && (h.value.toString.startsWith(MediaTypes.SIGNED.typ) || @@ -273,19 +278,39 @@ object MediatorAgent { Request, Response ] - } ++ Http.fromResource(s"public/webapp-fastopt-library.js").when { - case Method.GET -> !! / "public" / "webapp-fastopt-library.js" => true - case _ => false - } ++ { - Http.fromResource(s"public/webapp-fastopt-bundle.js").when { - case Method.GET -> !! / "public" / path => true - // Response( - // body = Body.fromStream(ZStream.fromIterator(Source.fromResource(s"public/$path").iter).map(_.toByte)), - // headers = Headers(HeaderNames.contentType, HeaderValues.applicationJson), - // ) - case _ => false + } /* ++ Http.fromResource(s"public/webapp-fastopt-bundle.js.gz").when { + case Method.GET -> !! / "public" / "webapp-fastopt-bundle.js.gz" => true + case _ => false + } */ ++ Http + .fromResource(s"public/webapp-fastopt-bundle.js.gz") + .map(e => + e.setHeaders( + Headers( + e.headers.filter(_.key != "content-encoding") ++ Seq( + Header("content-type", "application/javascript"), + Header("content-encoding", "gzip"), + ) + ) + ) + ) + .when { + case Method.GET -> !! / "public" / "webapp-fastopt-bundle.js" => true + case _ => false } - } @@ + // ++ { + // Http + // .fromResource(s"public/webapp-fastopt-bundle.js") + // // .map(e => e.addHeader()) + // .when { + // case Method.GET -> !! / "public" / path => true + // // Response( + // // body = Body.fromStream(ZStream.fromIterator(Source.fromResource(s"public/$path").iter).map(_.toByte)), + // // headers = Headers(HeaderNames.contentType, HeaderValues.applicationJson), + // // ) + // case _ => false + // } + // } + @@ HttpAppMiddleware.cors( zio.http.middleware.Cors.CorsConfig( allowedOrigins = _ => true, diff --git a/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorStandalone.scala b/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorStandalone.scala index 85dd5198..d64a5404 100644 --- a/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorStandalone.scala +++ b/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorStandalone.scala @@ -38,13 +38,14 @@ case class MediatorConfig(endpoint: java.net.URI, keyAgreement: OKPPrivateKey, k case class DataBaseConfig( protocol: String, host: String, - port: String, + port: Option[String], userName: String, password: String, dbName: String ) { - val connectionString = s"$protocol://$userName:$password@$host:$port/$dbName" - val displayConnectionString = s"$protocol://$userName:******@$host:$port/$dbName" + private def maybePort = port.filter(_.nonEmpty).map(":" + _).getOrElse("") + val connectionString = s"$protocol://$userName:$password@$host$maybePort/$dbName" + val displayConnectionString = s"$protocol://$userName:******@$host$maybePort/$dbName" override def toString: String = s"""DataBaseConfig($protocol, $host, $port, $userName, "******", $dbName)""" } diff --git a/project/plugins.sbt b/project/plugins.sbt index d75e9643..251a03b0 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -52,6 +52,7 @@ addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.11.0") // Deploy demo addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.1") +addSbtPlugin("com.typesafe.sbt" % "sbt-gzip" % "1.0.2") // Release addSbtPlugin("com.github.sbt" % "sbt-release" % "1.1.0")