Skip to content
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: OpenAPI docs exposed as /search/spec.json #25

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ lazy val searchApi = project
Dependencies.http4sDsl ++
Dependencies.http4sServer ++
Dependencies.tapirHttp4sServer ++
Dependencies.tapirOpenAPi
Dependencies.tapirOpenAPI
)
.dependsOn(
commons % "compile->compile;test->test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ package io.renku.search.api
import cats.effect.{Async, Resource}
import cats.syntax.all.*
import fs2.io.net.Network
import io.circe.syntax.given
import io.renku.search.http.borer.TapirBorerJson
import io.renku.solr.client.SolrConfig
import org.http4s.dsl.Http4sDsl
import org.http4s.server.Router
import org.http4s.{HttpApp, HttpRoutes, Response}
import sttp.apispec.openapi.circe.given
import sttp.tapir.*
import sttp.tapir.docs.openapi.OpenAPIDocsOptions
import sttp.tapir.docs.openapi.OpenAPIDocsInterpreter
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.server.http4s.Http4sServerInterpreter
import sttp.tapir.swagger.SwaggerUIOptions
import sttp.tapir.swagger.bundle.SwaggerInterpreter

object HttpApplication:
def apply[F[_]: Async: Network](
Expand All @@ -52,7 +52,7 @@ class HttpApplication[F[_]: Async](searchApi: SearchApi[F])
).orNotFound

private lazy val businessRoutes: HttpRoutes[F] =
Http4sServerInterpreter[F]().toRoutes(swaggerEndpoints ::: businessEndpoints)
Http4sServerInterpreter[F]().toRoutes(openAPIEndpoint :: businessEndpoints)

private lazy val businessEndpoints: List[ServerEndpoint[Any, F]] =
List(
Expand All @@ -69,10 +69,16 @@ class HttpApplication[F[_]: Async](searchApi: SearchApi[F])
.out(borerJsonBody[List[SearchEntity]])
.description("Search API for searching Renku entities")

private lazy val swaggerEndpoints =
SwaggerInterpreter(
swaggerUIOptions = SwaggerUIOptions.default.copy(contextPath = List(businessRoot))
).fromServerEndpoints[F](businessEndpoints, "Search API", "0.0.1")
private lazy val openAPIEndpoint =
val docs = OpenAPIDocsInterpreter()
.serverEndpointsToOpenAPI(businessEndpoints, "Search API", "0.0.1")

endpoint
.in("spec.json")
.get
.out(stringJsonBody)
.description("OpenAPI docs")
.serverLogic(_ => docs.asJson.spaces2.asRight.pure[F])

private lazy val operationsRoutes: HttpRoutes[F] =
Http4sServerInterpreter[F]().toRoutes(List(pingEndpoint))
Expand Down
6 changes: 4 additions & 2 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ object Dependencies {
val scodec = "2.2.2"
val scodecBits = "1.1.38"
val scribe = "3.13.0"
val sttpApiSpec = "0.7.4"
val tapir = "1.9.9"
}

Expand Down Expand Up @@ -116,7 +117,8 @@ object Dependencies {
val tapirHttp4sServer = Seq(
"com.softwaremill.sttp.tapir" %% "tapir-http4s-server" % V.tapir
)
val tapirOpenAPi = Seq(
"com.softwaremill.sttp.tapir" %% "tapir-swagger-ui-bundle" % V.tapir
val tapirOpenAPI = Seq(
"com.softwaremill.sttp.tapir" %% "tapir-openapi-docs" % V.tapir,
"com.softwaremill.sttp.apispec" %% "openapi-circe-yaml" % V.sttpApiSpec
)
}
Loading