generated from kevchuang/scala3-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): Use zServerLogic and implement services package (#9)
- Loading branch information
Showing
11 changed files
with
74 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
...es/server/src/main/scala/io/kevchuang/reviewboard/http/controllers/HealthController.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
modules/server/src/main/scala/io/kevchuang/reviewboard/http/endpoints/HttpEndpoint.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
package io.kevchuang.reviewboard.http.endpoints | ||
|
||
import sttp.tapir.server.ServerEndpoint | ||
import sttp.tapir.ztapir.* | ||
import zio.* | ||
|
||
trait HttpEndpoint[I, E, O]: | ||
trait HttpEndpoint[R, I, E, O]: | ||
def endpointDescription: ApiEndpoint[I, E, O] | ||
def endpointLogic: I => Task[O] | ||
def serverEndpoint: ServerEndpoint[Any, Task] = | ||
endpointDescription.serverLogicSuccess[Task](endpointLogic) | ||
def endpointLogic: I => ZIO[R, E, O] | ||
def serverEndpoint: ZServerEndpoint[R, Any] = | ||
endpointDescription.zServerLogic(endpointLogic) | ||
end HttpEndpoint |
8 changes: 5 additions & 3 deletions
8
modules/server/src/main/scala/io/kevchuang/reviewboard/http/routes/HealthRoutes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
package io.kevchuang.reviewboard.http.routes | ||
|
||
import io.kevchuang.reviewboard.http.endpoints.HealthEndpoint | ||
import io.kevchuang.reviewboard.services.health.HealthService | ||
import sttp.tapir.server.ServerEndpoint | ||
import sttp.tapir.ztapir.* | ||
import zio.* | ||
|
||
private case class HealthRoutes(healthEndpoint: HealthEndpoint) | ||
extends HttpRoute: | ||
def routes: List[ServerEndpoint[Any, Task]] = | ||
extends HttpRoute[HealthService]: | ||
def endpoints: List[ZServerEndpoint[HealthService, Any]] = | ||
List(healthEndpoint.serverEndpoint) | ||
end HealthRoutes | ||
|
||
object HealthRoutes: | ||
def make: UIO[HealthRoutes] = | ||
def make: RIO[HealthService, HealthRoutes] = | ||
for healthEndpoint <- HealthEndpoint.make | ||
yield HealthRoutes(healthEndpoint) | ||
end HealthRoutes |
8 changes: 6 additions & 2 deletions
8
modules/server/src/main/scala/io/kevchuang/reviewboard/http/routes/HttpRoute.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
package io.kevchuang.reviewboard.http.routes | ||
|
||
import sttp.tapir.server.ServerEndpoint | ||
import sttp.tapir.server.ziohttp.{ZioHttpInterpreter, ZioHttpServerOptions} | ||
import sttp.tapir.ztapir.ZServerEndpoint | ||
import zio.* | ||
import zio.http.HttpApp | ||
|
||
trait HttpRoute: | ||
def routes: List[ServerEndpoint[Any, Task]] | ||
trait HttpRoute[R]: | ||
def endpoints: List[ZServerEndpoint[R, Any]] | ||
def routes: HttpApp[R] = ZioHttpInterpreter().toHttp(endpoints) | ||
end HttpRoute |
15 changes: 7 additions & 8 deletions
15
modules/server/src/main/scala/io/kevchuang/reviewboard/http/server/HttpServer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
package io.kevchuang.reviewboard.http.server | ||
|
||
import sttp.tapir.server.ServerEndpoint | ||
import zio.* | ||
import zio.http.Server | ||
import zio.http.{HttpApp, Server} | ||
|
||
trait HttpServer: | ||
def start( | ||
routes: List[ServerEndpoint[Any, Task]] | ||
): ZIO[Server, Throwable, Unit] | ||
def start[R]( | ||
routes: HttpApp[R] | ||
): ZIO[R & Server, Throwable, Unit] | ||
end HttpServer | ||
|
||
object HttpServer: | ||
def start( | ||
routes: List[ServerEndpoint[Any, Task]] | ||
): ZIO[HttpServer & Server, Throwable, Unit] = | ||
def start[R]( | ||
routes: HttpApp[R] | ||
): ZIO[R & HttpServer & Server, Throwable, Unit] = | ||
ZIO.serviceWithZIO[HttpServer](_.start(routes)) | ||
end HttpServer |
18 changes: 5 additions & 13 deletions
18
modules/server/src/main/scala/io/kevchuang/reviewboard/http/server/HttpServerLive.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,14 @@ | ||
package io.kevchuang.reviewboard.http.server | ||
|
||
import sttp.tapir.server.ServerEndpoint | ||
import sttp.tapir.server.ziohttp.{ZioHttpInterpreter, ZioHttpServerOptions} | ||
import zio.* | ||
import zio.http.Server | ||
import zio.http.{HttpApp, Server} | ||
|
||
object HttpServerLive: | ||
def live: ZLayer[Any, Nothing, HttpServer] = | ||
lazy val live: ZLayer[Any, Nothing, HttpServer] = | ||
ZLayer.succeed( | ||
new HttpServer: | ||
override def start( | ||
endpoints: List[ServerEndpoint[Any, Task]] | ||
): ZIO[Server, Throwable, Unit] = | ||
Server | ||
.serve( | ||
ZioHttpInterpreter( | ||
ZioHttpServerOptions.default | ||
).toHttp(endpoints) | ||
) | ||
override def start[R]( | ||
routes: HttpApp[R] | ||
): ZIO[R & Server, Throwable, Unit] = Server.serve(routes) | ||
) | ||
end HttpServerLive |
13 changes: 13 additions & 0 deletions
13
modules/server/src/main/scala/io/kevchuang/reviewboard/services/health/HealthService.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.kevchuang.reviewboard.services.health | ||
|
||
import io.kevchuang.reviewboard.domain.health.HealthCheckResponse | ||
import zio.* | ||
|
||
trait HealthService: | ||
def checkHealth: ZIO[Any, Nothing, HealthCheckResponse] | ||
end HealthService | ||
|
||
object HealthService: | ||
def checkHealth: ZIO[HealthService, Nothing, HealthCheckResponse] = | ||
ZIO.serviceWithZIO[HealthService](_.checkHealth) | ||
end HealthService |
14 changes: 14 additions & 0 deletions
14
...es/server/src/main/scala/io/kevchuang/reviewboard/services/health/HealthServiceLive.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.kevchuang.reviewboard.services.health | ||
|
||
import io.kevchuang.reviewboard.domain.health.HealthCheckResponse | ||
import zio.* | ||
import io.github.iltotore.iron.* | ||
|
||
object HealthServiceLive: | ||
lazy val live: ULayer[HealthService] = | ||
ZLayer.succeed( | ||
new HealthService: | ||
def checkHealth: UIO[HealthCheckResponse] = | ||
ZIO.succeed(HealthCheckResponse("All good !")) | ||
) | ||
end HealthServiceLive |
7 changes: 7 additions & 0 deletions
7
modules/server/src/main/scala/io/kevchuang/reviewboard/services/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.kevchuang.reviewboard | ||
|
||
import io.kevchuang.reviewboard.services.health.HealthService | ||
|
||
package object services: | ||
type Services = HealthService | ||
end services |