-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: a simple service exposing Search API (#8)
* feat: a simple service exposing Search API * feat: solr client to allow specifying a search phrase * feat: avro JSON EntityCodec for http4s
- Loading branch information
Showing
26 changed files
with
500 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# http4s-avro | ||
|
||
This module contains tooling to bridge avro and http4s. |
68 changes: 68 additions & 0 deletions
68
modules/http4s-avro/src/main/scala/io/renku/search/http/avro/AvroEntityCodec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.search.http.avro | ||
|
||
import cats.data.EitherT | ||
import cats.effect.Async | ||
import cats.syntax.all.* | ||
import fs2.Chunk | ||
import io.renku.avro.codec.json.{AvroJsonDecoder, AvroJsonEncoder} | ||
import org.http4s.* | ||
import org.http4s.MediaType.application | ||
import org.http4s.headers.`Content-Type` | ||
import scodec.bits.ByteVector | ||
|
||
object AvroEntityCodec extends AvroEntityCodec: | ||
export Implicits.given | ||
|
||
trait AvroEntityCodec: | ||
|
||
def decodeEntity[F[_]: Async, A: AvroJsonDecoder]: EntityDecoder[F, A] = | ||
EntityDecoder.decodeBy(MediaType.application.json)(decodeJson) | ||
|
||
private def decodeJson[F[_]: Async, A: AvroJsonDecoder]( | ||
media: Media[F] | ||
): DecodeResult[F, A] = | ||
EitherT { | ||
media.body.compile | ||
.to(ByteVector) | ||
.map(decodeAvro) | ||
} | ||
|
||
private def decodeAvro[A: AvroJsonDecoder]: ByteVector => Either[DecodeFailure, A] = | ||
AvroJsonDecoder[A] | ||
.decode(_) | ||
.leftMap(err => | ||
MalformedMessageBodyFailure(s"Cannot decode Json Avro message: $err") | ||
) | ||
|
||
def encodeEntity[F[_], A: AvroJsonEncoder]: EntityEncoder[F, A] = | ||
EntityEncoder.simple(`Content-Type`(application.json))(a => | ||
Chunk.byteVector(AvroJsonEncoder[A].encode(a)) | ||
) | ||
|
||
trait Implicits: | ||
|
||
given entityDecoder[F[_]: Async, A: AvroJsonDecoder]: EntityDecoder[F, A] = | ||
decodeEntity[F, A] | ||
|
||
given entityEncoder[F[_], A: AvroJsonEncoder]: EntityEncoder[F, A] = | ||
encodeEntity[F, A] | ||
|
||
object Implicits extends Implicits |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@namespace("io.renku.api") | ||
protocol ApiShapes { | ||
|
||
/* An example record for a Project Entity returned from the Search API */ | ||
record Project { | ||
string id; | ||
string name; | ||
string description; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
modules/redis-client/src/test/scala/io/renku/redis/client/util/TestSearchRedisServer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.redis.client.util | ||
|
||
import cats.effect.{ExitCode, IO, IOApp} | ||
import io.renku.servers.RedisServer | ||
|
||
/** This is a utility to start a Redis server for manual testing */ | ||
object TestSearchRedisServer extends IOApp: | ||
|
||
override def run(args: List[String]): IO[ExitCode] = | ||
(IO(RedisServer.start()) >> IO.never[ExitCode]).as(ExitCode.Success) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# search-api | ||
|
||
This module exposes the Search API. |
43 changes: 43 additions & 0 deletions
43
modules/search-api/src/main/scala/io/renku/search/api/HttpApplication.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.search.api | ||
|
||
import cats.Monad | ||
import cats.effect.{Async, Resource} | ||
import fs2.io.net.Network | ||
import io.renku.solr.client.SolrConfig | ||
import org.http4s.dsl.Http4sDsl | ||
import org.http4s.server.Router | ||
import org.http4s.{HttpApp, HttpRoutes, Request, Response} | ||
|
||
object HttpApplication: | ||
def apply[F[_]: Async: Network]( | ||
solrConfig: SolrConfig | ||
): Resource[F, HttpApp[F]] = | ||
SearchApi[F](solrConfig).map(new HttpApplication[F](_).router) | ||
|
||
class HttpApplication[F[_]: Monad](searchApi: SearchApi[F]) extends Http4sDsl[F]: | ||
|
||
lazy val router: HttpApp[F] = | ||
Router[F]("/" -> routes).orNotFound | ||
|
||
private lazy val routes: HttpRoutes[F] = HttpRoutes.of[F] { | ||
case GET -> Root / "api" / phrase => searchApi.find(phrase) | ||
case GET -> Root / "ping" => Ok("pong") | ||
} |
38 changes: 38 additions & 0 deletions
38
modules/search-api/src/main/scala/io/renku/search/api/HttpServer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.search.api | ||
|
||
import cats.effect.{Async, Resource} | ||
import com.comcast.ip4s.{Port, ipv4, port} | ||
import fs2.io.net.Network | ||
import org.http4s.HttpApp | ||
import org.http4s.ember.server.EmberServerBuilder | ||
import org.http4s.server.Server | ||
|
||
object HttpServer: | ||
|
||
val port: Port = port"8080" | ||
|
||
def build[F[_]: Async: Network](app: HttpApp[F]): Resource[F, Server] = | ||
EmberServerBuilder | ||
.default[F] | ||
.withHost(ipv4"0.0.0.0") | ||
.withPort(port) | ||
.withHttpApp(app) | ||
.build |
Oops, something went wrong.