Skip to content

Commit

Permalink
Merge pull request #356 from lichess-org/refactor-http-routes
Browse files Browse the repository at this point in the history
Make httpRoutes returns http routes
  • Loading branch information
lenguyenthanh authored Nov 1, 2024
2 parents 342f60e + 04c9fda commit fc301b4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions modules/app/src/main/scala/app.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object App extends IOApp.Simple:
class SearchApp(res: AppResources, config: AppConfig)(using Logger[IO], LoggerFactory[IO]):
def run(): Resource[IO, Unit] =
for
httpApp <- Routes(res, config.server)
server <- MkHttpServer.apply.newEmber(config.server, httpApp)
_ <- Logger[IO].info(s"Starting server on ${config.server.host}:${config.server.port}").toResource
httpRoutes <- Routes(res, config.server)
server <- MkHttpServer.apply.newEmber(config.server, httpRoutes.orNotFound)
_ <- Logger[IO].info(s"Starting server on ${config.server.host}:${config.server.port}").toResource
yield ()
10 changes: 5 additions & 5 deletions modules/app/src/main/scala/http.logger.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package lila.search
package app

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 @@ -14,10 +14,10 @@ object ApiErrorLogger:
!res.status.isSuccess && res.status.code != 404

private def logError(req: Request[IO], res: Response[IO])(using Logger[IO]): IO[Unit] =
Http4sLogger.logMessage(req)(true, true)(Logger[IO].warn) >>
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))
9 changes: 5 additions & 4 deletions modules/app/src/main/scala/http.middleware.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ package app

import cats.effect.IO
import org.http4s.*
import org.http4s.implicits.*
import org.http4s.server.middleware.*
import org.typelevel.log4cats.{ Logger, LoggerFactory }

import scala.concurrent.duration.*

type Middleware = HttpRoutes[IO] => HttpRoutes[IO]
def ApplyMiddleware(config: HttpServerConfig)(routes: HttpRoutes[IO])(using LoggerFactory[IO]): HttpApp[IO] =
def ApplyMiddleware(config: HttpServerConfig)(routes: HttpRoutes[IO])(using
LoggerFactory[IO]
): HttpRoutes[IO] =

val autoSlash: Middleware = AutoSlash(_)
val timeout: Middleware = Timeout(60.seconds)

val middleware = autoSlash.andThen(timeout)

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))

given Logger[IO] = LoggerFactory[IO].getLogger
val logger =
if config.apiLogger then verboseLogger
else ApiErrorLogger.instance

logger(middleware(routes).orNotFound)
logger(middleware(routes))
4 changes: 2 additions & 2 deletions modules/app/src/main/scala/http.routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import cats.data.NonEmptyList
import cats.effect.{ IO, Resource }
import cats.syntax.all.*
import lila.search.spec.*
import org.http4s.{ HttpApp, HttpRoutes }
import org.http4s.HttpRoutes
import org.typelevel.log4cats.LoggerFactory
import smithy4s.http4s.SimpleRestJsonBuilder

def Routes(resources: AppResources, config: HttpServerConfig)(using
LoggerFactory[IO]
): Resource[IO, HttpApp[IO]] =
): Resource[IO, HttpRoutes[IO]] =

val healthServiceImpl: HealthService[IO] = HealthServiceImpl(resources.esClient)

Expand Down

0 comments on commit fc301b4

Please sign in to comment.