-
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.
- Loading branch information
1 parent
fcb3a04
commit bba9cbd
Showing
13 changed files
with
532 additions
and
437 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package no.java.partner.plugins | ||
|
||
import io.bkbn.kompendium.core.plugin.NotarizedApplication | ||
import io.bkbn.kompendium.core.routes.swagger | ||
import io.bkbn.kompendium.json.schema.definition.TypeDefinition | ||
import io.bkbn.kompendium.oas.OpenApiSpec | ||
import io.bkbn.kompendium.oas.component.Components | ||
import io.bkbn.kompendium.oas.info.Info | ||
import io.bkbn.kompendium.oas.security.BearerAuth | ||
import io.bkbn.kompendium.oas.server.Server | ||
import io.ktor.server.application.Application | ||
import io.ktor.server.application.install | ||
import io.ktor.server.routing.routing | ||
import java.net.URI | ||
import java.time.Instant | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
import kotlin.reflect.typeOf | ||
|
||
fun Application.configureOpenApi(version: String, port: Int) { | ||
install(NotarizedApplication()) { | ||
spec = OpenApiSpec( | ||
jsonSchemaDialect = "https://spec.openapis.org/oas/3.1/dialect/base", | ||
info = Info( | ||
"Orbit API", | ||
version = version, | ||
description = "API for managing javaBin partners", | ||
), | ||
servers = mutableListOf( | ||
Server( | ||
url = URI("http://localhost:$port"), | ||
description = "Dev", | ||
), | ||
), | ||
components = Components( | ||
securitySchemes = mutableMapOf( | ||
"auth-jwt" to BearerAuth(), | ||
), | ||
), | ||
) | ||
customTypes = | ||
mapOf( | ||
typeOf<Instant>() to TypeDefinition(type = "string", format = "date-time"), | ||
typeOf<LocalDate>() to TypeDefinition(type = "string", format = "date"), | ||
typeOf<LocalDateTime>() to TypeDefinition(type = "string", format = "date-time"), | ||
) | ||
} | ||
|
||
routing { | ||
swagger(pageTitle = "javaBin Partner API") | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
src/main/kotlin/no/java/partner/plugins/openapi/CommonResponses.kt
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,58 @@ | ||
package no.java.partner.plugins.openapi | ||
|
||
import io.bkbn.kompendium.core.metadata.ResponseInfo | ||
import io.ktor.http.HttpStatusCode | ||
import kotlin.reflect.KType | ||
import kotlin.reflect.typeOf | ||
|
||
private sealed class StandardApiResponseInfo( | ||
private val statusCode: HttpStatusCode, | ||
private val description: String, | ||
private val responseType: KType, | ||
private vararg val examples: Any, | ||
) { | ||
|
||
fun build() = ResponseInfo.builder { | ||
responseCode(statusCode) | ||
description(description) | ||
responseType(responseType) | ||
@Suppress("SpreadOperator") | ||
examples(*(examples.mapIndexed { idx, example -> Pair("example-$idx", example) }.toTypedArray())) | ||
} | ||
} | ||
|
||
private data object BadRequestResponse : StandardApiResponseInfo( | ||
HttpStatusCode.BadRequest, | ||
"Bad Request - could not parse request", | ||
typeOf<String>(), | ||
"ID Parameter missing", | ||
) | ||
|
||
private data object NotFoundResponse : StandardApiResponseInfo( | ||
HttpStatusCode.NotFound, | ||
"Not Found - could not find something", | ||
typeOf<String>(), | ||
"Partner Not Found", | ||
"List Not Found", | ||
"Contact Not Found", | ||
) | ||
|
||
private data object UnauthorizedResponse : StandardApiResponseInfo( | ||
HttpStatusCode.Unauthorized, | ||
"Unauthorized - not logged in", | ||
typeOf<String>(), | ||
"Missing Principal", | ||
"Not an admin", | ||
) | ||
|
||
private data object InternalServerErrorResponse : StandardApiResponseInfo( | ||
HttpStatusCode.InternalServerError, | ||
"Internal Server Error - something went wrong", | ||
typeOf<String>(), | ||
"Something went wrong", | ||
) | ||
|
||
val BadRequestResponseInfo = BadRequestResponse.build() | ||
val NotFoundResponseInfo = NotFoundResponse.build() | ||
val UnauthorizedResponseInfo = UnauthorizedResponse.build() | ||
val InternalServerErrorResponseInfo = InternalServerErrorResponse.build() |
Oops, something went wrong.