Skip to content

Commit

Permalink
Implement forum endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed May 9, 2024
1 parent 94a59e0 commit 77a7dfd
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 27 deletions.
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ lazy val app = (project in file("modules/app"))
libraryDependencies ++= Seq(
"com.disneystreaming.smithy4s" %% "smithy4s-http4s" % smithy4sVersion.value,
"com.disneystreaming.smithy4s" %% "smithy4s-http4s-swagger" % smithy4sVersion.value,
"com.sksamuel.elastic4s" %% "elastic4s-effect-cats" % "8.11.5",
catsCore,
catsEffect,
ducktape,
http4sServer,
http4sEmberClient,
cirisCore,
cirisHtt4s,
logbackX
),
excludeDependencies ++= Seq("org.typelevel" % "cats-core_2.13", "org.typelevel" % "cats-kernel_2.13"),
excludeDependencies ++= Seq("org.typelevel" % "cats-core_2.13", "org.typelevel" % "cats-kernel_2.13", "com.sksamuel.elastic4s" % "elastic4s-core_2.13", "com.sksamuel.elastic4s" % "elastic4s-domain_2.13", "com.sksamuel.elastic4s" % "elastic4s-http_2.13", "com.fasterxml.jackson.module" % "jackson-module-scala_2.13"),
Compile / run / fork := true,
)
.enablePlugins(JavaAppPackaging)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloy#simpleRestJson
@simpleRestJson
service SearchService {
version: "3.0.0",
operations: [SearchForum]
operations: [SearchForum, CountForum]
}

@readonly
Expand Down Expand Up @@ -41,6 +41,8 @@ structure SearchForumInput {
}

structure ForumInputBody {
troll: Boolean
query: String
@required
text: String
@required
troll: Boolean = false
}
22 changes: 22 additions & 0 deletions modules/app/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<configuration>

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logs/application.log</file>
<encoder>
<pattern>%date [%thread] %-5level %logger{20} - %msg%n%xException</pattern>
</encoder>
</appender>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%thread] %-5level %logger{20} - %msg%n%xException</pattern>
</encoder>
</appender>

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

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

import cats.effect.*
import org.typelevel.log4cats.Logger
import com.sksamuel.elastic4s.http.JavaClient
import com.sksamuel.elastic4s.cats.effect.instances.*
import com.sksamuel.elastic4s.{ ElasticClient, ElasticProperties }

class AppResources private (val esClient: ESClient[IO])

object AppResources:

def instance(conf: AppConfig)(using Logger[IO]): Resource[IO, AppResources] =
makeClient(conf.elastic)
.map(ESClient.apply[IO])
.map(AppResources(_))

def makeClient(conf: ElasticConfig): Resource[IO, ElasticClient] =
Resource.make(IO(ElasticClient(JavaClient(ElasticProperties(conf.uri))))): client =>
IO(client.close())
9 changes: 5 additions & 4 deletions modules/app/src/main/scala/app.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ object App extends IOApp.Simple:
def app: Resource[IO, Unit] =
for
config <- AppConfig.load.toResource
_ <- Logger[IO].info(s"Starting fide-api with config: $config").toResource
_ <- SearchApp(config).run()
_ <- Logger[IO].info(s"Starting lila-search with config: $config").toResource
res <- AppResources.instance(config)
_ <- SearchApp(res, config).run()
yield ()

class SearchApp(config: AppConfig)(using Logger[IO]):
class SearchApp(res: AppResources, config: AppConfig)(using Logger[IO]):
def run(): Resource[IO, Unit] =
for
httpApp <- Routes
httpApp <- Routes(res)
server <- MkHttpServer.apply.newEmber(config.server, httpApp)
_ <- Logger[IO].info(s"Starting server on ${config.server.host}:${config.server.port}").toResource
yield ()
5 changes: 4 additions & 1 deletion modules/app/src/main/scala/http.middleware.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ def ApplyMiddleware(routes: HttpRoutes[IO]): HttpApp[IO] =
val timeout: Middleware = Timeout(60.seconds)

