Skip to content

Commit

Permalink
feat(server): Implement health check endpoint (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevchuang authored Feb 21, 2024
1 parent a22b6ad commit 219fd10
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ target/
.idea/

project/target
project/project

modules/foundations/target

modules/server/target
3 changes: 1 addition & 2 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ maxColumn = 80
project.git = true
runner.dialect = scala3
rewrite.scala3.convertToNewSyntax = true
rewrite.scala3.removeOptionalBraces = yes
rewrite.rules = [RedundantBraces]
align.preset = most
align.multiline = false
align.stripMargin = true
project.excludePaths = ["glob:**/project/"]
rewrite.scala3.insertEndMarkerMinLines = 1

7 changes: 5 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import BuildHelper.*
lazy val foundations = (project in file("modules/foundations"))
.settings(libraryDependencies ++= Dependencies.dependencies)

lazy val server = (project in file("modules/server"))
.settings(libraryDependencies ++= Dependencies.dependencies)

lazy val root = (project in file("."))
.settings(standardSettings)
.settings(nameSettings)
.aggregate(foundations)
.dependsOn(foundations)
.aggregate(foundations, server)
.dependsOn(foundations, server)

This file was deleted.

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
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
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
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
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
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
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
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
10 changes: 9 additions & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import sbt.*

object Dependencies {
val flywayVersion = "9.16.0"
val ironVersion = "2.4.0"
val javaJwtVersion = "4.4.0"
val javaMailVersion = "1.6.2"
val logbackVersion = "1.4.7"
Expand All @@ -22,6 +23,11 @@ object Dependencies {
"org.flywaydb" % "flyway-core" % flywayVersion
)

val iron: List[ModuleID] = List(
"io.github.iltotore" %% "iron-zio" % ironVersion,
"io.github.iltotore" %% "iron-zio-json" % ironVersion
)

val `java-jwt`: List[ModuleID] = List(
"com.auth0" % "java-jwt" % javaJwtVersion
)
Expand Down Expand Up @@ -52,11 +58,12 @@ object Dependencies {

val tapir: List[ModuleID] = List(
"com.softwaremill.sttp.tapir" %% "tapir-sttp-client" % tapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-iron" % tapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-json-zio" % tapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-zio" % tapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-zio-http-server" % tapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-swagger-ui-bundle" % tapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-sttp-stub-server" % tapirVersion % "test"
"com.softwaremill.sttp.tapir" %% "tapir-sttp-stub-server" % tapirVersion % Test
)

val `zio-config`: List[ModuleID] = List(
Expand Down Expand Up @@ -90,6 +97,7 @@ object Dependencies {

val dependencies: List[ModuleID] =
flyway ++
iron ++
`java-jwt` ++
javaMail ++
logback ++
Expand Down

0 comments on commit 219fd10

Please sign in to comment.