Skip to content

Commit

Permalink
Setup http server with http4s
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Nov 19, 2023
1 parent 615785c commit 3e12680
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Redis
REDIS_HOST=localhost
REDIS_PORT=5432

# http server
HTTP_SERVER_HOST=0.0.0.0
HTTP_SERVER_PORT=9665
21 changes: 21 additions & 0 deletions app/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logs/application.log</file>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>

<logger name="http4s"/>
</configuration>
24 changes: 24 additions & 0 deletions app/src/main/scala/App.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lila.fishnet

import cats.effect.{ IO, IOApp }
import cats.effect.*
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger

import lila.fishnet.http.*

object App extends IOApp.Simple:

given Logger[IO] = Slf4jLogger.getLogger[IO]

given client: LilaClient = new LilaClient:
def send(move: Lila.Move): IO[Unit] =
IO.println(s"send $move")

override def run: IO[Unit] = Config
.load
.flatMap: cfg =>
Executor.apply.flatMap: ec =>
val app = HttpApi(ec, HealthCheck()).httpApp
MkHttpServer.apply.newEmber(cfg.server, app)
.useForever
6 changes: 3 additions & 3 deletions app/src/main/scala/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ object HttpServerConfig:
case class RedisConfig(host: Host, port: Port)

object RedisConfig:
private def host[F[_]] = env("REDIS_HOST").or(prop("redis.host")).as[Host]
private def port[F[_]] = env("REDIS_PORT").or(prop("redis.port")).as[Port]
def config[F[_]] = (host, port).parMapN(RedisConfig.apply)
private def host = env("REDIS_HOST").or(prop("redis.host")).as[Host]
private def port = env("REDIS_PORT").or(prop("redis.port")).as[Port]
def config = (host, port).parMapN(RedisConfig.apply)
5 changes: 4 additions & 1 deletion app/src/main/scala/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ trait Executor:

object Executor:

val maxSize = 300
def apply(using client: LilaClient): IO[Executor] =
instance(client)

val maxSize = 300
type State = Map[Work.Id, Work.Move]

def instance(client: LilaClient): IO[Executor] =
Ref.of[IO, State](Map.empty).map: ref =>
new Executor:
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/scala/MkHttpServer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lila.fishnet

import org.http4s.*
import cats.effect.{ IO, Resource }
import fs2.io.net.Network
import org.http4s.ember.server.EmberServerBuilder
import org.http4s.server.Server
import org.http4s.server.defaults.Banner
import org.typelevel.log4cats.Logger

trait MkHttpServer:
def newEmber(cfg: HttpServerConfig, httpApp: HttpApp[IO]): Resource[IO, Server]

object MkHttpServer:

def apply(using server: MkHttpServer): MkHttpServer = server

given forAsyncLogger(using Logger[IO]): MkHttpServer = new:

def newEmber(cfg: HttpServerConfig, httpApp: HttpApp[IO]): Resource[IO, Server] = EmberServerBuilder
.default[IO]
.withHost(cfg.host)
.withPort(cfg.port)
.withHttpApp(httpApp)
.build
.evalTap(showEmberBanner)

private def showEmberBanner(s: Server): IO[Unit] =
Logger[IO].info(s"\n${Banner.mkString("\n")}\nHTTP Server started at ${s.address}")
3 changes: 3 additions & 0 deletions app/src/main/scala/http/HealthCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ trait HealthCheck:

object HealthCheck:

def apply(): HealthCheck = new HealthCheck:
def status: IO[AppStatus] = IO.pure(AppStatus(true))

case class AppStatus(status: Boolean) derives Codec.AsObject
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ lazy val app = project
kamonInflux,
kamonSystemMetrics,
log4Cats,
logback,
chessTestKit,
monocleCore,
munit,
Expand Down

0 comments on commit 3e12680

Please sign in to comment.