Skip to content

Commit

Permalink
Refactor HttpApi to return HttpRoutes
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Nov 2, 2024
1 parent 430433b commit 37acb9e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/src/main/scala/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class FishnetApp(res: AppResources, config: AppConfig)(using Logger[IO]):
def run(): Resource[IO, Unit] =
for
executor <- createExecutor
httpApp = HttpApi(executor, HealthCheck(), config.server).httpApp
server <- MkHttpServer.apply.newEmber(config.server, httpApp)
httpRoutes = HttpApi(executor, HealthCheck(), config.server).routes
server <- MkHttpServer.apply.newEmber(config.server, httpRoutes.orNotFound)
_ <- RedisSubscriberJob(executor, res.redisPubsub).run()
_ <- WorkCleaningJob(executor).run()
_ <- Logger[IO].info(s"Starting server on ${config.server.host}:${config.server.port}").toResource
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/scala/http/ApiErrorLogger.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package lila.fishnet
package http

import cats.data.Kleisli
import cats.data.{ Kleisli, OptionT }
import cats.effect.IO
import cats.syntax.all.*
import org.http4s.internal.Logger as Http4sLogger
import org.http4s.{ HttpApp, Request, Response }
import org.http4s.{ HttpRoutes, Request, Response }
import org.typelevel.log4cats.Logger

object ApiErrorLogger:
Expand All @@ -17,7 +17,7 @@ object ApiErrorLogger:
Http4sLogger.logMessage(req)(true, true)(Logger[IO].warn) >>
Http4sLogger.logMessage(res)(true, true)(Logger[IO].warn)

def instance(using Logger[IO]): HttpApp[IO] => HttpApp[IO] = http =>
def instance(using Logger[IO]): HttpRoutes[IO] => HttpRoutes[IO] = http =>
Kleisli: req =>
http(req).flatTap: res =>
logError(req, res).whenA(isResponseError(res))
OptionT.liftF(logError(req, res).whenA(isResponseError(res)))
11 changes: 4 additions & 7 deletions app/src/main/scala/http/HttpApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package http
import cats.effect.IO
import cats.syntax.all.*
import org.http4s.*
import org.http4s.implicits.*
import org.http4s.server.middleware.*
import org.typelevel.log4cats.Logger

Expand All @@ -19,8 +18,6 @@ final class HttpApi(
private val fishnetRoutes = FishnetRoutes(executor).routes
private val healthRoutes = HealthRoutes(healthCheck).routes

private val routes: HttpRoutes[IO] = fishnetRoutes <+> healthRoutes

type Middleware = HttpRoutes[IO] => HttpRoutes[IO]

private val autoSlash: Middleware = AutoSlash(_)
Expand All @@ -29,12 +26,12 @@ final class HttpApi(
private val middleware = autoSlash andThen timeout

private def verboseLogger =
RequestLogger.httpApp[IO](true, true) andThen
ResponseLogger.httpApp[IO, Request[IO]](true, true)
RequestLogger.httpRoutes[IO](true, true) andThen
ResponseLogger.httpRoutes[IO, Request[IO]](true, true)

private val loggers =
if config.apiLogger then verboseLogger
else ApiErrorLogger.instance

val httpApp: HttpApp[IO] =
loggers(middleware(routes).orNotFound)
val routes: HttpRoutes[IO] =
loggers(middleware(fishnetRoutes <+> healthRoutes))

0 comments on commit 37acb9e

Please sign in to comment.