Skip to content

Commit

Permalink
Add a Kotlin DSL for endpoint documentation (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
Syer10 authored Nov 14, 2021
1 parent 845b588 commit b02884f
Show file tree
Hide file tree
Showing 5 changed files with 450 additions and 11 deletions.
3 changes: 2 additions & 1 deletion server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ dependencies {
implementation("com.squareup.okio:okio:2.10.0")

// Javalin api
implementation("io.javalin:javalin:4.0.0")
implementation("io.javalin:javalin:4.1.1")
implementation("io.javalin:javalin-openapi:4.1.1")
// jackson version locked by javalin, ref: `io.javalin.core.util.OptionalDependency`
val jacksonVersion = "2.12.4"
implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ object MangaAPI {
}

path("manga") {
get("{mangaId}", MangaController::retrieve)
get("{mangaId}", MangaController.retrieve)
get("{mangaId}/thumbnail", MangaController::thumbnail)

get("{mangaId}/category", MangaController::categoryList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,35 @@ import suwayomi.tachidesk.manga.impl.Chapter
import suwayomi.tachidesk.manga.impl.Library
import suwayomi.tachidesk.manga.impl.Manga
import suwayomi.tachidesk.manga.impl.Page
import suwayomi.tachidesk.manga.model.dataclass.MangaDataClass
import suwayomi.tachidesk.server.JavalinSetup.future
import suwayomi.tachidesk.server.util.handler
import suwayomi.tachidesk.server.util.pathParam
import suwayomi.tachidesk.server.util.queryParam
import suwayomi.tachidesk.server.util.withOperation

object MangaController {
/** get manga info */
fun retrieve(ctx: Context) {
val mangaId = ctx.pathParam("mangaId").toInt()
val onlineFetch = ctx.queryParam("onlineFetch")?.toBoolean() ?: false

ctx.future(
future {
Manga.getManga(mangaId, onlineFetch)
val retrieve = handler(
pathParam<Int>("mangaId"),
queryParam("onlineFetch", false),
documentWith = {
withOperation {
summary("Get a manga")
description("Get a manga from the database using a specific id")
}
)
}
},
behaviorOf = { ctx, mangaId, onlineFetch ->
ctx.future(
future {
Manga.getManga(mangaId, onlineFetch)
}
)
},
withResults = {
json<MangaDataClass>("OK")
}
)

/** manga thumbnail */
fun thumbnail(ctx: Context) {
Expand Down
20 changes: 20 additions & 0 deletions server/src/main/kotlin/suwayomi/tachidesk/server/JavalinSetup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import io.javalin.Javalin
import io.javalin.apibuilder.ApiBuilder.path
import io.javalin.core.security.RouteRole
import io.javalin.http.staticfiles.Location
import io.javalin.plugin.openapi.OpenApiOptions
import io.javalin.plugin.openapi.OpenApiPlugin
import io.javalin.plugin.openapi.ui.SwaggerOptions
import io.swagger.v3.oas.models.info.Info
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
Expand Down Expand Up @@ -46,6 +50,7 @@ object JavalinSetup {
logger.info { "Serving webUI static files" }
config.addStaticFiles(applicationDirs.webUIRoot, Location.EXTERNAL)
config.addSinglePageRoot("/", applicationDirs.webUIRoot + "/index.html", Location.EXTERNAL)
config.registerPlugin(OpenApiPlugin(getOpenApiOptions()))
}

config.enableCorsForAllOrigins()
Expand Down Expand Up @@ -98,6 +103,21 @@ object JavalinSetup {
}
}

private fun getOpenApiOptions(): OpenApiOptions {
val applicationInfo = Info().apply {
version("1.0")
description("Tachidesk Api")
}
return OpenApiOptions(applicationInfo).apply {
path("/api/openapi.json")
swagger(
SwaggerOptions("/api/swagger-ui").apply {
title("Tachidesk Swagger Documentation")
}
)
}
}

object Auth {
enum class Role : RouteRole { ANYONE, USER_READ, USER_WRITE }
}
Expand Down
Loading

0 comments on commit b02884f

Please sign in to comment.