val middleware = autoSlash.andThen(timeout)
val logger =
RequestLogger.httpApp[IO](true, true) andThen
ResponseLogger.httpApp[IO, Request[IO]](true, true)

middleware(routes).orNotFound
logger(middleware(routes).orNotFound)
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 @@ -9,11 +9,11 @@ import org.http4s.{ HttpApp, HttpRoutes }
import org.typelevel.log4cats.Logger
import smithy4s.http4s.SimpleRestJsonBuilder

def Routes(using Logger[IO]): Resource[IO, HttpApp[IO]] =
def Routes(resources: AppResources)(using Logger[IO]): Resource[IO, HttpApp[IO]] =

val healthServiceImpl: HealthService[IO] = new HealthService.Default[IO](IO.stub)

val searchServiceImpl: SearchService[IO] = new SearchService.Default[IO](IO.stub)
val searchServiceImpl: SearchService[IO] = SearchServiceImpl(resources.esClient)

val search: Resource[IO, HttpRoutes[IO]] =
SimpleRestJsonBuilder.routes(searchServiceImpl).resource
Expand Down
27 changes: 27 additions & 0 deletions modules/app/src/main/scala/service.search.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package lila.search
package app

import cats.effect.*
import lila.search.spec.*
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.syntax.*
import forum.ForumQuery.*
import io.github.arainko.ducktape.*

class SearchServiceImpl(esClient: ESClient[IO])(using Logger[IO]) extends SearchService[IO]:

override def countForum(text: String, troll: Boolean): IO[CountResponse] =
esClient
.count(Index("forum"), Forum(text, troll))
.map(_.to[CountResponse])
.handleErrorWith: e =>
error"Error in countForum: text=$text, troll=$troll" *>
IO.raiseError(InternalServerError("Internal server error"))

override def searchForum(body: ForumInputBody, from: Int, size: Int): IO[SearchResponse] =
esClient
.search(Index("forum"), Forum(body.text, body.troll), From(from), Size(size))
.map(_.to[SearchResponse])
.handleErrorWith: e =>
error"Error in searchForum: body=$body, from=$from, size=$size" *>
IO.raiseError(InternalServerError("Internal server error"))
22 changes: 6 additions & 16 deletions modules/core/src/main/scala/ESClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,18 @@ object ESClient {
q: Queryable[A]
): F[SearchResponse] =
client
.execute { q.searchDef(query)(from, size)(index) }
.execute(q.searchDef(query)(from, size)(index))
.flatMap(toResult)
.map(SearchResponse.apply)

def count[A](index: Index, query: A)(implicit q: Queryable[A]): F[CountResponse] =
client
.execute {
q.countDef(query)(index)
}
.execute(q.countDef(query)(index))
.flatMap(toResult)
.map(CountResponse.apply)

def store(index: Index, id: Id, obj: JsonObject): F[Response[IndexResponse]] =
client.execute {
indexInto(index.name).source(obj.json).id(id.value)
}
client.execute(indexInto(index.name).source(obj.json).id(id.value))

def storeBulk(index: Index, objs: List[(String, JsonObject)]): F[Unit] =
if (objs.isEmpty) ().pure[F]
Expand All @@ -70,9 +66,7 @@ object ESClient {
}.void

def deleteOne(index: Index, id: Id): F[Response[DeleteResponse]] =
client.execute {
deleteById(index.toES, id.value)
}
client.execute(deleteById(index.toES, id.value))

def deleteMany(index: Index, ids: List[Id]): F[Response[BulkResponse]] =
client.execute {
Expand All @@ -96,17 +90,13 @@ object ESClient {

def refreshIndex(index: Index): F[Unit] =
client
.execute {
ElasticDsl.refreshIndex(index.name)
}
.execute(ElasticDsl.refreshIndex(index.name))
.void
.recover { case _: Exception =>
println(s"Failed to refresh index $index")
}

private def dropIndex(index: Index) =
client.execute {
deleteIndex(index.name)
}
client.execute { deleteIndex(index.name) }
}
}
File renamed without changes.

0 comments on commit 77a7dfd

Please sign in to comment.