Skip to content

Commit

Permalink
Merge branch 'nob/updates' into 'main'
Browse files Browse the repository at this point in the history
Updating dependencies, Restructure Exporter

See merge request reactivecore/kreuzberg!54
  • Loading branch information
nob13 committed Apr 19, 2024
2 parents 5179514 + e06d284 commit 428ffcf
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 94 deletions.
31 changes: 9 additions & 22 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@ ThisBuild / Test / run / fork := true

ThisBuild / organization := "net.reactivecore"

val zioVersion = "2.0.16"
val zioLoggingVersion = "2.1.14"
val scalatagsVersion = "0.12.0"
val zioVersion = "2.0.22"
val zioLoggingVersion = "2.2.2"
val scalaTagsVersion = "0.12.0"
val zioHttpVersion = "3.0.0-RC2"
val scalatestVersion = "3.2.16"
val logbackVersion = "1.4.7"
val scalaJsDomVersion = "2.6.0"
val scalatestVersion = "3.2.18"
val logbackVersion = "1.5.6"
val scalaJsDomVersion = "2.8.0"
val scalaJsWeakReferencesVersion = "1.0.0"
val scalaJsJavaTimeVersion = "2.5.0"
val scalaXmlVersion = "2.1.0"
val scalaTagsVersion = "0.12.0"
val scalaXmlVersion = "2.2.0"
val circeVersion = "0.14.6"
val tapirVersion = "1.9.11"
val tapirVersion = "1.10.4"
val questVersion = "0.2.0"

