-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: a simple service exposing Search API #8
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9cfaede
chore: readme for search-api
jachro 36b9491
chore: search-api added to build.sbt
jachro 87870c3
feat: solr client to allow specifying a search phrase
jachro e37cdb9
chore: build.sbt incomplete definition of the search-api
jachro a3236f5
feat: avro JSON codec; SearchApi with a spec
jachro 5cf9fff
feat: Search API Microservice and manual testing facilities
jachro 8151510
chore: formatting
jachro 831940e
refactor: given instead of implicit in AvroEntityCodec
jachro 6d36d13
refactor: Logger per class instead of global one
jachro 37d38ea
refactor: response creation in the SearchApi simplified
jachro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.MediaType.application | ||
import org.http4s.headers.`Content-Type` | ||
import org.http4s.{DecodeFailure, DecodeResult, EntityDecoder, EntityEncoder, MalformedMessageBodyFailure, Media, MediaType} | ||
import scodec.bits.ByteVector | ||
|
||
object AvroEntityCodec extends AvroEntityCodec: | ||
export Implicits.* | ||
|
||
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: | ||
|
||
implicit def entityDecoder[F[_]: Async, A: AvroJsonDecoder]: EntityDecoder[F, A] = | ||
decodeEntity[F, A] | ||
|
||
implicit def 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. |
44 changes: 44 additions & 0 deletions
44
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,44 @@ | ||
/* | ||
* 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} | ||
import scribe.Scribe | ||
|
||
object HttpApplication: | ||
def apply[F[_]: Async: Network: Scribe]( | ||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can now use
given
:) (I might forgot to change that myself when I used code from somewhere else:))There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I used metrics collector as a template ;)