Skip to content

Commit

Permalink
Merge branch 'bugfix/respect-rootPath' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
SMILEY4 committed Sep 15, 2022
2 parents 3db9bb6 + 04c2fb6 commit 5c4d708
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "io.github.smiley4"
version = "0.5.1"
version = "0.5.2"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ val SwaggerUI = createApplicationPlugin(name = "SwaggerUI", createConfiguration
pluginConfig.getSwaggerUI().swaggerUrl,
pluginConfig.getSwaggerUI().forwardRoot,
pluginConfig.getSwaggerUI().authentication,
application.environment.config,
SWAGGER_UI_WEBJARS_VERSION,
) { apiSpecJson }.setup(application)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.smiley4.ktorswaggerui
import io.ktor.server.application.Application
import io.ktor.server.application.call
import io.ktor.server.auth.authenticate
import io.ktor.server.config.ApplicationConfig
import io.ktor.server.response.respondRedirect
import io.ktor.server.routing.Route
import io.ktor.server.routing.get
Expand All @@ -17,6 +18,7 @@ class SwaggerRouting(
private val swaggerUrl: String,
private val forwardRoot: Boolean,
private val authentication: String?,
appConfig: ApplicationConfig,
swaggerWebjarVersion: String,
jsonSpecProvider: () -> String
) {
Expand All @@ -25,10 +27,22 @@ class SwaggerRouting(

private val controller = SwaggerController(
swaggerWebjarVersion = swaggerWebjarVersion,
apiSpecUrl = "/" + (if (swaggerUrl.startsWith("/")) swaggerUrl.substring(1) else swaggerUrl) + "/api.json",
apiSpecUrl = getApiSpecUrl(appConfig),
jsonSpecProvider = jsonSpecProvider
)

private fun getApiSpecUrl(appConfig: ApplicationConfig): String {
val rootPath = appConfig.propertyOrNull("ktor.deployment.rootPath")?.getString()?.let { "/${dropSlashes(it)}" } ?: ""
return "$rootPath/${dropSlashes(swaggerUrl)}/api.json"
}

private fun dropSlashes(str: String): String {
var value = str
value = if (value.startsWith("/")) value.substring(1) else value
value = if (value.endsWith("/")) value.substring(0, value.length - 1) else value
return value
}


/**
* registers the required routes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.github.smiley4.ktorswaggerui.examples

import io.github.smiley4.ktorswaggerui.SwaggerUI
import io.github.smiley4.ktorswaggerui.dsl.get
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.Application
import io.ktor.server.application.call
import io.ktor.server.application.install
import io.ktor.server.netty.EngineMain
import io.ktor.server.response.respondText
import io.ktor.server.routing.routing


fun main(args: Array<String>): Unit = EngineMain.main(args)

fun Application.module() {

install(SwaggerUI) {
swagger {
swaggerUrl = "swagger-ui"
forwardRoot = true
}
info {
title = "Example API"
version = "latest"
description = "Example api for testing"
}
server {
url = "http://localhost:8080"
description = "Development server"
}
}

routing {
get("hello", {
description = "Simple 'Hello World'- Route"
response {
HttpStatusCode.OK to {
description = "Successful Response"
}
}
}) {
call.respondText("Hello World!")
}
}
}

10 changes: 10 additions & 0 deletions src/test/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ktor {
deployment {
host = "localhost"
port = 8080
rootPath = "/root"
}
application {
modules = [io.github.smiley4.ktorswaggerui.examples.RootPathExampleKt.module]
}
}

0 comments on commit 5c4d708

Please sign in to comment.