val isIntelliJ = {
Expand Down Expand Up @@ -211,19 +210,7 @@ lazy val examples = (crossProject(JSPlatform, JVMPlatform) in file("examples"))
lazy val runner = (project in file("runner"))
.settings(
Compile / compile := (Compile / compile).dependsOn(examples.js / Compile / fastOptJS).value,
Compile / run / mainClass := Some("kreuzberg.examples.showcase.ServerMain"),
reStartArgs := Seq("serve"),
publishArtifact := false,
publish / skip := true,
publishLocal := {},
testSettings
)
.dependsOn(examples.jvm)

lazy val runnerLoom = (project in file("runner-loom"))
.settings(
Compile / compile := (Compile / compile).dependsOn(examples.js / Compile / fastOptJS).value,
Compile / run / mainClass := Some("kreuzberg.examples.showcase.ServerMainLoom"),
Compile / run / mainClass := Some("kreuzberg.examples.showcase.Main"),
reStartArgs := Seq("serve"),
publishArtifact := false,
publish / skip := true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package kreuzberg.examples.showcase

import kreuzberg.examples.showcase.todo.TodoApi
import kreuzberg.miniserver.{AssetCandidatePath, AssetPaths, DeploymentConfig, MiniServerConfig}
import kreuzberg.rpc.{Dispatcher, SecurityError}
import zio.ZIO

val deploymentConfig = DeploymentConfig(
AssetPaths(
Seq(
AssetCandidatePath("examples/js/target/client_bundle/client/fast"),
AssetCandidatePath("../examples/js/target/client_bundle/client/fast"),
AssetCandidatePath("../../examples/js/target/client_bundle/client/fast")
)
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package kreuzberg.examples.showcase

object Exporter extends kreuzberg.miniserver.Exporter(deploymentConfig)
20 changes: 20 additions & 0 deletions examples/jvm/src/main/scala/kreuzberg/examples/showcase/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kreuzberg.examples.showcase

import scala.annotation.experimental

@experimental
object Main {
def main(args: Array[String]): Unit = {
args.headOption match {
case None =>
println("Expected command")
System.exit(1)
case Some("serve") => ServerMain.main(args.tail)
case Some("serveLoom") => ServerMainLoom.main(args.tail)
case Some("export") => Exporter.main(args.tail)
case Some(other) =>
println("Undefined command")
System.exit(1)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@ package kreuzberg.examples.showcase

import kreuzberg.examples.showcase.todo.TodoApi
import kreuzberg.miniserver.*
import kreuzberg.miniserver.ziohttp.Bootstrapper
import kreuzberg.rpc.{Dispatcher, Failure, SecurityError}
import zio.ZIO

import scala.annotation.experimental

@experimental
object ServerMain
extends Bootstrapper(
extends kreuzberg.miniserver.ziohttp.MiniServer(
MiniServerConfig(
AssetPaths(
Seq(
AssetCandidatePath("examples/js/target/client_bundle/client/fast"),
AssetCandidatePath("../examples/js/target/client_bundle/client/fast"),
AssetCandidatePath("../../examples/js/target/client_bundle/client/fast")
)
),
deploymentConfig,
api = Some(ZIO.attempt {
Dispatcher.makeZioDispatcher[TodoApi[zio.Task]](new TodoService).preRequestFlatMap { request =>
// Demonstrating adding a pre filter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,7 @@ object ServerMainLoom extends App {
}

val config = MiniServerConfig[Id](
AssetPaths(
Seq(
AssetCandidatePath("examples/js/target/client_bundle/client/fast"),
AssetCandidatePath("../examples/js/target/client_bundle/client/fast"),
AssetCandidatePath("../../examples/js/target/client_bundle/client/fast")
)
),
deploymentConfig,
api = Some(todoDispatcher)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kreuzberg.miniserver

import kreuzberg.Html
import kreuzberg.miniserver.{AssetPaths, DeploymentType}

/** Deployment related configuration */
case class DeploymentConfig(
assetPaths: AssetPaths,
extraJs: Seq[String] = Nil,
extraCss: Seq[String] = Nil,
extraHtmlHeader: Seq[Html] = Nil,
deploymentType: Option[DeploymentType] = None,
produktionBlacklist: Seq[String] = Seq(
".*\\.js\\.map",
".*\\.css\\.map"
),
noScriptText: Option[String] = None // if not given, use default.
) {
def hashedUrl(name: String): String = assetPaths.hashedUrl(name, deploymentType)

def locateAsset(name: String): Option[Location] = assetPaths.locateAsset(name: String, deploymentType)
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,25 @@
package kreuzberg.miniserver.ziohttp
package kreuzberg.miniserver

import kreuzberg.miniserver.*
import zio.Task

import java.io.File
import java.nio.file.{Files, Path, Paths, StandardCopyOption}
import java.util.jar.{JarEntry, JarFile}
import scala.jdk.CollectionConverters.*
import scala.jdk.StreamConverters.*

class Bootstrapper(config: MiniServerConfig[Task]) {
val help =
"""
|serve - start development server
|export <dir> - start export script
|help - show help
|""".stripMargin

private val blacklistRegexes = config.produktionBlacklist.map(_.r)
/** Exports Index, JS and Assets to a directory */
class Exporter(config: DeploymentConfig) {

final def main(args: Array[String]): Unit = {
args.headOption match {
case Some("serve") =>
new MiniServer(config).main(args.tail)
case Some("export") =>
val dir = args.tail.headOption.getOrElse {
println("Missing directory")
sys.exit(1)
}
exportAll(Paths.get(dir))
case Some("help") =>
println(help)
case None =>
println(help)
case other =>
println(s"Unknown argument ${other}")
sys.exit(1)
case None =>
println(s"Expected directory")
System.exit(1)
case Some(dir) =>
exportAll(Path.of(dir))
}
}

private def exportAll(dir: Path): Unit = {
/** Export everything into given directory. */
def exportAll(dir: Path): Unit = {
println(s"Exporting into ${dir}...")
Files.createDirectories(dir)
val renderedIndex = "<!DOCTYPE html>" + Index(config).index.toString
Expand Down Expand Up @@ -109,6 +89,8 @@ class Bootstrapper(config: MiniServerConfig[Task]) {
Files.copy(inputStream, dstName, StandardCopyOption.REPLACE_EXISTING)
}

private val blacklistRegexes = config.produktionBlacklist.map(_.r)

private def isBlacklisted(s: String): Boolean = {
blacklistRegexes.exists(_.matches(s))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scalatags.Text.all.*
import scalatags.Text.tags2.noscript

/** Index page for MiniServer. */
case class Index(config: MiniServerConfig[_]) {
case class Index(config: DeploymentConfig) {
def index = html(
head(
script(src := config.hashedUrl("main.js")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,8 @@ import kreuzberg.rpc.Dispatcher
* Effect Type, can be Id.
*/
case class MiniServerConfig[F[_]](
assetPaths: AssetPaths,
extraJs: Seq[String] = Nil,
extraCss: Seq[String] = Nil,
extraHtmlHeader: Seq[Html] = Nil,
deployment: DeploymentConfig,
host: String = "0.0.0.0",
port: Int = 8090,
deploymentType: Option[DeploymentType] = None,
produktionBlacklist: Seq[String] = Seq(
".*\\.js\\.map",
".*\\.css\\.map"
),
api: Option[F[Dispatcher[F]]] = None,
noScriptText: Option[String] = None // if not given, use default.
) {
def hashedUrl(name: String): String = assetPaths.hashedUrl(name, deploymentType)

def locateAsset(name: String): Option[Location] = assetPaths.locateAsset(name: String, deploymentType)
}
api: Option[F[Dispatcher[F]]] = None
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ class MiniServer(config: MiniServerConfig[Id]) {
.host(config.host)
.port(config.port)
.addEndpoints(endpoints)
.addEndpoints(if (config.deploymentType.contains(DeploymentType.Debug)) docEndpoints else Nil)
.addEndpoints(if (config.deployment.deploymentType.contains(DeploymentType.Debug)) docEndpoints else Nil)
.start()
logger.info(s"Listening on port ${config.port}")
}

private val indexHtml: String = Index(config).index.toString
private val indexHtml: String = Index(config.deployment).index.toString

val assetEndpoint: PublicEndpoint[List[String], StatusCode, ByteBuffer, Any] = {
endpoint.get
Expand All @@ -46,7 +46,7 @@ class MiniServer(config: MiniServerConfig[Id]) {

private val assetHandler = assetEndpoint.serverLogic[Id] { paths =>
val fullName = paths.mkString("/")
config.locateAsset(fullName) match {
config.deployment.locateAsset(fullName) match {
case None => Left(StatusCode.NotFound)
case Some(value) =>
Using.resource(value.load()) { data =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ class MiniServer(config: MiniServerConfig[Task]) extends ZIOAppDefault {
/** Hook before startup. */
def preflightCheck: Task[Unit] = {
ZIO
.fromOption(config.locateAsset("main.js"))
.fromOption(config.deployment.locateAsset("main.js"))
.mapError { _ =>
val cwd = Paths.get("").toAbsolutePath
new IllegalStateException(
s"Could not find client javascript code, searched in ${config.assetPaths}, working directory=$cwd"
s"Could not find client javascript code, searched in ${config.deployment.assetPaths}, working directory=$cwd"
)
}
.unit
}

private val indexHtml: String = Index(config).index.toString
private val indexHtml: String = Index(config.deployment).index.toString

def assetProvider: HttpApp[Any, Throwable] = Http.collectHttp[Request] {
case Method.GET -> "" /: "assets" /: path =>
config.locateAsset(path.encode) match {
config.deployment.locateAsset(path.encode) match {
case None =>
Http.fromHandler(Handler.notFound)
case Some(Location.File(file)) =>
Expand All @@ -44,7 +44,7 @@ class MiniServer(config: MiniServerConfig[Task]) extends ZIOAppDefault {
case Method.GET -> "" /: path =>
Http.fromHandler(
Handler.html(
Index(config).index.toString
Index(config.deployment).index.toString
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
addSbtPlugin("org.jetbrains.scala" % "sbt-ide-settings" % "1.1.1")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.15.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.17")

addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.2")
Expand Down

0 comments on commit 428ffcf

Please sign in to comment.