-
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.
Refactoring routes, add public version endpoint - Refactors code around routes with the goal to open it for extension and make it better testable - Add tests for all provided routes - Add a public `/version` endpoint returning version information for this service - Use `/api/search` as a url path prefix for the public routes, so querying will be `/api/search/query` and version is `/api/search/version` - This goes together with a change in gateway to pass the full request path - The old way, `/search`, is kept alive to have an easier transition - Additionally provide the same under `/search/query` so clients (ui) can already adopt to the new path even when gateway hasn't been updated - This "old" behaviour is in separate "legacy" classes and can be removed later - Log request and responses to the public api, but not to internal endpoints; allowing to be extended with more middlewares if necessary
- Loading branch information
Showing
16 changed files
with
706 additions
and
149 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
87 changes: 0 additions & 87 deletions
87
modules/search-api/src/main/scala/io/renku/search/api/Routes.scala
This file was deleted.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
modules/search-api/src/main/scala/io/renku/search/api/SearchServer.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,80 @@ | ||
/* | ||
* 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.* | ||
import cats.syntax.all.* | ||
import fs2.io.net.Network | ||
|
||
import io.renku.search.api.routes.OpenApiLegacyRoute | ||
import io.renku.search.http.HttpServer | ||
import io.renku.search.http.metrics.MetricsRoutes | ||
import io.renku.search.http.routes.OperationRoutes | ||
import io.renku.search.metrics.CollectorRegistryBuilder | ||
import org.http4s.HttpRoutes | ||
import org.http4s.server.middleware.ResponseLogger | ||
import org.http4s.server.middleware.{RequestId, RequestLogger} | ||
import scribe.Scribe | ||
|
||
object SearchServer: | ||
def create[F[_]: Async: Network](config: SearchApiConfig, app: ServiceRoutes[F]) = | ||
for | ||
routes <- makeHttpRoutes(app) | ||
server <- HttpServer[F](config.httpServerConfig) | ||
.withHttpApp(routes.orNotFound) | ||
.build | ||
yield server | ||
|
||
def makeHttpRoutes[F[_]: Async]( | ||
app: ServiceRoutes[F] | ||
): Resource[F, HttpRoutes[F]] = | ||
val openApiLegacy = OpenApiLegacyRoute(app.docEndpoints).routes | ||
val opRoutes = OperationRoutes[F] | ||
val metrics = MetricsRoutes[F](CollectorRegistryBuilder[F].withJVMMetrics) | ||
for | ||
logger <- Resource.pure(scribe.cats.effect[F]) | ||
businessRoutes <- metrics.makeRoutes(app.routes) | ||
routes = List( | ||
app.openapiDocRoutes, | ||
openApiLegacy, | ||
withMiddleware(logger, businessRoutes), | ||
opRoutes | ||
).reduce(_ <+> _) | ||
yield routes | ||
|
||
def withMiddleware[F[_]: Async]( | ||
logger: Scribe[F], | ||
httpRoutes: HttpRoutes[F] | ||
): HttpRoutes[F] = | ||
middleWares[F](logger).foldLeft(httpRoutes)((r, mf) => mf(r)) | ||
|
||
private def middleWares[F[_]: Async]( | ||
logger: Scribe[F] | ||
): List[HttpRoutes[F] => HttpRoutes[F]] = | ||
val log = (str: String) => logger.info(str) | ||
List( | ||
RequestLogger | ||
.httpRoutes(logHeaders = true, logBody = false, logAction = log.some), | ||
RequestId.httpRoutes[F], | ||
ResponseLogger.httpRoutes( | ||
logHeaders = true, | ||
logBody = false, | ||
logAction = log.some | ||
) | ||
) |
77 changes: 77 additions & 0 deletions
77
modules/search-api/src/main/scala/io/renku/search/api/ServiceRoutes.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,77 @@ | ||
/* | ||
* 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 cats.syntax.all.* | ||
import fs2.io.net.Network | ||
|
||
import io.renku.openid.keycloak.JwtVerify | ||
import io.renku.search.api.auth.Authenticate | ||
import io.renku.search.api.routes.* | ||
import io.renku.search.http.ClientBuilder | ||
import io.renku.search.http.RetryConfig | ||
import org.http4s.HttpRoutes | ||
import org.http4s.ember.client.EmberClientBuilder | ||
import org.http4s.server.Router | ||
|
||
/** Defines the routes for the whole search service */ | ||
trait ServiceRoutes[F[_]] extends RoutesDefinition[F]: | ||
def config: SearchApiConfig | ||
def pathPrefix: List[String] | ||
def openapiDocRoutes: HttpRoutes[F] | ||
|
||
object ServiceRoutes: | ||
def apply[F[_]: Async: Network]( | ||
cfg: SearchApiConfig, | ||
prefix: List[String] | ||
): Resource[F, ServiceRoutes[F]] = | ||
for | ||
logger <- Resource.pure(scribe.cats.effect[F]) | ||
httpClient <- ClientBuilder(EmberClientBuilder.default[F]) | ||
.withDefaultRetry(RetryConfig.default) | ||
.withLogging(logBody = false, logger) | ||
.build | ||
|
||
searchApi <- SearchApi[F](cfg.solrConfig) | ||
jwtVerify <- Resource.eval(JwtVerify(httpClient, cfg.jwtVerifyConfig)) | ||
authenticate = Authenticate[F](jwtVerify, logger) | ||
|
||
routeDefs = List( | ||
SearchRoutes(searchApi, authenticate, Nil), | ||
VersionRoute[F](Nil) | ||
) | ||
legacyDefs = List( | ||
SearchLegacyRoutes(searchApi, authenticate, Nil) | ||
) | ||
yield new ServiceRoutes[F] { | ||
override val config = cfg | ||
override val pathPrefix = prefix | ||
private val pathPrefixStr = prefix.mkString("/", "/", "") | ||
|
||
override val docEndpoints = | ||
(routeDefs ++ legacyDefs).map(_.docEndpoints).reduce(_ ++ _) | ||
override val openapiDocRoutes: HttpRoutes[F] = | ||
OpenApiRoute(docEndpoints, prefix).routes | ||
override val routes = Router( | ||
pathPrefixStr -> routeDefs.map(_.routes).reduce(_ <+> _), | ||
"/search/query" -> legacyDefs.map(_.routes).reduce(_ <+> _), | ||
"/search" -> legacyDefs.map(_.routes).reduce(_ <+> _) | ||
) | ||
} |
Oops, something went wrong.