Skip to content

Commit

Permalink
Environment-based configuration of DeriveClient (#22)
Browse files Browse the repository at this point in the history
* Add support for DeriveClient host configuration

* Remove some methods from FrontendUtils from a previous approach

* Add zio.app namespace to serverhost config value

* Update versions and add version vals to build.sbt

* Remove nativeImageVersion from build.sbt

* Alphabetize version vals

* Revert quillZioVersion and update deploy.sh

* Temporarily changing git info in TemplateGenerator for testing

* Remove leftover debugging code, revert TemplateGenerator

* Rename zio.app.serverhost to more accurately named zio.app.backendUri

* Push "api" to dev/application.conf
  • Loading branch information
forkedcancel authored Aug 9, 2021
1 parent 2e55fbb commit d14cf08
Show file tree
Hide file tree
Showing 16 changed files with 258 additions and 80 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.idea/
target/
node_modules
cli/src/main/resources/dist/
65 changes: 45 additions & 20 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import BuildEnvPlugin.autoImport
import BuildEnvPlugin.autoImport.BuildEnv
import xerial.sbt.Sonatype.autoImport.sonatypeCredentialHost

import java.io.InputStream

lazy val scala213 = "2.13.6"
lazy val scala3 = "3.0.0"

Expand All @@ -26,21 +30,23 @@ inThisBuild(

lazy val supportedScalaVersions = List(scala213)

val animusVersion = "0.1.7"
val boopickleVerison = "1.3.2"
val fansiVersion = "0.2.14"
val laminarVersion = "0.13.0"
val laminextVersion = "0.13.5"
val postgresVersion = "42.2.20"
val sttpVersion = "3.3.6"
val zioHttpVersion = "1.0.0.0-RC17"
val zioJsonVersion = "0.1.5"
val zioMagicVersion = "0.3.3"
val zioNioVersion = "1.0.0-RC11"
val zioProcessVersion = "0.4.0"
val zioVersion = "1.0.9"
val zioQueryVersion = "0.2.9"
val quillVersion = "3.7.0"
val animusVersion = "0.1.9"
val boopickleVerison = "1.3.2"
val fansiVersion = "0.2.14"
val laminarVersion = "0.13.1"
val laminextVersion = "0.13.10"
val postgresVersion = "42.2.23"
val quillVersion = "3.7.2"
val scalaJavaTimeVersion = "2.3.0"
val shoconVersion = "1.0.0"
val sttpVersion = "3.3.13"
val zioHttpVersion = "1.0.0.0-RC17"
val zioJsonVersion = "0.1.5"
val zioMagicVersion = "0.3.6"
val zioNioVersion = "1.0.0-RC11"
val zioProcessVersion = "0.4.0"
val zioVersion = "1.0.10"
val zioQueryVersion = "0.2.9"

val sharedSettings = Seq(
addCompilerPlugin("org.typelevel" %% "kind-projector" % "0.13.0" cross CrossVersion.full),
Expand Down Expand Up @@ -138,8 +144,8 @@ lazy val cliFrontend = project
libraryDependencies ++= Seq(
"io.github.kitlangton" %%% "animus" % animusVersion,
"com.raquo" %%% "laminar" % laminarVersion,
"io.github.cquiroz" %%% "scala-java-time" % "2.3.0",
"io.github.cquiroz" %%% "scala-java-time-tzdb" % "2.3.0",
"io.github.cquiroz" %%% "scala-java-time" % scalaJavaTimeVersion,
"io.github.cquiroz" %%% "scala-java-time-tzdb" % scalaJavaTimeVersion,
"io.laminext" %%% "websocket" % laminextVersion,
"com.softwaremill.sttp.client3" %%% "core" % sttpVersion,
"com.softwaremill.sttp.client3" %%% "monix" % sttpVersion
Expand Down Expand Up @@ -192,6 +198,7 @@ lazy val coreJS = core.js
lazy val coreJVM = core.jvm

lazy val examples = crossProject(JSPlatform, JVMPlatform)
.enablePlugins(ShoconPlugin)
.in(file("examples"))
.settings(
name := "zio-app-examples",
Expand Down Expand Up @@ -220,9 +227,27 @@ lazy val examples = crossProject(JSPlatform, JVMPlatform)
scalaJSUseMainModuleInitializer := true,
libraryDependencies ++= Seq(
"com.raquo" %%% "laminar" % laminarVersion,
"io.github.cquiroz" %%% "scala-java-time" % "2.3.0",
"io.github.cquiroz" %%% "scala-java-time-tzdb" % "2.3.0"
)
"io.github.cquiroz" %%% "scala-java-time" % scalaJavaTimeVersion,
"io.github.cquiroz" %%% "scala-java-time-tzdb" % scalaJavaTimeVersion,
"org.akka-js" %%% "shocon" % shoconVersion
),
(Compile / compile) := (Compile / compile).dependsOn(shoconConcat).value,
shoconConcatFile := {
autoImport.buildEnv.value match {
case BuildEnv.Production =>
(Compile / packageBin / artifactPath).value / "zio-app-examples-opt/shocon.conf"
case _ =>
(Compile / packageBin / artifactPath).value / "shocon.conf"
}
},
shoconFilter := {
autoImport.buildEnv.value match {
case BuildEnv.Production =>
tuple: (String, InputStream) => tuple._1.contains("resources/prod")
case _ =>
tuple: (String, InputStream) => tuple._1.contains("resources/dev")
}
}
)
.dependsOn(core)

Expand Down
60 changes: 45 additions & 15 deletions cli/src/main/g8/build.sbt
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import BuildEnvPlugin.autoImport
import BuildEnvPlugin.autoImport.BuildEnv

import java.io.InputStream

name := "$name$"
description := "$description$"
version := "0.0.1"

val animusVersion = "0.1.9"
val laminarVersion = "0.13.0"
val quillZioVersion = "3.7.1"
val sttpVersion = "3.3.6"
val zioAppVersion = "0.2.5"
val zioConfigVersion = "1.0.6"
val zioHttpVersion = "1.0.0.0-RC17"
val zioJsonVersion = "0.1.5"
val zioMagicVersion = "0.3.3"
val zioVersion = "1.0.9"
val animusVersion = "0.1.9"
val boopickleVerison = "1.3.2"
val laminarVersion = "0.13.1"
val laminextVersion = "0.13.10"
val postgresVersion = "42.2.23"
val quillZioVersion = "3.7.2"
val scalaJavaTimeVersion = "2.3.0"
val shoconVersion = "1.0.0"
val sttpVersion = "3.3.13"
val zioAppVersion = "0.2.6"
val zioConfigVersion = "1.0.6"
val zioHttpVersion = "1.0.0.0-RC17"
val zioJsonVersion = "0.1.5"
val zioMagicVersion = "0.3.6"
val zioVersion = "1.0.10"

val sharedSettings = Seq(
addCompilerPlugin("org.typelevel" %% "kind-projector" % "0.13.0" cross CrossVersion.full),
Expand All @@ -23,7 +33,7 @@ val sharedSettings = Seq(
),
libraryDependencies ++= Seq(
"io.github.kitlangton" %% "zio-app" % zioAppVersion,
"io.suzaku" %%% "boopickle" % "1.3.2",
"io.suzaku" %%% "boopickle" % boopickleVerison,
"dev.zio" %%% "zio" % zioVersion,
"dev.zio" %%% "zio-streams" % zioVersion,
"dev.zio" %%% "zio-macros" % zioVersion,
Expand All @@ -50,27 +60,47 @@ lazy val backend = project
"dev.zio" %% "zio-config-magnolia" % zioConfigVersion,
"io.d11" %% "zhttp" % zioHttpVersion,
"com.softwaremill.sttp.client3" %% "httpclient-backend-zio" % sttpVersion,
"org.postgresql" % "postgresql" % "42.2.8",
"org.postgresql" % "postgresql" % postgresVersion,
"io.getquill" %% "quill-jdbc-zio" % quillZioVersion
)
)
.dependsOn(shared)

lazy val frontend = project
.in(file("frontend"))
.enablePlugins(ScalaJSPlugin)
.enablePlugins(ScalaJSPlugin, ShoconPlugin)
.settings(
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) },
scalaJSLinkerConfig ~= { _.withSourceMap(false) },
scalaJSUseMainModuleInitializer := true,
libraryDependencies ++= Seq(
"io.github.kitlangton" %%% "animus" % animusVersion,
"com.raquo" %%% "laminar" % laminarVersion,
"io.github.cquiroz" %%% "scala-java-time" % "2.2.1",
"io.laminext" %%% "websocket" % "0.13.5"
"io.github.cquiroz" %%% "scala-java-time" % scalaJavaTimeVersion,
"io.laminext" %%% "websocket" % laminextVersion,
"org.akka-js" %%% "shocon" % shoconVersion
)
)
.settings(sharedSettings)
.settings(
(Compile / compile) := (Compile / compile).dependsOn(shoconConcat).value,
shoconConcatFile := {
autoImport.buildEnv.value match {
case BuildEnv.Production =>
(Compile / packageBin / artifactPath).value / "prod/shocon.conf"
case _ =>
(Compile / packageBin / artifactPath).value / "dev/shocon.conf"
}
},
shoconFilter := {
autoImport.buildEnv.value match {
case BuildEnv.Production =>
tuple: (String, InputStream) => tuple._1.contains("resources/prod")
case _ =>
tuple: (String, InputStream) => tuple._1.contains("resources/dev")
}
}
)
.dependsOn(shared)

lazy val shared = project
Expand Down
5 changes: 3 additions & 2 deletions cli/src/main/g8/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
sbt frontend/fullLinkJS
BUILD_ENV=prod sbt frontend/fullLinkJS
yarn exec vite -- build
cp dist/index.html dist/200.html
surge ./dist '$name$.surge.sh'
cp frontend/target/scala-2.13/frontend*jar/prod/shocon.conf dist/assets/shocon.conf
surge ./dist '$name$.surge.sh'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zio.app.backendUri = "api"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zio.app.backendUri = "https://changeme.wrong"
44 changes: 44 additions & 0 deletions cli/src/main/g8/project/BuildEnvPlugin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import sbt.Keys._
import sbt._
import sbt.plugins.JvmPlugin

/** sets the build environment */
object BuildEnvPlugin extends AutoPlugin {

// make sure it triggers automatically
override def trigger = AllRequirements
override def requires = JvmPlugin

object autoImport {
object BuildEnv extends Enumeration {
val Production, Test, Development = Value
}

val buildEnv = settingKey[BuildEnv.Value]("the current build environment")
}
import autoImport._

override def projectSettings: Seq[Setting[_]] = Seq(
buildEnv := {
sys.props
.get("env")
.orElse(sys.env.get("BUILD_ENV"))
.flatMap {
case "prod" => Some(BuildEnv.Production)
case "test" => Some(BuildEnv.Test)
case "dev" => Some(BuildEnv.Development)
case _ => None
}
.getOrElse(BuildEnv.Development)
},
// give feed back
onLoadMessage := {
// depend on the old message as well
val defaultMessage = onLoadMessage.value
val env = buildEnv.value
s"""|$defaultMessage
|Running in build environment: $env""".stripMargin
}
)

}
3 changes: 2 additions & 1 deletion cli/src/main/g8/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.6.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.7.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.8.1")
addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")
addSbtPlugin("org.akka-js" % "sbt-shocon" % "1.0.0")
25 changes: 15 additions & 10 deletions core/js/src/main/scala/zio/app/FrontendUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ object FrontendUtils {
implicit val exPickler: CompositePickler[Throwable] = exceptionPickler

private val sttpBackend =
FetchZioBackend(fetchOptions = FetchOptions(credentials = None, mode = Some(RequestMode.`same-origin`)))
FetchZioBackend(fetchOptions = FetchOptions(credentials = None, mode = Some(RequestMode.cors)))

def fetch[E: Pickler, A: Pickler](service: String, method: String): IO[E, A] =
fetchRequest[E, A](bytesRequest.get(uri"api/$service/$method"))
def fetch[E: Pickler, A: Pickler](uriPrefix: String, service: String, method: String): IO[E, A] =
fetchRequest[E, A](bytesRequest.get(uri"$uriPrefix/$service/$method"))

def fetch[E: Pickler, A: Pickler](service: String, method: String, value: ByteBuffer): IO[E, A] =
fetchRequest[E, A](bytesRequest.post(uri"api/$service/$method").body(value))
def fetch[E: Pickler, A: Pickler](uriPrefix: String, service: String, method: String, value: ByteBuffer): IO[E, A] =
fetchRequest[E, A](bytesRequest.post(uri"$uriPrefix/$service/$method").body(value))

def fetchRequest[E: Pickler, A: Pickler](request: Request[Array[Byte], Any]): IO[E, A] =
sttpBackend
Expand All @@ -40,22 +40,27 @@ object FrontendUtils {
}
}

def fetchStream[E: Pickler, A: Pickler](service: String, method: String): Stream[E, A] =
def fetchStream[E: Pickler, A: Pickler](uriPrefix: String, service: String, method: String): Stream[E, A] = {
ZStream
.unwrap {
basicRequest
.get(uri"api/$service/$method")
.get(uri"$uriPrefix/$service/$method")
.response(asStreamAlwaysUnsafe(ZioStreams))
.send(sttpBackend)
.orDie
.map(resp => transformZioResponseStream[E, A](resp.body))
}
}

def fetchStream[E: Pickler, A: Pickler](service: String, method: String, value: ByteBuffer): Stream[E, A] =
ZStream
def fetchStream[E: Pickler, A: Pickler](
uriPrefix: String,
service: String,
method: String,
value: ByteBuffer
): Stream[E, A] = ZStream
.unwrap {
basicRequest
.post(uri"api/$service/$method")
.post(uri"$uriPrefix/$service/$method")
.body(value)
.response(asStreamAlwaysUnsafe(ZioStreams))
.send(sttpBackend)
Expand Down
Loading

0 comments on commit d14cf08

Please sign in to comment.