Skip to content

Commit

Permalink
support ktor.deployment.rootPath
Browse files Browse the repository at this point in the history
  • Loading branch information
mcxinyu committed May 9, 2024
1 parent 8f579e1 commit b6e33a5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.github.smiley4.ktorswaggerui.builder.schema.TypeOverwrites
import io.github.smiley4.ktorswaggerui.data.PluginConfigData
import io.github.smiley4.ktorswaggerui.dsl.PluginConfigDsl
import io.github.smiley4.ktorswaggerui.routing.ApiSpec
import io.github.smiley4.ktorswaggerui.routing.ControllerUtils
import io.github.smiley4.ktorswaggerui.routing.ForwardRouteController
import io.github.smiley4.ktorswaggerui.routing.SwaggerController
import io.ktor.server.application.*
Expand Down Expand Up @@ -44,6 +45,7 @@ val SwaggerUI = createApplicationPlugin(name = "SwaggerUI", createConfiguration
}

try {
ControllerUtils.appConfig = applicationConfig
val routes = routes(application, config)
ApiSpec.setAll(buildOpenApiSpecs(config, routes))
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.smiley4.ktorswaggerui.builder.openapi

import io.github.smiley4.ktorswaggerui.builder.route.RouteMeta
import io.github.smiley4.ktorswaggerui.routing.ControllerUtils
import io.swagger.v3.oas.models.PathItem
import io.swagger.v3.oas.models.Paths

Expand All @@ -21,7 +22,8 @@ class PathsBuilder(
}

private fun addAsNewPath(paths: Paths, route: RouteMeta) {
paths.addPathItem(route.path, pathBuilder.build(route))
val rootPath = ControllerUtils.appConfig?.let { ControllerUtils.getRootPath(it) } ?: ""
paths.addPathItem("$rootPath${route.path}", pathBuilder.build(route))
}

private fun addToExistingPath(existing: PathItem, route: RouteMeta) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.github.smiley4.ktorswaggerui.routing

import io.ktor.server.config.ApplicationConfig
import io.ktor.server.config.*

object ControllerUtils {

var appConfig: ApplicationConfig? = null

fun getRootPath(appConfig: ApplicationConfig): String {
return appConfig.propertyOrNull("ktor.deployment.rootPath")?.getString()?.let { "/${dropSlashes(it)}" } ?: ""
}
Expand Down

0 comments on commit b6e33a5

Please sign in to comment.