Skip to content

Commit

Permalink
feat(openapi): provide swagger ui of model-server.yaml at endpoint …
Browse files Browse the repository at this point in the history
…`/swagger`
  • Loading branch information
nkoester committed Dec 11, 2023
1 parent cc5933a commit 07261d6
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 14 deletions.
6 changes: 3 additions & 3 deletions api/model-server-deprecated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ openapi: "3.0.3"
info:
title: "model-server deprecated API"
description: "modelix deprecated API"
version: "0.0.1"
version: "1.0.0"
servers:
- url: 'http://127.0.0.1:28101'
description: local model-server
- url: '/'
description: model-server
paths:
/json:
get:
Expand Down
6 changes: 3 additions & 3 deletions api/model-server-html.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ openapi: "3.0.3"
info:
title: "model-server content browser API"
description: "modelix content browser API"
version: "0.0.1"
version: "1.0.0"
servers:
- url: 'http://127.0.0.1:28101'
description: local model-server
- url: '/'
description: model-server
paths:
/:
get:
Expand Down
6 changes: 3 additions & 3 deletions api/model-server-light.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ openapi: "3.0.3"
info:
title: "model-server light API"
description: "modelix light API"
version: "0.0.1"
version: "1.0.0"
servers:
- url: 'http://127.0.0.1:28101'
description: local model-server
- url: '/'
description: model-server
paths:
# TODO: this is a WS, openAPI will not help here
/json/v2/{repositoryId}/ws:
Expand Down
6 changes: 3 additions & 3 deletions api/model-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ openapi: "3.0.3"
info:
title: "model-server public API"
description: "modelix public API"
version: "0.0.1"
version: "1.0.0"
servers:
- url: 'http://127.0.0.1:28101'
description: local model-server
- url: '/'
description: model-server
paths:
/v2/server-id:
get:
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ ktor-server-status-pages = { group = "io.ktor", name = "ktor-server-status-pages
ktor-server-test-host = { group = "io.ktor", name = "ktor-server-test-host", version.ref = "ktor" }
ktor-server-websockets = { group = "io.ktor", name = "ktor-server-websockets", version.ref = "ktor" }
ktor-server-resources = { group = "io.ktor", name = "ktor-server-resources", version.ref = "ktor" }
ktor-server-swagger = { group = "io.ktor", name = "ktor-server-swagger", version.ref = "ktor" }

ktor-client-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" }
ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktor" }
Expand Down
3 changes: 2 additions & 1 deletion model-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies {
implementation(libs.ktor.server.content.negotiation)
implementation(libs.ktor.server.resources)
implementation(libs.ktor.serialization.json)
implementation(libs.ktor.server.swagger)

implementation(libs.bundles.ignite)

Expand Down Expand Up @@ -190,7 +191,7 @@ spotless {

// OpenAPI integration
val basePackage = project.group.toString()
val openAPIgenerationPath = "${project.layout.buildDirectory}/generated/openapi"
val openAPIgenerationPath = "${project.layout.buildDirectory.get()}/generated/openapi"

// Pairs of the different OpenAPI files we use. Each pair must have its own 'category' as first argument as these
// are used to generate corresponding packages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ internal class CmdLineArgs {
)
var schemaInit = false

@Parameter(names = ["-noswagger"], description = "Disables SwaggerUI at endpoint '/swagger'", converter = BooleanConverter::class)
var noSwaggerUi: Boolean = false

@Parameter(names = ["-h", "--help"], help = true)
var help = false
}
13 changes: 13 additions & 0 deletions model-server/src/main/kotlin/org/modelix/model/server/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import io.ktor.server.netty.NettyApplicationEngine
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.plugins.cors.routing.CORS
import io.ktor.server.plugins.forwardedheaders.ForwardedHeaders
import io.ktor.server.plugins.swagger.swaggerUI
import io.ktor.server.resources.Resources
import io.ktor.server.response.respondText
import io.ktor.server.routing.IgnoreTrailingSlash
Expand Down Expand Up @@ -90,6 +91,8 @@ object Main {
LOG.info("Path to JDBC configuration file: " + cmdLineArgs.jdbcConfFile)
LOG.info("Schema initialization: " + cmdLineArgs.schemaInit)
LOG.info("Set values: " + cmdLineArgs.setValues)
LOG.info("Disable Swagger-UI: " + cmdLineArgs.noSwaggerUi)

if (cmdLineArgs.dumpOutName != null && !cmdLineArgs.inmemory) {
throw RuntimeException("For now dumps are supported only with the inmemory option")
}
Expand Down Expand Up @@ -224,11 +227,21 @@ object Main {
li {
a("user") { +"View JWT token and permissions" }
}
li {
a("swagger") { +"SwaggerUI" }
}
}
}
}
call.respondText("Model Server")
}
if (cmdLineArgs.noSwaggerUi) {
get("swagger") {
call.respondText("SwaggerUI is disabled")
}
} else {
swaggerUI(path = "swagger", swaggerFile = "../api/model-server.yaml")
}
}
}
ktorServer.start(wait = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import io.ktor.server.resources.post
import io.ktor.server.response.respond
import io.ktor.server.response.respondText
import io.ktor.server.routing.Route
import io.ktor.server.routing.get
import io.ktor.server.routing.route
import io.ktor.server.routing.routing
import io.ktor.server.websocket.webSocket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class PageWithMenuBar(val activePage: String, val baseUrl: String) : Template<HT
"json/" to "JSON API",
"headers" to "HTTP Headers",
"user" to "JWT token and permissions",
"swagger" to "SwaggerUI",
)
var classes = "menuItem"
if (activePage == "root") {
Expand Down

0 comments on commit 07261d6

Please sign in to comment.