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): Implement health check endpoint (#3)
- Loading branch information
Showing
13 changed files
with
143 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,8 @@ target/ | |
.idea/ | ||
|
||
project/target | ||
project/project | ||
|
||
modules/foundations/target | ||
|
||
modules/server/target |
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
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
9 changes: 0 additions & 9 deletions
9
modules/foundations/src/main/scala/io/kevchuang/board/Hello.scala
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
modules/server/src/main/scala/io/kevchuang/reviewboard/Application.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,21 @@ | ||
package io.kevchuang.reviewboard | ||
|
||
import io.kevchuang.reviewboard.http.controllers.HealthController | ||
import io.kevchuang.reviewboard.http.server.{HttpServer, HttpServerLive} | ||
import zio.* | ||
import zio.http.Server | ||
|
||
object Application extends ZIOAppDefault: | ||
|
||
private val program: ZIO[HttpServer & Server, Throwable, Unit] = | ||
for | ||
health <- HealthController.make | ||
endpoints = List(health.healthCheck) | ||
_ <- HttpServer.start(endpoints) | ||
yield () | ||
|
||
override def run: ZIO[Any with ZIOAppArgs with Scope, Any, Any] = | ||
program | ||
.provide(Server.default, HttpServerLive.live) | ||
|
||
end Application |
13 changes: 13 additions & 0 deletions
13
modules/server/src/main/scala/io/kevchuang/reviewboard/domain/health.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.domain | ||
|
||
import zio.json.* | ||
import io.github.iltotore.iron.* | ||
import io.github.iltotore.iron.zioJson.given | ||
import io.kevchuang.reviewboard.types.NotEmpty | ||
|
||
object health: | ||
final case class HealthCheckResponse(response: String :| NotEmpty) | ||
object HealthCheckResponse: | ||
given JsonCodec[HealthCheckResponse] = DeriveJsonCodec.gen | ||
end HealthCheckResponse | ||
end health |
18 changes: 18 additions & 0 deletions
18
...es/server/src/main/scala/io/kevchuang/reviewboard/http/controllers/HealthController.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,18 @@ | ||
package io.kevchuang.reviewboard.http.controllers | ||
|
||
import io.kevchuang.reviewboard.domain.health.HealthCheckResponse | ||
import io.kevchuang.reviewboard.http.endpoints.HealthEndpoint | ||
import zio.* | ||
import io.github.iltotore.iron.* | ||
import sttp.tapir.server.ServerEndpoint | ||
|
||
class HealthController private extends HealthEndpoint: | ||
def healthCheck: ServerEndpoint[Any, Task] = | ||
healthEndpoint.serverLogicSuccess[Task](_ => | ||
ZIO.succeed(HealthCheckResponse("All good !")) | ||
) | ||
end HealthController | ||
|
||
object HealthController: | ||
def make: UIO[HealthController] = ZIO.succeed(new HealthController) | ||
end HealthController |
19 changes: 19 additions & 0 deletions
19
modules/server/src/main/scala/io/kevchuang/reviewboard/http/endpoints/HealthEndpoint.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,19 @@ | ||
package io.kevchuang.reviewboard.http.endpoints | ||
|
||
import io.kevchuang.reviewboard.domain.health.* | ||
import io.kevchuang.reviewboard.domain.health.HealthCheckResponse.given | ||
import sttp.tapir.* | ||
import sttp.tapir.codec.iron.{*, given} | ||
import sttp.tapir.generic.auto.* | ||
import sttp.tapir.json.zio.{*, given} | ||
|
||
trait HealthEndpoint: | ||
val healthEndpoint: ApiEndpoint[Unit, Unit, HealthCheckResponse] = | ||
endpoint | ||
.tag("health") | ||
.name("health") | ||
.description("health check") | ||
.get | ||
.in("health") | ||
.out(jsonBody[HealthCheckResponse]) | ||
end HealthEndpoint |
7 changes: 7 additions & 0 deletions
7
modules/server/src/main/scala/io/kevchuang/reviewboard/http/endpoints/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.http | ||
|
||
import sttp.tapir.PublicEndpoint | ||
|
||
package object endpoints: | ||
type ApiEndpoint[I, E, O] = PublicEndpoint[I, E, O, Any] | ||
end endpoints |
18 changes: 18 additions & 0 deletions
18
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.kevchuang.reviewboard.http.server | ||
|
||
import sttp.tapir.server.ServerEndpoint | ||
import zio.* | ||
import zio.http.Server | ||
|
||
trait HttpServer: | ||
def start( | ||
endpoints: List[ServerEndpoint[Any, Task]] | ||
): ZIO[Server, Throwable, Unit] | ||
end HttpServer | ||
|
||
object HttpServer: | ||
def start( | ||
endpoints: List[ServerEndpoint[Any, Task]] | ||
): ZIO[HttpServer & Server, Throwable, Unit] = | ||
ZIO.serviceWithZIO[HttpServer](_.start(endpoints)) | ||
end HttpServer |
22 changes: 22 additions & 0 deletions
22
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.kevchuang.reviewboard.http.server | ||
|
||
import sttp.tapir.server.ServerEndpoint | ||
import sttp.tapir.server.ziohttp.{ZioHttpInterpreter, ZioHttpServerOptions} | ||
import zio.* | ||
import zio.http.Server | ||
|
||
object HttpServerLive: | ||
def 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) | ||
) | ||
) | ||
end HttpServerLive |
7 changes: 7 additions & 0 deletions
7
modules/server/src/main/scala/io/kevchuang/reviewboard/types/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.github.iltotore.iron.constraint.all.* | ||
|
||
package object types: | ||
type NotEmpty = Not[Empty] | ||
end types |
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