From b00a159108eef3d873037463a7b11a5d1cf2eecd Mon Sep 17 00:00:00 2001 From: MysteryMS Date: Mon, 11 Dec 2023 23:52:58 -0300 Subject: [PATCH 1/3] docs route --- build.gradle.kts | 6 + .../kotlin/io/musicorum/api/Application.kt | 3 +- .../serialization/ThemeSerialization.kt | 5 +- .../io/musicorum/api/realms/docs/Routes.kt | 15 + src/main/resources/openapi/documentation.yaml | 264 ++++++++++++++++++ 5 files changed, 291 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/io/musicorum/api/realms/docs/Routes.kt create mode 100644 src/main/resources/openapi/documentation.yaml diff --git a/build.gradle.kts b/build.gradle.kts index 5143f0f..585c95e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -32,6 +32,12 @@ repositories { } dependencies { + // OpenAPI docs + implementation("io.ktor:ktor-server-openapi:$ktor_version") + + // Swagger + implementation("io.ktor:ktor-server-swagger:$ktor_version") + // Ktor implementation("io.ktor:ktor-server-core-jvm:$ktor_version") implementation("io.ktor:ktor-server-auth-jvm:$ktor_version") diff --git a/src/main/kotlin/io/musicorum/api/Application.kt b/src/main/kotlin/io/musicorum/api/Application.kt index a3a022b..d954ae8 100644 --- a/src/main/kotlin/io/musicorum/api/Application.kt +++ b/src/main/kotlin/io/musicorum/api/Application.kt @@ -9,6 +9,7 @@ import io.musicorum.api.koin.installKoin import io.musicorum.api.plugins.configureHTTP import io.musicorum.api.realms.auth.createAuthRoutes import io.musicorum.api.realms.collages.routes.createCollagesRoutes +import io.musicorum.api.realms.docs.createDocsRoute import io.musicorum.api.realms.resources.createResourcesRoutes import io.musicorum.api.security.configureSecurity @@ -29,5 +30,5 @@ fun Application.module() { createResourcesRoutes() createAuthRoutes() createCollagesRoutes() - + createDocsRoute() } diff --git a/src/main/kotlin/io/musicorum/api/realms/collages/themes/serialization/ThemeSerialization.kt b/src/main/kotlin/io/musicorum/api/realms/collages/themes/serialization/ThemeSerialization.kt index f452bac..e518268 100644 --- a/src/main/kotlin/io/musicorum/api/realms/collages/themes/serialization/ThemeSerialization.kt +++ b/src/main/kotlin/io/musicorum/api/realms/collages/themes/serialization/ThemeSerialization.kt @@ -17,7 +17,10 @@ object ThemeDataSerializer : KSerializer { val data = decoder.decodeSerializableValue(InputTheme.serializer()) val r = when (data.name) { - ThemeEnum.ClassicCollage -> ThemeData(data.name, Json.decodeFromJsonElement(data.options)) + ThemeEnum.ClassicCollage -> ThemeData( + data.name, + Json.decodeFromJsonElement(data.options) + ) } println("END") diff --git a/src/main/kotlin/io/musicorum/api/realms/docs/Routes.kt b/src/main/kotlin/io/musicorum/api/realms/docs/Routes.kt new file mode 100644 index 0000000..a36db86 --- /dev/null +++ b/src/main/kotlin/io/musicorum/api/realms/docs/Routes.kt @@ -0,0 +1,15 @@ +package io.musicorum.api.realms.docs + +import io.ktor.server.application.* +import io.ktor.server.plugins.openapi.* +import io.ktor.server.plugins.swagger.* +import io.ktor.server.routing.* +import io.swagger.codegen.languages.StaticHtml2Generator +import io.swagger.codegen.v3.generators.html.StaticHtml2Codegen +import io.swagger.codegen.v3.generators.html.StaticHtmlCodegen + +fun Application.createDocsRoute() { + routing { + swaggerUI(path = "documentation", swaggerFile = "openapi/documentation.yaml") + } +} \ No newline at end of file diff --git a/src/main/resources/openapi/documentation.yaml b/src/main/resources/openapi/documentation.yaml new file mode 100644 index 0000000..4ce8240 --- /dev/null +++ b/src/main/resources/openapi/documentation.yaml @@ -0,0 +1,264 @@ +openapi: "3.0.3" +info: + title: "api API" + description: "api API" + version: "1.0.0" +servers: +- url: "https://api" +paths: + /collages: + get: + description: "" + responses: + "200": + description: "OK" + content: + '*/*': + schema: + type: "array" + items: + $ref: "#/components/schemas/Collage" + post: + description: "" + requestBody: + content: + '*/*': + schema: + $ref: "#/components/schemas/CollagePayloadReq" + required: true + responses: + "200": + description: "OK" + content: + '*/*': + schema: {} + /workers: + get: + description: "" + responses: + "200": + description: "OK" + content: + '*/*': + schema: + $ref: "#/components/schemas/WorkersResponse" + /resources/artists: + post: + description: "" + requestBody: + content: + '*/*': + schema: + $ref: "#/components/schemas/ArtistResourcesRequest" + required: true + responses: + "200": + description: "OK" + content: + '*/*': + schema: + type: "array" + items: + $ref: "#/components/schemas/ArtistResource" + /auth/clients: + get: + description: "" + responses: + "200": + description: "OK" + content: + '*/*': + schema: + $ref: "#/components/schemas/Map_String" + post: + description: "" + requestBody: + content: + '*/*': + schema: + $ref: "#/components/schemas/ClientCreationDAO" + required: true + responses: + "200": + description: "OK" + content: + '*/*': + schema: + $ref: "#/components/schemas/Client" +components: + schemas: + Instant: + type: "object" + properties: {} + Collage: + type: "object" + properties: + id: + type: "string" + theme: + type: "string" + enum: + - "ClassicCollage" + file: + type: "string" + duration: + type: "integer" + format: "int64" + createdAt: + $ref: "#/components/schemas/Instant" + IGenerationData: + type: "object" + properties: {} + ThemeData: + type: "object" + properties: + name: + type: "string" + enum: + - "ClassicCollage" + options: + $ref: "#/components/schemas/IGenerationData" + CollagePayloadReq: + type: "object" + properties: + user: + type: "string" + theme: + $ref: "#/components/schemas/ThemeData" + hideUsername: + type: "boolean" + ExceptionResponse: + type: "object" + properties: + message: + type: "string" + CollageResponse: + type: "object" + properties: + duration: + type: "integer" + format: "int64" + file: + type: "string" + id: + type: "string" + url: + type: "string" + SerializableWorker: + type: "object" + properties: + name: + type: "string" + engine: + type: "string" + availableThemes: + type: "array" + items: + type: "string" + WorkersResponse: + type: "object" + properties: + workers: + type: "array" + items: + $ref: "#/components/schemas/SerializableWorker" + ArtistResourcesRequest: + type: "object" + properties: + artists: + type: "array" + items: + type: "string" + Image: + type: "object" + properties: + hash: + type: "string" + size: + type: "string" + enum: + - "EXTRA_SMALL" + - "SMALL" + - "MEDIUM" + - "LARGE" + - "EXTRA_LARGE" + url: + type: "string" + ColorPalette: + type: "object" + properties: + vibrant: + type: "string" + darkVibrant: + type: "string" + lightVibrant: + type: "string" + muted: + type: "string" + darkMuted: + type: "string" + lightMuted: + type: "string" + ImageResource: + type: "object" + properties: + hash: + type: "string" + explicit: + type: "boolean" + active: + type: "boolean" + source: + type: "string" + enum: + - "SPOTIFY" + - "LASTFM" + - "DEEZER" + images: + type: "array" + items: + $ref: "#/components/schemas/Image" + colorPalette: + $ref: "#/components/schemas/ColorPalette" + createdAt: + type: "string" + ArtistResource: + type: "object" + properties: + hash: + type: "string" + name: + type: "string" + resources: + type: "array" + items: + $ref: "#/components/schemas/ImageResource" + preferredResource: + type: "string" + createdAt: + type: "string" + updatedAt: + type: "string" + Map: + type: "object" + properties: {} + Map_String: + type: "string" + ClientCreationDAO: + type: "object" + properties: + name: + type: "string" + Client: + type: "object" + properties: + id: + type: "string" + name: + type: "string" + key: + type: "string" + createdAt: + $ref: "#/components/schemas/Instant" + updatedAt: + $ref: "#/components/schemas/Instant" \ No newline at end of file From 27870603da7f3b585235aa336453ff5b80c891aa Mon Sep 17 00:00:00 2001 From: MysteryMS Date: Tue, 12 Dec 2023 11:28:16 -0300 Subject: [PATCH 2/3] add status code to error pages --- src/main/kotlin/io/musicorum/api/plugins/StatusPages.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/io/musicorum/api/plugins/StatusPages.kt b/src/main/kotlin/io/musicorum/api/plugins/StatusPages.kt index 51ab55c..37911cc 100644 --- a/src/main/kotlin/io/musicorum/api/plugins/StatusPages.kt +++ b/src/main/kotlin/io/musicorum/api/plugins/StatusPages.kt @@ -3,11 +3,9 @@ package io.musicorum.api.plugins import io.ktor.http.* import io.ktor.serialization.* import io.ktor.server.application.* -import io.ktor.server.plugins.* import io.ktor.server.plugins.statuspages.* import io.ktor.server.response.* import kotlinx.serialization.Serializable -import kotlinx.serialization.SerializationException fun Application.installStatusPages() { install(StatusPages) { @@ -21,10 +19,12 @@ fun Application.installStatusPages() { println(cause.message) println(cause.cause) return@exception call.respond( + HttpStatusCode.BadRequest, ExceptionResponse("Serialization error: " + cause.message) ) } call.respond( + HttpStatusCode.BadRequest, ExceptionResponse(cause.message ?: "Unknown error") ) } From d4d6843cb2899ceea7acb5b162b2bf5d80e3dfd5 Mon Sep 17 00:00:00 2001 From: MysteryMS Date: Tue, 12 Dec 2023 11:28:30 -0300 Subject: [PATCH 3/3] import cleanup --- src/main/kotlin/io/musicorum/api/realms/docs/Routes.kt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/kotlin/io/musicorum/api/realms/docs/Routes.kt b/src/main/kotlin/io/musicorum/api/realms/docs/Routes.kt index a36db86..b7c35f9 100644 --- a/src/main/kotlin/io/musicorum/api/realms/docs/Routes.kt +++ b/src/main/kotlin/io/musicorum/api/realms/docs/Routes.kt @@ -1,12 +1,8 @@ package io.musicorum.api.realms.docs import io.ktor.server.application.* -import io.ktor.server.plugins.openapi.* import io.ktor.server.plugins.swagger.* import io.ktor.server.routing.* -import io.swagger.codegen.languages.StaticHtml2Generator -import io.swagger.codegen.v3.generators.html.StaticHtml2Codegen -import io.swagger.codegen.v3.generators.html.StaticHtmlCodegen fun Application.createDocsRoute() { routing {