Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Port already bound Error #56

Closed
wants to merge 13 commits into from
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ lazy val root =
`zio-quickstart-sql`
)
.settings(
run / fork := true,
varshith257 marked this conversation as resolved.
Show resolved Hide resolved
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),

// 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)),

// 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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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),

// A 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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:8080")
// To use the persistence layer, provide the `PersistentUserRepo.layer` layer instead
InmemoryUserRepo.layer
) *> Console
.printLine("Server started on http://localhost:<port>")
.catchAll(_ => allocatePort(retriesLeft - 1))
}

allocatePort(retries)
.fold(
ex => ExitCode.failure,
_ => ExitCode.success
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,

// The prometheus reporting layer
prometheus.publisherLayer,
prometheus.prometheusLayer
// 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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Loading