From a3c387dedb110fabd44db940c5fcd66c95354585 Mon Sep 17 00:00:00 2001 From: M-Vamshi <21211a05f1@bvrit.ac.in> Date: Mon, 1 Jul 2024 16:14:01 +0530 Subject: [PATCH 01/13] fix Port already bound Error --- build.sbt | 1 + .../scala/dev/zio/quickstart/MainApp.scala | 40 ++++++++++++--- .../scala/dev/zio/quickstart/MainApp.scala | 44 ++++++++++++---- .../scala/dev/zio/quickstart/MainApp.scala | 48 +++++++++++++----- .../scala/dev/zio/quickstart/MainApp.scala | 50 ++++++++++++++----- .../scala/dev/zio/quickstart/MainApp.scala | 44 ++++++++++++---- 6 files changed, 174 insertions(+), 53 deletions(-) diff --git a/build.sbt b/build.sbt index 509d01a..1143369 100644 --- a/build.sbt +++ b/build.sbt @@ -41,6 +41,7 @@ lazy val root = `zio-quickstart-sql` ) .settings( + run / fork := true, testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework") ) diff --git a/zio-quickstart-restful-webservice-custom-logger/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-custom-logger/src/main/scala/dev/zio/quickstart/MainApp.scala index 2a62147..f8a5d68 100644 --- a/zio-quickstart-restful-webservice-custom-logger/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-custom-logger/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -5,22 +5,46 @@ import zio._ import zio.http._ import zio.logging.LogFormat import zio.logging.backend.SLF4J +import zio.Console._ +import scala.util.Random object MainApp extends ZIOAppDefault { override val bootstrap: ZLayer[Any, Nothing, Unit] = SLF4J.slf4j(LogFormat.colored) def run = { - Server - .serve(UserRoutes()) - .provide( - Server.defaultWithPort(8080), + val retries = 5 + val portRangeStart = 8000 + val portRangeEnd = 8005 - // An layer responsible for storing the state of the `counterApp` - ZLayer.fromZIO(Ref.make(0)), + def allocatePort(retriesLeft: Int): ZIO[Any, Throwable, Unit] = + if (retriesLeft <= 0) { + ZIO.fail( + new RuntimeException( + "Failed to allocate a port within the retry limit" + ) + ) + } else { + val randomPort = + Random.nextInt(portRangeEnd - portRangeStart + 1) + portRangeStart + Server + .serve(UserRoutes()) + .provide( + Server.defaultWithPort(randomPort), + + // A layer responsible for storing the state of the `counterApp` + ZLayer.fromZIO(Ref.make(0)), - // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead - InmemoryUserRepo.layer + // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead + InmemoryUserRepo.layer + ) + .catchAll(_ => allocatePort(retriesLeft - 1)) + } + + allocatePort(retries) + .fold( + ex => ExitCode.failure, + _ => ExitCode.success ) } } diff --git a/zio-quickstart-restful-webservice-dockerize/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-dockerize/src/main/scala/dev/zio/quickstart/MainApp.scala index e79420d..5512262 100644 --- a/zio-quickstart-restful-webservice-dockerize/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-dockerize/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -6,21 +6,45 @@ import dev.zio.quickstart.greet.GreetingRoutes import dev.zio.quickstart.users.{InmemoryUserRepo, UserRoutes} import zio._ import zio.http._ +import zio.Console._ +import scala.util.Random object MainApp extends ZIOAppDefault { def run = { - Server - .serve( - GreetingRoutes() ++ DownloadRoutes() ++ CounterRoutes() ++ UserRoutes() - ) - .provide( - Server.defaultWithPort(8080), + val retries = 5 + val portRangeStart = 8000 + val portRangeEnd = 8005 + + def allocatePort(retriesLeft: Int): ZIO[Any, Throwable, Unit] = + if (retriesLeft <= 0) { + ZIO.fail( + new RuntimeException( + "Failed to allocate a port within the retry limit" + ) + ) + } else { + val randomPort = + Random.nextInt(portRangeEnd - portRangeStart + 1) + portRangeStart + Server + .serve( + GreetingRoutes() ++ DownloadRoutes() ++ CounterRoutes() ++ UserRoutes() + ) + .provide( + Server.defaultWithPort(randomPort), - // An layer responsible for storing the state of the `counterApp` - ZLayer.fromZIO(Ref.make(0)), + // A layer responsible for storing the state of the `counterApp` + ZLayer.fromZIO(Ref.make(0)), + + // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead + InmemoryUserRepo.layer + ) + .catchAll(_ => allocatePort(retriesLeft - 1)) + } - // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead - InmemoryUserRepo.layer + allocatePort(retries) + .fold( + ex => ExitCode.failure, + _ => ExitCode.success ) } } diff --git a/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala index 766cbd7..8cb85e1 100644 --- a/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -6,22 +6,46 @@ import dev.zio.quickstart.greet.GreetingApp import dev.zio.quickstart.users.{InmemoryUserRepo, PersistentUserRepo, UserApp} import zio._ import zio.http._ +import scala.util.Random +import zio.Console._ object MainApp extends ZIOAppDefault { def run = { - val httpApps = GreetingApp() ++ DownloadApp() ++ CounterApp() ++ UserApp() - Server - .serve( - httpApps.withDefaultErrorResponse - ) - .provide( - Server.defaultWithPort(8080), + val retries = 5 + val portRangeStart = 8000 + val portRangeEnd = 8005 + + def allocatePort(retriesLeft: Int): ZIO[Any, Throwable, Unit] = + if (retriesLeft <= 0) { + ZIO.fail( + new RuntimeException( + "Failed to allocate a port within the retry limit" + ) + ) + } else { + val randomPort = + Random.nextInt(portRangeEnd - portRangeStart + 1) + portRangeStart + Server + .serve( + (GreetingApp() ++ DownloadApp() ++ CounterApp() ++ UserApp()).withDefaultErrorResponse + ) + .provide( + Server.defaultWithPort(randomPort), - // An layer responsible for storing the state of the `counterApp` - ZLayer.fromZIO(Ref.make(0)), + // A layer responsible for storing the state of the `counterApp` + ZLayer.fromZIO(Ref.make(0)), + + // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead + InmemoryUserRepo.layer + ) *> Console + .printLine("Server started on http://localhost:") + .catchAll(_ => allocatePort(retriesLeft - 1)) + } - // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead - InmemoryUserRepo.layer - ) *> Console.printLine("Server started on http://localhost:8080") + allocatePort(retries) + .fold( + ex => ExitCode.failure, + _ => ExitCode.success + ) } } diff --git a/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala index c5bb9a0..d70798a 100644 --- a/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -5,28 +5,52 @@ import dev.zio.quickstart.users._ import zio._ import zio.http._ import zio.metrics.connectors.{MetricsConfig, prometheus} +import zio.Console._ +import scala.util.Random object MainApp extends ZIOAppDefault { private val metricsConfig = ZLayer.succeed(MetricsConfig(5.seconds)) def run = { - Server - .serve(UserRoutes() ++ PrometheusPublisherApp()) - .provide( - Server.defaultWithPort(8080), + val retries = 5 + val portRangeStart = 8000 + val portRangeEnd = 8005 - // An layer responsible for storing the state of the `counterApp` - ZLayer.fromZIO(Ref.make(0)), + def allocatePort(retriesLeft: Int): ZIO[Any, Throwable, Unit] = + if (retriesLeft <= 0) { + ZIO.fail( + new RuntimeException( + "Failed to allocate a port within the retry limit" + ) + ) + } else { + val randomPort = + Random.nextInt(portRangeEnd - portRangeStart + 1) + portRangeStart + Server + .serve(UserRoutes() ++ PrometheusPublisherApp()) + .provide( + Server.defaultWithPort(randomPort), - // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead - InmemoryUserRepo.layer, + // A layer responsible for storing the state of the `counterApp` + ZLayer.fromZIO(Ref.make(0)), - // general config for all metric backend - metricsConfig, + // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead + InmemoryUserRepo.layer, + + // General config for all metric backend + metricsConfig, + + // The Prometheus reporting layer + prometheus.publisherLayer, + prometheus.prometheusLayer + ) + .catchAll(_ => allocatePort(retriesLeft - 1)) + } - // The prometheus reporting layer - prometheus.publisherLayer, - prometheus.prometheusLayer + allocatePort(retries) + .fold( + ex => ExitCode.failure, + _ => ExitCode.success ) } } diff --git a/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala index 8f26258..5f3e41c 100644 --- a/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -9,20 +9,44 @@ import dev.zio.quickstart.users.{ UserRoutes } import zio._ +import zio.Console._ import zio.http._ +import scala.util.Random object MainApp extends ZIOAppDefault: def run = - Server - .serve( - GreetingRoutes() ++ DownloadRoutes() ++ CounterRoutes() ++ UserRoutes() - ) - .provide( - Server.defaultWithPort(8080), + val retries = 5 + val portRangeStart = 8000 + val portRangeEnd = 8005 + + def allocatePort(retriesLeft: Int): ZIO[Any, Throwable, Unit] = + if (retriesLeft <= 0) { + ZIO.fail( + new RuntimeException( + "Failed to allocate a port within the retry limit" + ) + ) + } else { + val randomPort = + Random.nextInt(portRangeEnd - portRangeStart + 1) + portRangeStart + Server + .serve( + GreetingRoutes() ++ DownloadRoutes() ++ CounterRoutes() ++ UserRoutes() + ) + .provide( + Server.defaultWithPort(randomPort), + + // An layer responsible for storing the state of the `counterApp` + ZLayer.fromZIO(Ref.make(0)), - // An layer responsible for storing the state of the `counterApp` - ZLayer.fromZIO(Ref.make(0)), + // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead + InmemoryUserRepo.layer + ) + .catchAll(_ => allocatePort(retriesLeft - 1)) + } - // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead - InmemoryUserRepo.layer + allocatePort(retries) + .fold( + ex => ExitCode.failure, + _ => ExitCode.success ) From 2148c92d1539d28264b4a4d830c26c30969168ec Mon Sep 17 00:00:00 2001 From: M-Vamshi <21211a05f1@bvrit.ac.in> Date: Mon, 1 Jul 2024 18:51:07 +0530 Subject: [PATCH 02/13] fix fmt --- .../src/main/scala/dev/zio/quickstart/MainApp.scala | 2 +- .../src/main/scala/dev/zio/quickstart/MainApp.scala | 2 +- .../src/main/scala/dev/zio/quickstart/MainApp.scala | 2 +- .../src/main/scala/dev/zio/quickstart/MainApp.scala | 4 ++-- .../src/main/scala/dev/zio/quickstart/MainApp.scala | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/zio-quickstart-restful-webservice-custom-logger/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-custom-logger/src/main/scala/dev/zio/quickstart/MainApp.scala index f8a5d68..f2a0a1d 100644 --- a/zio-quickstart-restful-webservice-custom-logger/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-custom-logger/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -31,7 +31,7 @@ object MainApp extends ZIOAppDefault { .serve(UserRoutes()) .provide( Server.defaultWithPort(randomPort), - + // A layer responsible for storing the state of the `counterApp` ZLayer.fromZIO(Ref.make(0)), diff --git a/zio-quickstart-restful-webservice-dockerize/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-dockerize/src/main/scala/dev/zio/quickstart/MainApp.scala index 5512262..86f37b4 100644 --- a/zio-quickstart-restful-webservice-dockerize/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-dockerize/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -34,7 +34,7 @@ object MainApp extends ZIOAppDefault { // A layer responsible for storing the state of the `counterApp` ZLayer.fromZIO(Ref.make(0)), - + // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead InmemoryUserRepo.layer ) diff --git a/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala index 8cb85e1..15fb6d2 100644 --- a/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -34,7 +34,7 @@ object MainApp extends ZIOAppDefault { // A layer responsible for storing the state of the `counterApp` ZLayer.fromZIO(Ref.make(0)), - + // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead InmemoryUserRepo.layer ) *> Console diff --git a/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala index d70798a..3840b11 100644 --- a/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -36,10 +36,10 @@ object MainApp extends ZIOAppDefault { // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead InmemoryUserRepo.layer, - + // General config for all metric backend metricsConfig, - + // The Prometheus reporting layer prometheus.publisherLayer, prometheus.prometheusLayer diff --git a/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala index 5f3e41c..611d0c2 100644 --- a/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -35,7 +35,7 @@ object MainApp extends ZIOAppDefault: ) .provide( Server.defaultWithPort(randomPort), - + // An layer responsible for storing the state of the `counterApp` ZLayer.fromZIO(Ref.make(0)), From 87ea9b01f4a04e16ba304cdf9a4f8200e17717b7 Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Wed, 14 Aug 2024 13:32:34 +0530 Subject: [PATCH 03/13] revert --- .../scala/dev/zio/quickstart/MainApp.scala | 40 ++++--------------- 1 file changed, 8 insertions(+), 32 deletions(-) diff --git a/zio-quickstart-restful-webservice-custom-logger/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-custom-logger/src/main/scala/dev/zio/quickstart/MainApp.scala index f2a0a1d..2a62147 100644 --- a/zio-quickstart-restful-webservice-custom-logger/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-custom-logger/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -5,46 +5,22 @@ import zio._ import zio.http._ import zio.logging.LogFormat import zio.logging.backend.SLF4J -import zio.Console._ -import scala.util.Random object MainApp extends ZIOAppDefault { override val bootstrap: ZLayer[Any, Nothing, Unit] = SLF4J.slf4j(LogFormat.colored) def run = { - val retries = 5 - val portRangeStart = 8000 - val portRangeEnd = 8005 + Server + .serve(UserRoutes()) + .provide( + Server.defaultWithPort(8080), - def allocatePort(retriesLeft: Int): ZIO[Any, Throwable, Unit] = - if (retriesLeft <= 0) { - ZIO.fail( - new RuntimeException( - "Failed to allocate a port within the retry limit" - ) - ) - } else { - val randomPort = - Random.nextInt(portRangeEnd - portRangeStart + 1) + portRangeStart - Server - .serve(UserRoutes()) - .provide( - Server.defaultWithPort(randomPort), + // An layer responsible for storing the state of the `counterApp` + ZLayer.fromZIO(Ref.make(0)), - // A layer responsible for storing the state of the `counterApp` - ZLayer.fromZIO(Ref.make(0)), - - // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead - InmemoryUserRepo.layer - ) - .catchAll(_ => allocatePort(retriesLeft - 1)) - } - - allocatePort(retries) - .fold( - ex => ExitCode.failure, - _ => ExitCode.success + // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead + InmemoryUserRepo.layer ) } } From 15d53ba3a5b5c9be55534c8d15ca46fae0767785 Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Wed, 14 Aug 2024 13:33:20 +0530 Subject: [PATCH 04/13] Update MainApp.scala --- .../scala/dev/zio/quickstart/MainApp.scala | 44 +++++-------------- 1 file changed, 10 insertions(+), 34 deletions(-) diff --git a/zio-quickstart-restful-webservice-dockerize/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-dockerize/src/main/scala/dev/zio/quickstart/MainApp.scala index 86f37b4..e79420d 100644 --- a/zio-quickstart-restful-webservice-dockerize/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-dockerize/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -6,45 +6,21 @@ import dev.zio.quickstart.greet.GreetingRoutes import dev.zio.quickstart.users.{InmemoryUserRepo, UserRoutes} import zio._ import zio.http._ -import zio.Console._ -import scala.util.Random object MainApp extends ZIOAppDefault { def run = { - val retries = 5 - val portRangeStart = 8000 - val portRangeEnd = 8005 - - def allocatePort(retriesLeft: Int): ZIO[Any, Throwable, Unit] = - if (retriesLeft <= 0) { - ZIO.fail( - new RuntimeException( - "Failed to allocate a port within the retry limit" - ) - ) - } else { - val randomPort = - Random.nextInt(portRangeEnd - portRangeStart + 1) + portRangeStart - Server - .serve( - GreetingRoutes() ++ DownloadRoutes() ++ CounterRoutes() ++ UserRoutes() - ) - .provide( - Server.defaultWithPort(randomPort), - - // A layer responsible for storing the state of the `counterApp` - ZLayer.fromZIO(Ref.make(0)), + Server + .serve( + GreetingRoutes() ++ DownloadRoutes() ++ CounterRoutes() ++ UserRoutes() + ) + .provide( + Server.defaultWithPort(8080), - // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead - InmemoryUserRepo.layer - ) - .catchAll(_ => allocatePort(retriesLeft - 1)) - } + // An layer responsible for storing the state of the `counterApp` + ZLayer.fromZIO(Ref.make(0)), - allocatePort(retries) - .fold( - ex => ExitCode.failure, - _ => ExitCode.success + // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead + InmemoryUserRepo.layer ) } } From aa0415e617b389b7e03ef62c1b7ffac06daa0044 Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Wed, 14 Aug 2024 13:34:05 +0530 Subject: [PATCH 05/13] Update MainApp.scala --- .../scala/dev/zio/quickstart/MainApp.scala | 49 +++++-------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala index 15fb6d2..ab6fa82 100644 --- a/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -1,3 +1,4 @@ + package dev.zio.quickstart import dev.zio.quickstart.counter.CounterApp @@ -6,46 +7,22 @@ import dev.zio.quickstart.greet.GreetingApp import dev.zio.quickstart.users.{InmemoryUserRepo, PersistentUserRepo, UserApp} import zio._ import zio.http._ -import scala.util.Random -import zio.Console._ object MainApp extends ZIOAppDefault { def run = { - val retries = 5 - val portRangeStart = 8000 - val portRangeEnd = 8005 - - def allocatePort(retriesLeft: Int): ZIO[Any, Throwable, Unit] = - if (retriesLeft <= 0) { - ZIO.fail( - new RuntimeException( - "Failed to allocate a port within the retry limit" - ) - ) - } else { - val randomPort = - Random.nextInt(portRangeEnd - portRangeStart + 1) + portRangeStart - Server - .serve( - (GreetingApp() ++ DownloadApp() ++ CounterApp() ++ UserApp()).withDefaultErrorResponse - ) - .provide( - Server.defaultWithPort(randomPort), - - // A layer responsible for storing the state of the `counterApp` - ZLayer.fromZIO(Ref.make(0)), + val httpApps = GreetingApp() ++ DownloadApp() ++ CounterApp() ++ UserApp() + Server + .serve( + httpApps.withDefaultErrorResponse + ) + .provide( + Server.defaultWithPort(8080), - // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead - InmemoryUserRepo.layer - ) *> Console - .printLine("Server started on http://localhost:") - .catchAll(_ => allocatePort(retriesLeft - 1)) - } + // An layer responsible for storing the state of the `counterApp` + ZLayer.fromZIO(Ref.make(0)), - allocatePort(retries) - .fold( - ex => ExitCode.failure, - _ => ExitCode.success - ) + // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead + InmemoryUserRepo.layer + ) *> Console.printLine("Server started on http://localhost:8080") } } From 80f331ee99444094fafb7802d42d9e92347bec6a Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Wed, 14 Aug 2024 13:34:33 +0530 Subject: [PATCH 06/13] remove space --- .../src/main/scala/dev/zio/quickstart/MainApp.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala index ab6fa82..766cbd7 100644 --- a/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-logging/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -1,4 +1,3 @@ - package dev.zio.quickstart import dev.zio.quickstart.counter.CounterApp From 4f343624b7a790a78b31928f07ca8d6117a8c7ed Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Wed, 14 Aug 2024 13:35:11 +0530 Subject: [PATCH 07/13] revert changes --- .../scala/dev/zio/quickstart/MainApp.scala | 59 ++++++------------- 1 file changed, 18 insertions(+), 41 deletions(-) diff --git a/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala index 3840b11..25efafa 100644 --- a/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -1,3 +1,4 @@ + package dev.zio.quickstart import dev.zio.quickstart.prometheus.PrometheusPublisherApp @@ -5,52 +6,28 @@ import dev.zio.quickstart.users._ import zio._ import zio.http._ import zio.metrics.connectors.{MetricsConfig, prometheus} -import zio.Console._ -import scala.util.Random object MainApp extends ZIOAppDefault { private val metricsConfig = ZLayer.succeed(MetricsConfig(5.seconds)) def run = { - val retries = 5 - val portRangeStart = 8000 - val portRangeEnd = 8005 - - def allocatePort(retriesLeft: Int): ZIO[Any, Throwable, Unit] = - if (retriesLeft <= 0) { - ZIO.fail( - new RuntimeException( - "Failed to allocate a port within the retry limit" - ) - ) - } else { - val randomPort = - Random.nextInt(portRangeEnd - portRangeStart + 1) + portRangeStart - Server - .serve(UserRoutes() ++ PrometheusPublisherApp()) - .provide( - Server.defaultWithPort(randomPort), - - // A layer responsible for storing the state of the `counterApp` - ZLayer.fromZIO(Ref.make(0)), - - // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead - InmemoryUserRepo.layer, - - // General config for all metric backend - metricsConfig, - - // The Prometheus reporting layer - prometheus.publisherLayer, - prometheus.prometheusLayer - ) - .catchAll(_ => allocatePort(retriesLeft - 1)) - } - - allocatePort(retries) - .fold( - ex => ExitCode.failure, - _ => ExitCode.success + Server + .serve(UserRoutes() ++ PrometheusPublisherApp()) + .provide( + Server.defaultWithPort(8080), + + // An layer responsible for storing the state of the `counterApp` + ZLayer.fromZIO(Ref.make(0)), + + // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead + InmemoryUserRepo.layer, + + // general config for all metric backend + metricsConfig, + + // The prometheus reporting layer + prometheus.publisherLayer, + prometheus.prometheusLayer ) } } From 53d4f73450deb5a27bb7ec87d7695c4a50f38fbe Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Wed, 14 Aug 2024 13:36:24 +0530 Subject: [PATCH 08/13] Update MainApp.scala --- .../scala/dev/zio/quickstart/MainApp.scala | 45 +++++-------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala index 611d0c2..7fd6bef 100644 --- a/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -1,3 +1,4 @@ + package dev.zio.quickstart import dev.zio.quickstart.counter.CounterRoutes @@ -9,44 +10,20 @@ import dev.zio.quickstart.users.{ UserRoutes } import zio._ -import zio.Console._ import zio.http._ -import scala.util.Random object MainApp extends ZIOAppDefault: def run = - val retries = 5 - val portRangeStart = 8000 - val portRangeEnd = 8005 - - def allocatePort(retriesLeft: Int): ZIO[Any, Throwable, Unit] = - if (retriesLeft <= 0) { - ZIO.fail( - new RuntimeException( - "Failed to allocate a port within the retry limit" - ) - ) - } else { - val randomPort = - Random.nextInt(portRangeEnd - portRangeStart + 1) + portRangeStart - Server - .serve( - GreetingRoutes() ++ DownloadRoutes() ++ CounterRoutes() ++ UserRoutes() - ) - .provide( - Server.defaultWithPort(randomPort), - - // An layer responsible for storing the state of the `counterApp` - ZLayer.fromZIO(Ref.make(0)), + Server + .serve( + GreetingRoutes() ++ DownloadRoutes() ++ CounterRoutes() ++ UserRoutes() + ) + .provide( + Server.defaultWithPort(8080), - // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead - InmemoryUserRepo.layer - ) - .catchAll(_ => allocatePort(retriesLeft - 1)) - } + // An layer responsible for storing the state of the `counterApp` + ZLayer.fromZIO(Ref.make(0)), - allocatePort(retries) - .fold( - ex => ExitCode.failure, - _ => ExitCode.success + // To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead + InmemoryUserRepo.layer ) From 869f9cec8a26de19a6d3d91653b14afaaff87745 Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Wed, 14 Aug 2024 13:37:52 +0530 Subject: [PATCH 09/13] remove extra line --- .../src/main/scala/dev/zio/quickstart/MainApp.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala index 25efafa..c5bb9a0 100644 --- a/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice-metrics/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -1,4 +1,3 @@ - package dev.zio.quickstart import dev.zio.quickstart.prometheus.PrometheusPublisherApp From 11364805d4fbf87d32f5431f68ead669252bd0b8 Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Wed, 14 Aug 2024 13:38:19 +0530 Subject: [PATCH 10/13] remove extra line added --- .../src/main/scala/dev/zio/quickstart/MainApp.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala b/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala index 7fd6bef..8f26258 100644 --- a/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala +++ b/zio-quickstart-restful-webservice/src/main/scala/dev/zio/quickstart/MainApp.scala @@ -1,4 +1,3 @@ - package dev.zio.quickstart import dev.zio.quickstart.counter.CounterRoutes From b4d08a57903137ab9dc076c57f7ea4ad278c8ff7 Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Wed, 14 Aug 2024 14:11:49 +0530 Subject: [PATCH 11/13] update README --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index c7b2538..2dd3db8 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,31 @@ Once you are inside the project directory, run the application: $ sbt run ``` +## Running the Application with sbt-revolver + +To manage the quickstarts application lifecycle more effectively and avoid issues like the `Port already bound` error when restarting the server, we have integrated the `sbt-revolver plugin`. This plugin allows you to start, stop and monitor easily using the following commands: + +1. To start the application in a forked JVM, use the `reStart` command: + +```bash +$ sbt reStart +``` +This will start quickstart application, and if it's already running, it will first stop the previous instance before restarting. + +2. To stop the running application, use the `reStop` command: + +```bash +$ sbt reStop +``` +This command will force-kill the forked JVM running your application. Note that any shutdown hooks (e.g., graceful shutdown logic) will not be executed when using this command. + +3. To check the current running state of application, use the `reStatus` command: + +```bash +$ sbt reStatus +``` +This will provide you with a log message about whether the application is currently running or stopped. + ## List of Quickstarts - [ZIO Cache](zio-quickstart-cache) From dece41596f07503e4656d11fc999eacf6620e46f Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Wed, 14 Aug 2024 14:13:13 +0530 Subject: [PATCH 12/13] add sbt-resolver plugin --- project/plugins.sbt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project/plugins.sbt b/project/plugins.sbt index e7aff9f..93a7d80 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -8,4 +8,6 @@ addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.16") addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.11.1") +addSbtPlugin("io.spray" % "sbt-revolver" % "0.10.0") + resolvers ++= Resolver.sonatypeOssRepos("public") From a71821442665bfdba99f97ec127172a8a30ca795 Mon Sep 17 00:00:00 2001 From: Vamshi Maskuri <117595548+varshith257@users.noreply.github.com> Date: Wed, 14 Aug 2024 14:14:28 +0530 Subject: [PATCH 13/13] cleanup --- build.sbt | 1 - 1 file changed, 1 deletion(-) diff --git a/build.sbt b/build.sbt index 1143369..509d01a 100644 --- a/build.sbt +++ b/build.sbt @@ -41,7 +41,6 @@ lazy val root = `zio-quickstart-sql` ) .settings( - run / fork := true, testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework") )