-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add generating OpenApi yamls for the admin api (#2983)
- Loading branch information
Showing
16 changed files
with
297 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
Empty file.
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,12 @@ | ||
openapiDir := "./docs/03-endpoints/generated-openapi" | ||
|
||
# List all recipies | ||
default: | ||
@just --list | ||
|
||
# Update the OpenApi yml files by generating these from the tAPIr specs | ||
alias dog := docs-openapi-generate | ||
docs-openapi-generate: | ||
mkdir -p {{openapiDir}} | ||
rm {{openapiDir}}/*.yml >> /dev/null 2>&1 || true | ||
sbt "webapi/runMain org.knora.webapi.slice.common.api.DocsGenerator {{openapiDir}}" |
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
27 changes: 27 additions & 0 deletions
27
webapi/src/main/scala/org/knora/webapi/slice/admin/api/AdminApiEndpoints.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,27 @@ | ||
/* | ||
* Copyright © 2021 - 2024 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.knora.webapi.slice.admin.api | ||
|
||
import sttp.tapir.AnyEndpoint | ||
import zio.ZLayer | ||
|
||
final case class AdminApiEndpoints( | ||
maintenanceEndpoints: MaintenanceEndpoints, | ||
permissionsEndpoints: PermissionsEndpoints, | ||
projectsEndpoints: ProjectsEndpoints, | ||
usersEndpoints: UsersEndpoints | ||
) { | ||
|
||
val endpoints: Seq[AnyEndpoint] = | ||
maintenanceEndpoints.endpoints ++ | ||
permissionsEndpoints.endpoints ++ | ||
projectsEndpoints.endpoints ++ | ||
usersEndpoints.endpoints | ||
} | ||
|
||
object AdminApiEndpoints { | ||
val layer = ZLayer.derive[AdminApiEndpoints] | ||
} |
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
23 changes: 23 additions & 0 deletions
23
webapi/src/main/scala/org/knora/webapi/slice/common/api/ApiV2Endpoints.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,23 @@ | ||
/* | ||
* Copyright © 2021 - 2024 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.knora.webapi.slice.common.api | ||
|
||
import sttp.tapir.AnyEndpoint | ||
import zio.ZLayer | ||
|
||
import org.knora.webapi.slice.resourceinfo.api.ResourceInfoEndpoints | ||
import org.knora.webapi.slice.search.api.SearchEndpoints | ||
|
||
final case class ApiV2Endpoints(resourceInfoEndpoints: ResourceInfoEndpoints, searchEndpoints: SearchEndpoints) { | ||
|
||
val endpoints: Seq[AnyEndpoint] = | ||
resourceInfoEndpoints.endpoints ++ | ||
searchEndpoints.endpoints | ||
} | ||
|
||
object ApiV2Endpoints { | ||
val layer = ZLayer.derive[ApiV2Endpoints] | ||
} |
93 changes: 93 additions & 0 deletions
93
webapi/src/main/scala/org/knora/webapi/slice/common/api/DocsGenerator.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,93 @@ | ||
/* | ||
* Copyright © 2021 - 2024 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.knora.webapi.slice.common.api | ||
|
||
import org.apache.pekko.http.scaladsl.model.HttpResponse | ||
import org.apache.pekko.http.scaladsl.server.RequestContext | ||
import sttp.apispec.openapi.Server | ||
import sttp.apispec.openapi.circe.yaml.RichOpenAPI | ||
import sttp.tapir.AnyEndpoint | ||
import sttp.tapir.docs.openapi.OpenAPIDocsInterpreter | ||
import zio.Chunk | ||
import zio.Task | ||
import zio.ZIO | ||
import zio.ZIOAppArgs | ||
import zio.ZIOAppDefault | ||
import zio.ZLayer | ||
import zio.nio.file.Files | ||
import zio.nio.file.Path | ||
|
||
import org.knora.webapi.http.version.BuildInfo | ||
import org.knora.webapi.messages.admin.responder.usersmessages.UserIdentifierADM | ||
import org.knora.webapi.messages.v2.routing.authenticationmessages.KnoraCredentialsV2 | ||
import org.knora.webapi.routing.Authenticator | ||
import org.knora.webapi.slice.admin.api.AdminApiEndpoints | ||
import org.knora.webapi.slice.admin.api.MaintenanceEndpoints | ||
import org.knora.webapi.slice.admin.api.PermissionsEndpoints | ||
import org.knora.webapi.slice.admin.api.ProjectsEndpoints | ||
import org.knora.webapi.slice.admin.api.UsersEndpoints | ||
import org.knora.webapi.slice.admin.domain.model.User | ||
import org.knora.webapi.slice.resourceinfo.api.ResourceInfoEndpoints | ||
import org.knora.webapi.slice.search.api.SearchEndpoints | ||
|
||
final case class DocsNoopAuthenticator() extends Authenticator { | ||
override def getUserADM(requestContext: RequestContext): Task[User] = ??? | ||
override def calculateCookieName(): String = "KnoraAuthenticationMFYGSLTEMFZWG2BOON3WS43THI2DIMY9" | ||
override def getUserADMThroughCredentialsV2(credentials: Option[KnoraCredentialsV2]): Task[User] = ??? | ||
override def doLogoutV2(requestContext: RequestContext): Task[HttpResponse] = ??? | ||
override def doLoginV2(credentials: KnoraCredentialsV2.KnoraPasswordCredentialsV2): Task[HttpResponse] = ??? | ||
override def doAuthenticateV2(requestContext: RequestContext): Task[HttpResponse] = ??? | ||
override def presentLoginFormV2(requestContext: RequestContext): Task[HttpResponse] = ??? | ||
override def authenticateCredentialsV2(credentials: Option[KnoraCredentialsV2]): Task[Boolean] = ??? | ||
override def getUserByIdentifier(identifier: UserIdentifierADM): Task[User] = ??? | ||
} | ||
object DocsNoopAuthenticator { | ||
val layer = ZLayer.succeed(DocsNoopAuthenticator()) | ||
} | ||
|
||
object DocsGenerator extends ZIOAppDefault { | ||
|
||
private val interp: OpenAPIDocsInterpreter = OpenAPIDocsInterpreter() | ||
override def run: ZIO[ZIOAppArgs, java.io.IOException, Int] = { | ||
for { | ||
_ <- ZIO.logInfo("Generating OpenAPI docs") | ||
args <- getArgs | ||
adminEndpoints <- ZIO.serviceWith[AdminApiEndpoints](_.endpoints) | ||
v2Endpoints <- ZIO.serviceWith[ApiV2Endpoints](_.endpoints) | ||
path = Path(args.headOption.getOrElse("/tmp")) | ||
filesWritten <- writeToFile(adminEndpoints, path, "maintenance") <*> writeToFile(v2Endpoints, path, "v2") | ||
_ <- ZIO.logInfo(s"Wrote $filesWritten") | ||
} yield 0 | ||
}.provideSome[ZIOAppArgs]( | ||
AdminApiEndpoints.layer, | ||
ApiV2Endpoints.layer, | ||
BaseEndpoints.layer, | ||
DocsNoopAuthenticator.layer, | ||
MaintenanceEndpoints.layer, | ||
PermissionsEndpoints.layer, | ||
ProjectsEndpoints.layer, | ||
ResourceInfoEndpoints.layer, | ||
SearchEndpoints.layer, | ||
UsersEndpoints.layer | ||
) | ||
|
||
private def writeToFile(endpoints: Seq[AnyEndpoint], path: Path, name: String) = { | ||
val content = interp | ||
.toOpenAPI(endpoints, s"${BuildInfo.name}-$name", BuildInfo.version) | ||
.servers( | ||
List( | ||
Server(url = "http://localhost:3333", description = Some("Local development server")), | ||
Server(url = "https://api.dasch.swiss", description = Some("Production server")) | ||
) | ||
) | ||
for { | ||
_ <- ZIO.logInfo(s"Writing to $path") | ||
target = path / s"openapi-$name.yml" | ||
_ <- Files.deleteIfExists(target) *> Files.createFile(target) | ||
_ <- Files.writeBytes(target, Chunk.fromArray(content.toYaml.getBytes)) | ||
} yield target | ||
} | ||
} |
Oops, something went wrong.