Skip to content

Commit

Permalink
Go back to Scala 2.13 due to bug
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosedp committed Oct 14, 2022
1 parent 8995c97 commit a59d557
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 48 deletions.
5 changes: 5 additions & 0 deletions backend/src/GreetingApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import zhttp.http.middleware.Cors.CorsConfig
* - Does not use the environment
*/
object GreetingApp {
val corsConfig = CorsConfig(
allowedOrigins = _ == "*",
allowedMethods = Some(Set(Method.PUT, Method.DELETE, Method.POST, Method.GET)),
)

def apply(): Http[Any, Nothing, Request, Response] =
Http.collect[Request] {
// GET /greet?name=:name
Expand Down
30 changes: 15 additions & 15 deletions backend/src/MainApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import zhttp.http.middleware.Cors.CorsConfig
import zhttp.service.Server
import zio._

// Create CORS configuration
private val corsConfig =
CorsConfig(
allowedOrigins = _ == "*",
allowedMethods = Some(Set(Method.PUT, Method.DELETE, Method.POST, Method.GET)),
)

object MainApp extends ZIOAppDefault:
object MainApp extends ZIOAppDefault {
def run = console *> server
val console = Console.printLine(s"Server started on http://localhost:${SharedConfig.serverPort}")

Expand All @@ -25,11 +18,18 @@ object MainApp extends ZIOAppDefault:
http = HomeApp() ++ GreetingApp(),
)

object HomeApp {
def apply(): Http[Any, Nothing, Request, Response] =
Http.collect[Request] {
// GET /, redirect to /greet
case Method.GET -> !! =>
Response.redirect("/greet")
} @@ cors(corsConfig)
object HomeApp {
val corsConfig = CorsConfig(
allowedOrigins = _ == "*",
allowedMethods = Some(Set(Method.PUT, Method.DELETE, Method.POST, Method.GET)),
)

def apply(): Http[Any, Nothing, Request, Response] =
// Create CORS configuration
Http.collect[Request] {
// GET /, redirect to /greet
case Method.GET -> !! =>
Response.redirect("/greet")
} @@ cors(corsConfig)
}
}
76 changes: 43 additions & 33 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import com.goyeau.mill.scalafix.ScalafixModule
import $ivy.`io.github.davidgregory084::mill-tpolecat::0.3.1`
import io.github.davidgregory084.TpolecatModule

import $ivy.`com.carlosedp::mill-docker-nativeimage::0.1-SNAPSHOT`
import $ivy.`io.github.alexarchambault.mill::mill-native-image::0.1.21`
import io.github.alexarchambault.millnativeimage.NativeImage

import $ivy.`com.carlosedp::mill-docker-nativeimage::0.0.1`
import com.carlosedp.milldockernative.DockerNative

object libVersion {
val scala = "3.2.0"
val scala = "2.13.10"
val scalajs = "1.11.0"
val zio = "2.0.2"
val zhttp = "2.0.0-RC11"
Expand All @@ -27,6 +30,8 @@ object libVersion {
trait Common extends ScalaModule with TpolecatModule with ScalafmtModule with ScalafixModule {
override def scalaVersion = libVersion.scala
def scalafixIvyDeps = Agg(ivy"com.github.liancheng::organize-imports:${libVersion.organizeimports}")
override def scalacPluginIvyDeps =
Agg(ivy"org.scalameta:::semanticdb-scalac:4.5.13")
def repositoriesTask = T.task { // Add snapshot repositories in case needed
super.repositoriesTask() ++ Seq("oss", "s01.oss")
.map(r => s"https://$r.sonatype.org/content/repositories/snapshots")
Expand All @@ -44,43 +49,14 @@ trait Common extends ScalaModule with TpolecatModule with ScalafmtModule with Sc

// object shared extends Common

object backend extends Common with DockerModule with DockerNative {
object backend extends Common with DockerModule with DockerNative with NativeImage with NativeImageConfig {
// Runtime dependencies
def ivyDeps = super.ivyDeps() ++ Agg(
ivy"dev.zio::zio:${libVersion.zio}",
ivy"io.d11::zhttp:${libVersion.zhttp}",
)

object dockerNative extends DockerNativeConfig {
def nativeImageName = "backend"
def nativeImageGraalVmJvmId = T {
sys.env.getOrElse("GRAALVM_ID", "graalvm-java17:22.2.0")
}
def nativeImageClassPath = runClasspath()
def nativeImageMainClass = "com.carlosedp.zioscalajs.backend.MainApp"
def nativeImageOptions = super.nativeImageOptions() ++ Seq(
"--no-fallback",
"--enable-url-protocols=http,https",
"-Djdk.http.auth.tunneling.disabledSchemes=",
// "--static", // Does not work on MacOS
"--no-fallback",
"--install-exit-handlers",
"--enable-http",
"--initialize-at-run-time=io.netty.channel.DefaultFileRegion",
"--initialize-at-run-time=io.netty.channel.epoll.Native",
"--initialize-at-run-time=io.netty.channel.epoll.Epoll",
"--initialize-at-run-time=io.netty.channel.epoll.EpollEventLoop",
"--initialize-at-run-time=io.netty.channel.epoll.EpollEventArray",
"--initialize-at-run-time=io.netty.channel.kqueue.KQueue",
"--initialize-at-run-time=io.netty.channel.kqueue.KQueueEventLoop",
"--initialize-at-run-time=io.netty.channel.kqueue.KQueueEventArray",
"--initialize-at-run-time=io.netty.channel.kqueue.Native",
"--initialize-at-run-time=io.netty.channel.unix.Limits",
"--initialize-at-run-time=io.netty.channel.unix.Errors",
"--initialize-at-run-time=io.netty.channel.unix.IovArray",
"--allow-incomplete-classpath",
)

object dockerNative extends DockerNativeConfig with NativeImageConfig {
def tags = List("docker.io/carlosedp/zioscalajs-backend")
def exposedPorts = Seq(8080)
}
Expand All @@ -99,6 +75,40 @@ object backend extends Common with DockerModule with DockerNative {
}
}

trait NativeImageConfig extends JavaModule with NativeImage {
def nativeImageName = "backend"
def nativeImageClassPath = runClasspath()
def nativeImageGraalVmJvmId = T {
sys.env.getOrElse("GRAALVM_ID", "graalvm-java17:22.2.0")
}
// def nativeImageClassPath = runClasspath()
def nativeImageMainClass = "com.carlosedp.zioscalajs.backend.MainApp"
def nativeImageOptionsDef = Seq(
"--no-fallback",
"--enable-url-protocols=http,https",
"-Djdk.http.auth.tunneling.disabledSchemes=",
// "--static", // Does not work on MacOS
"--install-exit-handlers",
"--enable-http",
"--initialize-at-run-time=io.netty.channel.DefaultFileRegion",
"--initialize-at-run-time=io.netty.channel.epoll.Native",
"--initialize-at-run-time=io.netty.channel.epoll.Epoll",
"--initialize-at-run-time=io.netty.channel.epoll.EpollEventLoop",
"--initialize-at-run-time=io.netty.channel.epoll.EpollEventArray",
"--initialize-at-run-time=io.netty.channel.kqueue.KQueue",
"--initialize-at-run-time=io.netty.channel.kqueue.KQueueEventLoop",
"--initialize-at-run-time=io.netty.channel.kqueue.KQueueEventArray",
"--initialize-at-run-time=io.netty.channel.kqueue.Native",
"--initialize-at-run-time=io.netty.channel.unix.Limits",
"--initialize-at-run-time=io.netty.channel.unix.Errors",
"--initialize-at-run-time=io.netty.channel.unix.IovArray",
"--allow-incomplete-classpath",
)
if (System.getProperty("os.name").startsWith("Linux")) {
def nativeImageOptions = nativeImageOptionsDef ++ Seq("--static")
} else nativeImageOptionsDef
}

object frontend extends ScalaJSModule with Common {
def scalaJSVersion = libVersion.scalajs
def ivyDeps = super.ivyDeps() ++ Agg(
Expand Down

0 comments on commit a59d557

Please sign in to comment.