-
Notifications
You must be signed in to change notification settings - Fork 34
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
Showing
8 changed files
with
326 additions
and
52 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
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/io/github/smiley4/ktorswaggerui/routing/ApiSpec.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,24 @@ | ||
package io.github.smiley4.ktorswaggerui.routing | ||
|
||
object ApiSpec { | ||
|
||
private val apiSpecs = mutableMapOf<String, String>() | ||
|
||
fun setAll(specs: Map<String, String>) { | ||
apiSpecs.clear() | ||
apiSpecs.putAll(specs) | ||
} | ||
|
||
fun set(name: String, spec: String) { | ||
apiSpecs[name] = spec | ||
} | ||
|
||
fun get(name: String): String { | ||
return apiSpecs[name] ?: throw Exception("No api-spec with name $name registered.") | ||
} | ||
|
||
fun getAll(): Map<String, String> { | ||
return apiSpecs | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
src/main/kotlin/io/github/smiley4/ktorswaggerui/routing/manualRouting.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,81 @@ | ||
package io.github.smiley4.ktorswaggerui.routing | ||
|
||
import io.github.smiley4.ktorswaggerui.SWAGGER_UI_WEBJARS_VERSION | ||
import io.github.smiley4.ktorswaggerui.SwaggerUI | ||
import io.github.smiley4.ktorswaggerui.data.SwaggerUIData | ||
import io.github.smiley4.ktorswaggerui.data.SwaggerUiSort | ||
import io.github.smiley4.ktorswaggerui.dsl.PluginConfigDsl | ||
import io.github.smiley4.ktorswaggerui.dsl.route | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
|
||
|
||
fun Route.openApiSpec(specId: String = PluginConfigDsl.DEFAULT_SPEC_ID) { | ||
route({ hidden = true }) { | ||
get { | ||
call.respondText(ContentType.Application.Json, HttpStatusCode.OK) { ApiSpec.get(specId) } | ||
} | ||
} | ||
} | ||
|
||
fun Route.swaggerUI(apiUrl: String) { | ||
route({ hidden = true }) { | ||
get { | ||
call.respondRedirect("${call.request.uri}/index.html") | ||
} | ||
get("{filename}") { | ||
serveStaticResource(call.parameters["filename"]!!, SWAGGER_UI_WEBJARS_VERSION, call) | ||
} | ||
get("swagger-initializer.js") { | ||
serveSwaggerInitializer(call, SwaggerUIData.DEFAULT, apiUrl) | ||
} | ||
} | ||
} | ||
|
||
private suspend fun serveSwaggerInitializer(call: ApplicationCall, swaggerUiConfig: SwaggerUIData, apiUrl: String) { | ||
// see https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md for reference | ||
val propValidatorUrl = swaggerUiConfig.validatorUrl?.let { "validatorUrl: \"$it\"" } ?: "validatorUrl: false" | ||
val propDisplayOperationId = "displayOperationId: ${swaggerUiConfig.displayOperationId}" | ||
val propFilter = "filter: ${swaggerUiConfig.showTagFilterInput}" | ||
val propSort = "operationsSorter: " + | ||
if (swaggerUiConfig.sort == SwaggerUiSort.NONE) "undefined" | ||
else "\"${swaggerUiConfig.sort.value}\"" | ||
val propSyntaxHighlight = "syntaxHighlight: { theme: \"${swaggerUiConfig.syntaxHighlight.value}\" }" | ||
val content = """ | ||
window.onload = function() { | ||
window.ui = SwaggerUIBundle({ | ||
url: "$apiUrl", | ||
dom_id: '#swagger-ui', | ||
deepLinking: true, | ||
presets: [ | ||
SwaggerUIBundle.presets.apis, | ||
SwaggerUIStandalonePreset | ||
], | ||
plugins: [ | ||
SwaggerUIBundle.plugins.DownloadUrl | ||
], | ||
layout: "StandaloneLayout", | ||
withCredentials: ${swaggerUiConfig.withCredentials}, | ||
$propValidatorUrl, | ||
$propDisplayOperationId, | ||
$propFilter, | ||
$propSort, | ||
$propSyntaxHighlight | ||
}); | ||
}; | ||
""".trimIndent() | ||
call.respondText(ContentType.Application.JavaScript, HttpStatusCode.OK) { content } | ||
} | ||
|
||
private suspend fun serveStaticResource(filename: String, swaggerWebjarVersion: String, call: ApplicationCall) { | ||
val resourceName = "/META-INF/resources/webjars/swagger-ui/$swaggerWebjarVersion/$filename" | ||
val resource = SwaggerUI::class.java.getResource(resourceName) | ||
if (resource != null) { | ||
call.respond(ResourceContent(resource)) | ||
} else { | ||
call.respond(HttpStatusCode.NotFound, "$filename could not be found") | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/test/kotlin/io/github/smiley4/ktorswaggerui/examples/ManualRouting.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,43 @@ | ||
package io.github.smiley4.ktorswaggerui.examples | ||
|
||
import io.github.smiley4.ktorswaggerui.SwaggerUI | ||
import io.github.smiley4.ktorswaggerui.dsl.get | ||
import io.github.smiley4.ktorswaggerui.routing.openApiSpec | ||
import io.github.smiley4.ktorswaggerui.routing.swaggerUI | ||
import io.ktor.server.application.* | ||
import io.ktor.server.engine.* | ||
import io.ktor.server.netty.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
|
||
/** | ||
* An example showcasing manual swaggerui-routing | ||
*/ | ||
fun main() { | ||
embeddedServer(Netty, port = 8080, host = "localhost", module = Application::myModule).start(wait = true) | ||
} | ||
|
||
private fun Application.myModule() { | ||
|
||
install(SwaggerUI) { | ||
swagger { | ||
automaticRouter = false | ||
} | ||
} | ||
|
||
routing { | ||
|
||
route("swagger") { | ||
swaggerUI("/api.json") | ||
} | ||
route("api.json") { | ||
openApiSpec() | ||
} | ||
|
||
get("hello", { | ||
description = "Simple 'Hello World'- Route" | ||
}) { | ||
call.respondText("Hello World!") | ||
} | ||
} | ||
} |
Oops, something went wrong.